-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
231 lines (219 loc) · 6.52 KB
/
server.js
File metadata and controls
231 lines (219 loc) · 6.52 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
const express = require("express");
const app = express();
const fs = require("fs");
const { Pool } = require("pg");
const bodyParser = require("body-parser");
const PORT = 4000;
const getFunction = (request, response) => {
//reading file
fs.readFile("hello.json", "utf8", (error, data) => {
const obj = JSON.parse(data);
response.json(obj);
});
};
const pool = new Pool({
user: "postgres",
host: "localhost",
database: "cyf-hotels",
password: "idkfaiddqd",
port: 5432,
});
const dbFunction = (request, response) => {
pool.query("select * from hotels", (error, result) => {
response.json(result.rows);
});
};
//POST endpoint for a new hostel
app.use(bodyParser.json());
app.post("/hotels", function (req, res) {
const newHotelName = req.body.name;
let newHotelRooms = req.body.rooms;
const newHotelPostcode = req.body.postcode;
if (!Number.isInteger(newHotelRooms) || newHotelRooms <= 0) {
return res
.status(400)
.send("The number of rooms should be a positive integer.");
}
pool
.query("SELECT * FROM hotels WHERE name=$1", [newHotelName])
.then((result) => {
if (result.rows.length > 0) {
return res
.status(400)
.send("An hotel with the same name already exists!");
} else {
const query =
"INSERT INTO hotels (name, rooms, postcode) VALUES ($1, $2, $3) RETURNING id";
pool
.query(query, [newHotelName, newHotelRooms, newHotelPostcode])
.then(() => res.status(201).send("Hotel created!"))
.catch((e) => console.error(e));
}
});
});
// POST customers endpoint
app.post("/customers", function (req, res) {
const newCustomerName = req.body.name;
const newCustomerEmail = req.body.email;
const newCustomerAddress = req.body.address;
const newCustomerCity = req.body.city;
const newCustomerPostcode = req.body.postcode;
const newCustomerCountry = req.body.country;
pool
.query("SELECT * FROM customers WHERE name=$1", [newCustomerName])
.then((result) => {
if (result.rows.length > 0) {
return res
.status(400)
.send("A customer with the same name already exists!");
} else {
const query =
"INSERT INTO customers (name, email, address, city, postcode, country ) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id";
pool
.query(query, [
newCustomerName,
newCustomerEmail,
newCustomerAddress,
newCustomerCity,
newCustomerPostcode,
newCustomerCountry,
])
.then(() =>
res.status(201).send(`Customer ${newCustomerId} created!`)
)
.catch((e) => console.error(e));
}
});
});
// customer name, check in date, number of nights, hotel name, hotel postcose.
const getCustomerBookings = (req, res) => {
const customerId = req.params.customerId;
// res.send(customers.find((u) => u.id === customerId));
pool
.query(
`select
c.name as customer_name,
b.checkin_date,
b.nights,
h.name,
h.postcode
from customers c
inner join bookings b on b.customer_id = c.id
inner join hotels h on h.id = b.hotel_id
where c.id =$1 `,
[customerId]
)
.then((result) => res.json(result.rows))
.catch((e) => console.error(e));
};
// const getFunction = (request, response) => {
// //reading file
// const text = fs.readFileSync("hello.json");
// const obj = JSON.parse(text);
// //return content
// return response.json(obj);
// };
const getHotels = async (req, res) => {
try {
const queryRows = await pool.query("SELECT * FROM hotels");
await res.json(queryRows.rows);
} catch (err) {
console.log(err);
}
};
const getHotelById = async (req, res) => {
const hotelId = req.params.hotelId;
pool
.query(
`select
*
from hotels h
where h.id =$1 `,
[hotelId]
)
.then((result) => res.json(result.rows))
.catch((e) => console.error(e));
};
const getCustomers = async (req, res) => {
try {
const queryRows = await pool.query(
`select
* from customers c
order by name`
);
await res.json(queryRows.rows);
} catch (err) {
console.log(err);
}
};
const getCustomerById = async (req, res) => {
const customerId = req.params.customerId;
pool
.query(
`select
*
from customers c
where c.id =$1 `,
[customerId]
)
.then((result) => res.json(result.rows))
.catch((e) => console.error(e));
};
// PATCH FUNCTION
const updateCustomer = async (req, res) => {
const customerId = req.params.customerId;
const newEmail = req.body.email;
const newAddress = req.body.address;
const newCity = req.body.city;
const newPostcode = req.body.postcode;
const newCountry = req.body.country;
if (newEmail.length <= 0) {
return res.status(400).send("The email field shouldn't be empty");
}
pool
.query(
"UPDATE customers SET email=$1 address=$2 city=$3 postcode=$4 country=$5 WHERE id=$6",
[newEmail, newAddress, newCity, newPostcode, newCountry, customerId]
)
.then(() => res.send(`Customer ${customerId} updated!`))
.catch((e) => console.error(e));
};
const deleteCustomer = async (req, res) => {
const customerId = req.params.customerId;
pool
.query("DELETE FROM bookings WHERE customer_id=$1", [customerId])
.then(() => {
pool
.query("DELETE FROM customers WHERE id=$1", [customerId])
.then(() => res.send(`Customer ${customerId} deleted!`))
.catch((e) => console.error(e));
})
.catch((e) => console.error(e));
};
const deleteHotel = async (req, res) => {
const hotelId = req.params.hotelId;
pool
.query("DELETE FROM bookings WHERE hotel_id=$1", [hotelId])
.then(() => {
pool
.query("DELETE FROM hotels WHERE id=$1", [hotelId])
.then(() => res.send(`Hotel ${hotelId} deleted!`))
.catch((e) => console.error(e));
})
.catch((e) => console.error(e));
};
// Finish and Ex 4
app.delete("/hotels/:hotelId", deleteHotel);
app.delete("/customers/:customerId", deleteCustomer);
app.patch("/customers/:customerId", updateCustomer);
app.get("/customers/:customerId/bookings", getCustomerBookings);
app.get("/customers/:customerId", getCustomerById);
app.get("/hotels", getHotels);
app.get("/hotels/:hotelId", getHotelById);
app.get("/customers", getCustomers);
app.get("/hello", getFunction);
app.get("/db", dbFunction);
app.get("/", (req, res) => {
res.json("Welcome to the Hotels application.");
});
app.listen(PORT, () => console.log(`listening to ${PORT}`));