-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
123 lines (120 loc) · 3.52 KB
/
index.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
const express = require("express");
const app = express();
const port = 3000;
const bodyParser = require("body-parser");
const cors = require("cors");
// establishing the connection
const Apartment = require("./models/Apartment");
const {
getAllApartments,
createNewApartment,
getApartmentById,
deleteApartmentById,
updateApartmentById,
} = require("./controllers/queries");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// now we can serve static files to the client
app.use(express.static("public"));
app.use(cors());
// syncing our model with our database and catching all errors
Apartment.sync()
.then((result) => {
//console.log(result);
app.listen(port, () => {
console.log(`Server started on port ${port}`);
});
})
.catch((err) => {
console.log(err);
});
// all of our routes and corresponding http-requests
// ----- ----- ----- ----- ----- ----- ----- -----
// ROUTE: /apartments
app
.route("/apartments")
.get((req, res) => {
// price: asc/desc; rooms= any integer number;
const { price, rooms: rooms_param } = req.query;
// generating our query options depending on what query-parameters we have
const options = {};
if (Object.keys(req.query).length === 2) {
options["where"] = { rooms: parseInt(rooms_param) };
options["order"] = [["price", price.toUpperCase()]];
} else if (price) {
options["order"] = [["price", price.toUpperCase()]];
} else if (rooms_param) {
options["where"] = { rooms: parseInt(rooms_param) };
}
getAllApartments(options)
.then((data) => {
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(data, null, 4));
})
.catch((err) => {
console.log(err);
res
.status(400)
.json({ info: "Error while retrieving list of all apartments" });
});
})
.post((req, res) => {
const body_params = req.body;
createNewApartment(body_params)
.then((data) => {
res
.status(200)
.json({ info: "New apartment has been added succesfully" });
})
.catch((err) => {
console.log(err);
res.status(400).json({ info: "Data you have provided is invalid" });
});
});
// ----- ----- ----- ----- ----- ----- ----- -----
// ROUTE: /apartments/:id
app
.route("/apartments/:id")
.get((req, res) => {
const id = req.params.id;
getApartmentById(id)
.then((data) => {
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(data, null, 4));
})
.catch((err) => {
console.log(err);
res.status(400).json({
info: "Error occured while retrieving data about this apartment",
});
});
})
.delete((req, res) => {
const id = req.params.id;
deleteApartmentById(id)
.then((data) => {
res
.status(200)
.json({ info: "Apartment has been deleted succesfully" });
})
.catch((err) => {
console.log(err);
res
.status(400)
.json({ info: "Error occured while deleting this apartment" });
});
})
.put((req, res) => {
const id = req.params.id;
const body_params = req.body;
updateApartmentById(id, body_params)
.then((data) => {
res
.status(200)
.json({ info: "Apartment info has been updated succesfully" });
})
.catch((err) => {
console.log(err);
res.status(400).json({ info: "Data you have provided is invalid" });
});
});