-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfileController.js
More file actions
73 lines (63 loc) · 2.11 KB
/
fileController.js
File metadata and controls
73 lines (63 loc) · 2.11 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
const db = require('./models/closetModels')
const fileController = {};
fileController.getClothes = async (req, res, next) => {
const queryStr = 'SELECT * FROM Closet ORDER BY _id ASC';
try {
const data = await db.query(queryStr);
res.locals.clothes = data.rows;
return next();
} catch (error) {
return next(error);
}
};
fileController.newClothingItem = async (req, res, next) => {
const { itemName, itemClothingType, itemColor, itemImage } = req.body
const queryStr = 'INSERT INTO Closet (itemName, itemClothingType, itemColor, itemImage) VALUES ($1, $2, $3, $4)';
const queryParams = [itemName, itemClothingType, itemColor, itemImage];
try {
await db.query(queryStr, queryParams);
return next();
} catch (error) {
return next({
log: `Database error`,
status: 502,
message: { err: `${error.stack}` },
});
}
};
fileController.updateClosetItem = async (req, res, next) => {
const id = req.body._id;
const firstQuery = 'SELECT times_worn FROM Closet WHERE _id = $1';
try {
const data = await db.query(firstQuery, [id]);
let { times_worn } = data.rows[0];
times_worn += 1;
const todaysDate = new Date();
const queryStr = 'UPDATE Closet SET last_worn = $1, times_worn = $2 WHERE _id= $3';
const queryParams = [todaysDate, times_worn, id];
await db.query(queryStr, queryParams);
return next();
} catch (error) {
return next({
log: `Database error`,
status: 502,
message: { err: `${error.stack}` },
});
}
}
fileController.deleteClothingItem = async (req, res, next) => {
const id = req.body._id
const queryStr = 'DELETE FROM Closet WHERE _id = $1';
const queryParams = [id];
try {
await db.query(queryStr, queryParams);
return next();
} catch (error) {
return next({
log: `Database error`,
status: 502,
message: { err: `${error.stack}` },
});
}
}
module.exports = fileController;