-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
237 lines (208 loc) · 5.89 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
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
232
233
234
235
236
237
const express = require("express");
const app = express();
const cors = require("cors");
const path = require("path");
const bodyParser = require("body-parser");
const port = 8080;
const sqlite3 = require("sqlite3").verbose();
const logIfError = (err) => {
if (err) {
console.error(err.message);
return;
}
};
const db = new sqlite3.Database(
"./shopListDB.db",
sqlite3.OPEN_READWRITE,
(err) => {
logIfError(err);
console.log("connection with database successful");
}
);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(
cors({
origin: "*",
credentials: true,
})
);
app.use(express.static(path.join(__dirname, "build")));
app.get("/", (req, res) => {
res.send("HOMEPAGE");
});
app.get("/createlist", (req, res) => {
db.all("SELECT * FROM PRODUCTS ORDER BY name", [], (err, products) => {
logIfError(err);
products.forEach((prod) => {
console.log(prod);
});
res.send(products);
});
});
app.get("/showlists", (req, res) => {
db.all(
"SELECT ID, Date FROM SHOPPING_LISTS WHERE Status = 0",
[],
(err, dates) => {
logIfError(err);
dates.forEach((date) => {
console.log(date);
});
res.send(dates);
}
);
});
app.get("/showlists/get-list", (req, res) => {
console.log("getting: ", req.query.date);
db.all(
"SELECT SHOPPING_LISTS.ID, PRODUCTS.name, PRODUCTS.department, RELATIONAL.Amount, PRODUCTS.units, RELATIONAL.Comment \
FROM PRODUCTS \
INNER JOIN RELATIONAL ON PRODUCTS.ID = RELATIONAL.ProductID \
INNER JOIN SHOPPING_LISTS ON SHOPPING_LISTS.ID = RELATIONAL.ListID WHERE Status = 0 AND Date = ? ",
[req.query.date],
(err, list) => {
logIfError(err);
console.log("list:", list);
res.send(list);
}
);
});
app.get("/additem", (req, res) => {
res.send("additem");
});
app.post("/additem", (req, res) => {
console.log(req.body.product);
db.get(
"SELECT name FROM PRODUCTS WHERE name = ? COLLATE NOCASE",
[req.body.product],
(err, check) => {
console.log("check:", check);
if (err) res.send({ status: "wrong" });
else if (check === undefined) {
console.log("NO PRODUCT EXISTS!");
db.run(
"INSERT INTO PRODUCTS(name, department, units) VALUES (?, ?, ?)",
[req.body.product, req.body.department, req.body.units],
(err) => {
if (err) res.send({ status: "wrong" });
else res.send({ status: "inserted" });
}
);
} else if (req.body.product.toUpperCase() === check.name.toUpperCase()) {
console.log("PRODUCT EXISTS!");
res.send({ status: "exists" });
}
}
);
});
app.get("/closelist", (req, res) => {
res.send("CLOSE LIST");
});
app.post("/closelist", (req, res) => {
db.get("SELECT 1 FROM SHOPPING_LISTS WHERE Status = 1", (err, check) => {
logIfError(err);
if (check === undefined) {
console.log("NO LIST EXISTS");
db.run("INSERT INTO SHOPPING_LISTS(Date, Status) VALUES (DATE(), 1)"),
(err) => {
logIfError(err);
res.send(req.body.map((product) => product.id));
};
db.get("SELECT ID FROM SHOPPING_LISTS WHERE Status = 1", (err, id) => {
logIfError(err);
console.log("ID is:", id);
req.body.forEach((product) => {
db.run(
"INSERT INTO RELATIONAL(ListID, ProductID, Amount, Comment) VALUES(?, ?, ?, ?)",
[id.ID, product.id, product.amount, product.comment]
),
(err) => {
logIfError(err);
};
});
});
} else {
console.log("LIST EXISTS!");
db.get("SELECT ID FROM SHOPPING_LISTS WHERE Status = 1", (err, id) => {
logIfError(err);
console.log("ID is:", id);
req.body.map((product) => {
db.run(
"INSERT INTO RELATIONAL(ListID, ProductID, Amount, Comment) VALUES(?, ?, ?, ?)",
[id.ID, product.id, product.amount, product.comment]
),
(err) => {
logIfError(err);
};
});
});
}
});
res.send("DONE!");
});
app.get("/shoppinglist", (req, res) => {
db.all(
"SELECT PRODUCTS.ID, PRODUCTS.name, PRODUCTS.department, RELATIONAL.Amount, PRODUCTS.units, RELATIONAL.Comment \
FROM PRODUCTS \
INNER JOIN RELATIONAL ON PRODUCTS.ID = RELATIONAL.ProductID \
INNER JOIN SHOPPING_LISTS ON SHOPPING_LISTS.ID = RELATIONAL.ListID WHERE Status = 1",
[],
(err, list) => {
logIfError(err);
list.forEach((prod) => {
console.log(prod);
});
res.send(list);
}
);
});
app.post("/finishshopping", (req, res) => {
db.run("UPDATE SHOPPING_LISTS SET Status = 0 WHERE Status = 1"),
(err) => {
logIfError(err);
res.send("Status updated to 0!");
};
});
app.listen(port, () => {
console.log(`App listening on port ${port}`);
db.run(
"CREATE TABLE IF NOT EXISTS PRODUCTS ( \
ID INTEGER NOT NULL UNIQUE, \
name TEXT NOT NULL, \
department TEXT NOT NULL, \
units TEXT NOT NULL, \
PRIMARY KEY(ID AUTOINCREMENT) \
)"
),
(err) => {
logIfError(err);
console.log("Table PRODUCTS Created!");
};
db.run(
"CREATE TABLE IF NOT EXISTS SHOPPING_LISTS ( \
ID INTEGER NOT NULL UNIQUE, \
Date TEXT NOT NULL, \
Status INTEGER NOT NULL, \
PRIMARY KEY(ID AUTOINCREMENT) \
)"
),
(err) => {
logIfError(err);
console.log("Table SHOPPING_LISTS Created!");
};
db.run(
"CREATE TABLE IF NOT EXISTS RELATIONAL ( \
ListID INTEGER NOT NULL, \
ProductID INTEGER NOT NULL, \
Amount INTEGER NOT NULL, \
Comment TEXT NOT NULL, \
FOREIGN KEY(ProductID) REFERENCES PRODUCTS(ID), \
FOREIGN KEY(ListID) REFERENCES SHOPPING_LISTS(ID) \
)"
),
(err) => {
logIfError(err);
console.log("Table RELATIONAL Created!");
};
});