-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathburgers_controller.js
55 lines (50 loc) · 1.76 KB
/
burgers_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
// Dependencies
var express = require("express");
// Import the model to use its db functions for burger.js
var burger = require("../models/burger.js");
// Create the router for the app, and export the router at the end of your file.
var router = express.Router();
// Create routes and set up logic where required.
router.get("/", function (req, res) {
burger.selectAll(function(data) {
var hbsObject = {
burgers: data
};
console.log(hbsObject);
res.render("index", hbsObject);
});
});
// Add new burger to the db.
router.post("/api/burgers", function (req, res) {
burger.insertOne(["burger_name", "devoured"], [req.body.burger_name, req.body.devoured], function(result) {
// Send back the ID of the new burger
res.json({ id: result.insertId });
});
});
// Set burger devoured status to true.
router.put("/api/burgers/:id", function(req, res) {
var condition = "id = " + req.params.id;
console.log("condition", condition);
burger.updateOne({ devoured: req.body.devoured }, condition, function(result) {
if (result.changedRows === 0) {
// If no rows were changed, then the ID must not exist, so 404.
return res.status(404).end();
} else {
res.status(200).end();
}
});
});
// Delete burger from db.
router.delete("/api/burgers/:id", function(req, res) {
var condition = "id = " + req.params.id;
console.log("condition", condition);
burger.deleteOne(condition, function(result) {
if (result.changedRows === 0) {
// If no rows were changed, then the ID must not exist, so 404.
return res.status(404).end();
} else {
res.status(200).end();
}
});
});
module.exports = router;