This repository was archived by the owner on Aug 17, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathexercise.js
More file actions
216 lines (202 loc) · 6.72 KB
/
exercise.js
File metadata and controls
216 lines (202 loc) · 6.72 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
//routes,
// app.get("/hotels", function(req, res) {
// pool.query('SELECT * FROM hotels')
// .then((result) => res.json(result.rows))
// .catch((error) => {
// console.error(error);
// res.status(500).json(error);
// });
// });
// app.post("/hotels", function (req, res) {
// const newHotelName = req.body.name;
// const 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)";
// pool
// .query(query, [newHotelName, newHotelRooms, newHotelPostcode])
// .then(() => res.send("Hotel created!"))
// .catch((error) => {
// console.error(error);
// res.status(500).json(error);
// });
// }
// });
// });
// //name, email, address, city, postcode, country
// app.post("/customers", function (req, res) {
// const newCustomerName = req.body.name;
// 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;
// pool
// .query("SELECT * FROM customers WHERE name=$1", [newCustomerName])
// .then((result) => {
// if (result.rows.length > 0) {
// return res
// .status(400)
// .send("An Customer with the same name already exists!");
// } else {
// const query =
// "INSERT INTO customers (name, email, address, city, postcode, country) VALUES ($1, $2, $3)";
// pool
// .query(query, [newCustomerName, newAddress, newCity, newPostcode, newCountry])
// .then(() => res.send("Customer created!"))
// .catch((error) => {
// console.error(error);
// res.status(500).json(error);
// });
// }
// });
// });
// // search hotel by id
// app.get("/hotels/:hotelId", function (req, res) {
// const hotelId = req.params.hotelId;
// pool
// .query("SELECT * FROM hotels WHERE id=$1", [hotelId])
// .then((result) => res.json(result.rows))
// .catch((error) => {
// console.error(error);
// res.status(500).json(error);
// });
// });
// app.get("/customers", function (req, res) {
// const customerNameQuery = req.query.name;
// let query = `SELECT * FROM customers ORDER BY name`;
// let params = [];
// if (customerNameQuery) {
// query = `SELECT * FROM customers WHERE name LIKE $1 ORDER BY name`;
// params.push(`%${customerNameQuery}%`);
// }
// pool
// .query(query, params)
// .then((result) => res.json(result.rows))
// .catch((error) => {
// console.error(error);
// res.status(500).json(error);
// });
// });
// app.get("/customers/:customerId", function (req, res) {
// const customerId = req.params.customerId;
// pool
// .query("SELECT * FROM customers WHERE id=$1", [customerId])
// .then((result) => res.json(result.rows))
// .catch((error) => {
// console.error(error);
// res.status(500).json(error);
// });
// });
// app.get("/customers/:customerId/bookings", function (req, res) {
// const customerId = req.params.customerId;
// pool
// .query("SELECT * FROM customers c inner join bookings b on c.id = b.customer_id WHERE c.id=$1", [customerId])
// .then((result) => res.json(result.rows))
// .catch((error) => {
// console.error(error);
// res.status(500).json(error);
// });
// });
// // updating customers email
// app.put("/customers/:customerId", function (req, res) {
// const customerId = req.params.customerId;
// const newEmail = req.body.email;
// if(newEmail === "" || !newEmail.includes("@")){
// return res
// .status(400)
// .send("Please insert a valid email address");
// }
// pool
// .query("UPDATE customers SET email=$1 WHERE id=$2", [newEmail, customerId])
// .then(() => res.send(`Customer ${customerId} updated!`))
// .catch((error) => {
// console.error(error);
// res.status(500).json(error);
// });
// });
// app.listen(port, function() {
// console.log("Server is listening on port 3001. Ready to accept requests!");
// });
//DELETE
// app.delete("/customers/:customerId", function (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((error) => {
// console.error(error);
// res.status(500).json(error);
// });
// });
// //DELETE
// app.delete("/customers/:customerId", function (req, res) {
// const customerId = req.params.customerId;
// pool
// .query("DELETE FROM customers WHERE id=$1", [customerId])
// .then(() => res.send(`Customer ${customerId} deleted!`))
// .catch((error) => {
// console.error(error);
// res.status(500).json(error);
// });
// });
// GET QUERY NAME
// app.get("/products/", function(req, res) {
// const nameSearch = req.query.name;
// let query = `SELECT * FROM products ORDER BY name`;
// let params = [];
// if(nameSearch){
// query = 'SELECT * FROM products WHERE name LIKE $1 ORDER BY name';
// params.push(`%{nameSearch}%`)
// }
// pool.query(query, params)
// .then((result) => {
// res.status(200).send(result.rows)
// })
// .catch((error) =>{
// console.error(error)
// res.status(500).send(error)
// })
// });
//GET WITH QUERY NAME
// app.get("/hotels", function (req, res) {
// const hotelNameQuery = req.query.name;
// let query = `SELECT * FROM hotels ORDER BY name`;
// let params = [];
// if (hotelNameQuery) {
// query = `SELECT * FROM hotels WHERE name LIKE $1 ORDER BY name`;
// params.push(`%${hotelNameQuery}%`);
// }
// pool
// .query(query, params)
// .then((result) => res.json(result.rows))
// .catch((error) => {
// console.error(error);
// res.status(500).json(error);
// });
// });
//GET WITH QUERY
// app.get("/products", function (req, res) {
// const prodId = req.query.name;
// pool
// .query("SELECT * FROM hotels WHERE id=$1", [prodId])
// .then((result) => res.json(result.rows))
// .catch((error) => {
// console.error(error);
// res.status(500).json(error);
// });
// });