-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmeally_controller.js
68 lines (55 loc) · 1.71 KB
/
meally_controller.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
//setting up the express and router functionalities
const express = require("express");
const router = express.Router();
//imports the meally model
let db = require("../models");
//router functions and logic
router.get("/", function (req, res) {
//the selectAll matches the code in the ORM file
db.meally.selectAll(function (data) {
let hbsObject = {
orders: data
};
console.log(hbsObject);
//the handlebars partials file where we will render the datase results
res.render("orders", hbsObject);
});
});
//this will write the new order to the database
router.post("/api/orders", function (req, res) {
db.exportsmeally.createOne([
"new_order"
], [
req.body.name
], function (result) {
res.json({ id: result.insertId });
});
});
//this is to update the order
router.put("/api/orders/:id", function (req, res) {
let condition = "id = " + req.params.id;
console.log("condition", condition);
//this is to change the status of the burger from not eaten to eaten
//this is where it pulls from the boolean variable in SQL
db.meally.updateOne({
delivered: req.body.delivered
}, condition, function (result) {
if (result.changedRows == 0) {
return res.status(404).end();
} else {
res.status(200).end();
}
});
});
router.delete("/api/orders/:id", function (req, res) {
let condition = "id = " + req.params.id;
db.meally.deleteOne(condition, function (result) {
if (result.affectedRows == 0) {
return res.status(404).end();
} else {
res.status(200).end();
}
});
});
//exporting for use
module.exports = router;