-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
150 lines (124 loc) · 3.6 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const express = require('express');
const { PrismaClient } = require('@prisma/client');
const cors = require('cors');
const path = require('node:path');
const prisma = new PrismaClient();
const app = express();
const PORT = 3001;
app.use(express.json());
app.use(cors());
app.use(express.static(path.join(__dirname, 'public')));
app.get('/api/hotels', async (req, res) => {
try {
const hotels = await prisma.hotel.findMany();
res.json(hotels);
} catch (error) {
console.error('Error fetching hotels:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
app.get('/api/hotels/:id', async (req, res) => {
const { id } = req.params;
try {
const hotel = await prisma.hotel.findUnique({
where: { id: parseInt(id) },
include: { reviews: true }, // Include the associated reviews
});
if (!hotel) {
return res.status(404).json({ error: 'Hotel not found' });
}
res.json(hotel);
} catch (error) {
console.error('Error fetching hotel:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
app.post('/api/hotels/:id/reviews', async (req, res) => {
const { id } = req.params;
const { rating, comment, username } = req.body;
if (!username) {
return res.status(401).json({ error: 'User not authenticated' });
}
try {
const hotelId = parseInt(id, 10);
if (isNaN(hotelId)) {
res.status(400).json({ error: 'Invalid hotel ID' });
return;
}
const review = await prisma.review.create({
data: {
hotelId,
username,
rating,
comment,
},
});
res.status(201).json({ review });
} catch (error) {
console.error('Error creating review:', error);
res.status(500).json({ error: 'Failed to create review' });
}
});
app.get('/api/hotels/:id/review', async (req, res) => {
const { id } = req.params;
try {
const review = await prisma.review.findFirst({
where: { hotelId: parseInt(id) },
});
if (!review) {
return res.status(404).json({ error: 'Review not found' });
}
res.json(review);
} catch (error) {
console.error('Error fetching review:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
app.get('/api/reviews', async (req, res) => {
try {
const reviews = await prisma.review.findMany();
res.json(reviews);
} catch (error) {
console.error('Error fetching reviews:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
app.get('/api/hotels/:id/reviews', async (req, res) => {
const { id } = req.params;
try {
const reviews = await prisma.review.findMany({
where: { hotelId: parseInt(id) },
});
res.json(reviews);
} catch (error) {
console.error('Error fetching reviews:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
app.get('/api/posts', async (req, res) => {
try {
const posts = await prisma.post.findMany();
res.json(posts);
} catch (error) {
console.error('Error fetching posts:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
app.get('/api/posts/:postId', async (req, res) => {
const { postId } = req.params;
try {
const post = await prisma.post.findUnique({
where: { id: parseInt(postId) },
});
if (!post) {
return res.status(404).json({ error: 'Post not found' });
}
res.json(post);
} catch (error) {
console.error('Error fetching post:', error);
res.status(500).json({ error: 'Internal server error' });
}
});