-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
211 lines (169 loc) · 6.22 KB
/
app.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
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
// IMPORTS ---------------------------------------------------------------------
// ejs is required in set EJS section implicitly
const path = require("path");
const express = require("express");
const { PomoRepository } = require("./pomoRepository");
// GLOBAL CONSTANTS, SET UP ----------------------------------------------------
const port = 3000;
// instantiate PomoRepository
const pomoRepository = new PomoRepository();
// start server
const app = express();
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
// set EJS
app.set("view engine", "ejs"); //it will do require("ejs");
app.set("views", path.join(__dirname, "views"));
// ROUTING ---------------------------------------------------------------------
// -- General --
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, "static/")));
// -- Pages --
app.get("/insert", (req, res, next) => {
res.render("insertOrEditLast.ejs", { editLast: false });
});
app.get("/submitted", (req, res, next) => {
res.render("submitted.ejs");
});
app.get("/editLast", (req, res, next) => {
res.render("insertOrEditLast.ejs", { editLast: true });
});
app.get("/control", (req, res, next) => {
res.render("control.ejs");
});
// -- Api --
// -- Routes for "GET" --
// -- Categories --
app.get("/api/categories", async (req, res, next) => {
// params: req.query.groups
const groups = parseIdArrayFromQueryString(req.query.groups);
res.send({
categories: await pomoRepository.getCategories(groups)
});
});
app.get("/api/categories/:id", async (req, res, next) => {
// params: req.params.id
res.send({
category: await pomoRepository.getCategory(parseInt(req.params.id))
});
});
// -- Groups --
app.get("/api/groups", async (req, res, next) => {
// no params
res.send({
groups: await pomoRepository.getGroups()
});
});
app.get("/api/groups/:id", async (req, res, next) => {
// params: req.params.id
res.send({
group: await pomoRepository.getGroup(parseInt(req.params.id))
});
});
// -- Pomos --
app.get("/api/pomos", async (req, res, next) => {
// params: req.query.categories, req.query.dateFrom, req.query.dateTo, req.query.lastAmount
const categories = parseIdArrayFromQueryString(req.query.categories);
// dateFrom and dateTo already work as strings
// parse to int
let lastAmount = undefined;
if (req.query.lastAmount != undefined) {
lastAmount = parseInt(req.query.lastAmount);
}
res.send({
pomos: await pomoRepository.getPomos(categories, req.query.dateFrom, req.query.dateTo, lastAmount)
});
});
app.get("/api/pomos/:id", async (req, res, next) => {
// params: req.params.id
res.send({
pomo: await pomoRepository.getPomo(parseInt(req.params.id))
});
});
// -- Aggregations --
app.get("/api/aggregations/pomos-quantities", async (req, res, next) => {
// params: req.query.categories, req.query.dateFrom, req.query.dateTo
const categories = parseIdArrayFromQueryString(req.query.categories);
// dateFrom and dateTo already work as strings
res.send({
pomosQuantities: await pomoRepository.getPomosQuantities(categories, req.query.dateFrom, req.query.dateTo)
});
});
app.get("/api/aggregations/pomos-averages", async (req, res, next) => {
// params: req.query.categories, req.query.dateFrom, req.query.dateTo
const categories = parseIdArrayFromQueryString(req.query.categories);
// dateFrom and dateTo already work as strings
res.send({
pomosAverages: await pomoRepository.getPomosAverages(categories, req.query.dateFrom, req.query.dateTo)
});
});
// -- Routes for "POST" --
app.post("/api/categories", async (req, res, next) => {
// params: req.body.name
res.send({
insertedCategory: await pomoRepository.insertCategory(req.body.name)
});
});
app.post("/api/groups", async (req, res, next) => {
// params: req.body.name, req.body.categories
const categories = parseIdArrayFromQueryString(req.body.categories);
res.send({
insertedGroup: await pomoRepository.insertGroup(req.body.name, categories)
});
});
app.post("/api/pomos", async (req, res, next) => {
// params: req.body.datetime, req.body.catId
res.send({
insertedPomo: await pomoRepository.insertPomo(req.body.datetime, req.body.catId)
});
});
// -- Routes for "PATCH" --
app.patch("/api/categories/:id", async (req, res, next) => {
// params: req.params.id, req.body.newName
res.send({
updatedCategory: await pomoRepository.updateCategory(parseInt(req.params.id), req.body.newName)
});
});
app.patch("/api/groups/:id", async (req, res, next) => {
// params: req.params.id, req.body.newName, req.body.newCategories
const newCategories = parseIdArrayFromQueryString(req.body.newCategories);
res.send({
updatedGroup: await pomoRepository.updateGroup(parseInt(req.params.id), req.body.newName, newCategories)
});
});
app.patch("/api/pomos/:id", async (req, res, next) => {
// params: req.params.id, req.body.newDatetime, req.body.newCatId
res.send({
updatedPomo: await pomoRepository.updatePomo(parseInt(req.params.id), req.body.newDatetime, req.body.newCatId)
});
});
// -- Routes for "DELETE" --
app.delete("/api/categories/:id", async (req, res, next) => {
// params: req.params.id
res.send({
deletedCategory: await pomoRepository.deleteCategory(parseInt(req.params.id))
});
});
app.delete("/api/groups/:id", async (req, res, next) => {
// params: req.params.id
res.send({
deletedGroup: await pomoRepository.deleteGroup(parseInt(req.params.id))
});
});
app.delete("/api/pomos/:id", async (req, res, next) => {
// params: req.params.id
res.send({
deletedPomo: await pomoRepository.deletePomo(parseInt(req.params.id))
});
});
// AUX METHODS -----------------------------------------------------------------
function parseIdArrayFromQueryString(queryArray) {
// useful for either groups and categories
// parse to a list of ints
let idArray = undefined;
if (queryArray != undefined) {
idArray = queryArray.split(",").map(str => parseInt(str));
}
return idArray;
}