-
Notifications
You must be signed in to change notification settings - Fork 9
/
collections.js
102 lines (92 loc) · 2.54 KB
/
collections.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const express = require("express");
const router = express.Router();
const {
createCollection,
getCollections,
updateCollection,
deleteCollection,
createCollectionEntry,
deleteCollectionEntry,
NotFoundException,
} = require("./crud");
const { settings } = require("./settings");
// Base URL: /api/v1/collections
router.use(function checkUseLocalDB(req, res, next) {
if (!settings.uselocaldb) {
return res.status(400).json({
error: "mpvremote-uselocaldb disabled!",
});
} else {
next();
}
});
router.get("/:id?", async (req, res) => {
try {
if (req.params.id) {
const collection = await getCollections(req.params.id);
if (!collection)
return res.status(404).json({ message: "Collection not exists" });
return res.json(collection);
} else {
return res.json(await getCollections());
}
} catch (exc) {
console.log(exc);
return res.status(500).json({ message: exc });
}
});
router.post("", async (req, res) => {
// TODO Some validation.
try {
const collection = await createCollection(req.body);
return res.json(collection);
} catch (exc) {
console.log(exc);
return res.status(500).json({ message: exc });
}
});
router.patch("/:collection_id/", async (req, res) => {
try {
return res.json(await updateCollection(req.params.collection_id, req.body));
} catch (exc) {
if (exc instanceof NotFoundException)
return res.status(404).json({ message: exc.message });
else {
console.log(exc);
return res.status(500).json({ message: exc });
}
}
});
router.delete("/:collection_id/", async (req, res) => {
try {
const collection_id = req.params.collection_id;
deleteCollection(collection_id);
return res.json({});
} catch (exc) {
return res.status(500).json({ message: exc });
}
});
router.post("/:collection_id/entries/", async (req, res) => {
try {
const collection_entry = await createCollectionEntry(
req.params.collection_id,
req.body
);
return res.json(collection_entry);
} catch (exc) {
if (exc instanceof NotFoundException)
return res.status(404).json({ message: exc.message });
else return res.status(500).json({ message: exc });
}
});
router.delete("/entries/:id", async (req, res) => {
try {
deleteCollectionEntry(req.params.id);
return res.json({});
} catch (exc) {
if (exc instanceof NotFoundException)
return res.status(404).json({ message: exc.message });
else return res.status(500).json({ message: exc });
}
});
module.exports = router;