-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
36 lines (31 loc) · 843 Bytes
/
api.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
const express = require("express");
const db = require("./db");
const router = express.Router();
router.get("/api/entries", (req, res) => {
db.getEntries()
.then(entries => {
// only return the last 10 entries
res.json(entries.slice(Math.max(entries.length - 10, 0)));
})
.catch(() => {
res.sendStatus(500);
});
});
router.get("/api/allowed", (req, res) => {
db.shouldOpenDoor()
.then(allow => {
res.sendStatus(allow ? 200 : 404);
})
.catch(() => res.sendStatus(500));
});
router.post("/api/allow", (req, res) => {
db.allowDoor()
.then(() => res.sendStatus(200))
.catch(() => res.sendStatus(500));
});
router.post("/api/disallow", (req, res) => {
db.disallowDoor()
.then(() => res.sendStatus(200))
.catch(() => res.sendStatus(500));
});
module.exports = router;