-
Notifications
You must be signed in to change notification settings - Fork 0
/
members.js
84 lines (76 loc) · 2.33 KB
/
members.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
const express = require("express");
const { Deta } = require("deta");
const router = express.Router();
const { checkAuth } = require("../middleware/checkAuth");
// deta
const deta = Deta(process.env.DB_DATA_KEY);
const membersDB = deta.Base("members");
// info about this route
router.get("/info", (req, res) => {
res.status(200).send(
"This route is reserved for everything related to team members."
);
});
// get all objects in DB
router.get("/", async (req, res) => {
try {
const members = await membersDB.fetch();
res.status(200).json(members);
} catch (error) {
res.status(503).json({ error: "Database Error" });
}
});
// get single object by key from DB
router.get("/:key", async (req, res) => {
try {
const key = req.params.key;
const value = await membersDB.get(key);
if (value === null) {
res.status(404).json({ error: "No value found at " + key });
}
res.status(200).json(value);
} catch (error) {
res.status(503).json({ error: "Database Error" });
}
});
// insert new object into DB
router.post("/", checkAuth, async (req, res) => {
try {
const { name, funktionIG, teamrolle, comment, imgPath } = req.body;
const toCreate = { name, funktionIG, teamrolle, comment, imgPath };
const toInsert = await membersDB.insert(toCreate);
res.status(201).json(toInsert);
} catch (error) {
res.status(400).json({ error: "Bad Request" });
}
});
// alter exisiting object in DB
router.put("/", checkAuth, async (req, res) => {
try {
const { key, name, funktionIG, teamrolle, comment, imgPath } = req.body;
const toAlter = await membersDB.update(
{
name,
funktionIG,
teamrolle,
comment,
imgPath,
},
key
);
res.status(200).json(toAlter);
} catch (error) {
res.status(503).json({ error: "Database Error" });
}
});
// delete object in DB by string
router.delete("/", checkAuth, async (req, res) => {
try {
const key = req.body.key;
const toDelete = await membersDB.delete(key);
res.status(200).json(toDelete);
} catch {
res.status(503).json({ error: "Database Error" });
}
});
module.exports = router;