-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlinksController.js
More file actions
93 lines (83 loc) · 2.57 KB
/
linksController.js
File metadata and controls
93 lines (83 loc) · 2.57 KB
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
const admissionLink = require('../../models/admissions/importantLink');
const { sendError, validateID } = require('../../utils');
const createLink = (req, res) => {
const link = new admissionLink(req.body);
link.save()
.then((createdLink) => res.status(201).json(createdLink))
.catch((err) => sendError(res, err));
};
const getLinks = (req, res) => {
let filter = {};
if (req.query.visible === 'visible') {
filter.visible = true;
} else if (req.query.visible === 'hidden') {
filter.visible = false;
}
admissionLink
.find(filter)
.sort({ updatedAt: -1 })
.then((links) => res.json(links))
.catch((err) => sendError(res, err));
};
const getLinkById = (req, res) => {
const id = req.query.id;
validateID(id)
.then(() => {
admissionLink
.findById(id)
.then((link) => res.json(link))
.catch((err) => sendError(res, err));
})
.catch((err) => sendError(res, err));
};
const editLink = (req, res) => {
const id = req.query.id;
validateID(id)
.then(() => {
req.body.updatedAt = Date.now();
admissionLink
.findByIdAndUpdate(id, req.body, {
new: true,
runValidators: true,
})
.then((updatedLink) => res.json(updatedLink))
.catch((err) => sendError(res, err));
})
.catch((err) => sendError(res, err));
};
const editMetaData = (req, res) => {
const id = req.query.id;
validateID(id)
.then(() => {
admissionLink
.findById(id)
.then((link) => {
link.visible = !link.visible;
link.disabledAt = !link.visible ? Date.now() : null;
link.save()
.then((updatedLink) => res.json(updatedLink))
.catch((err) => sendError(res, err));
})
.catch((err) => sendError(res, err));
})
.catch((err) => sendError(res, err));
};
const deleteLink = (req, res) => {
const id = req.query.id;
validateID(id)
.then(() => {
admissionLink
.findByIdAndDelete(id)
.then((deletedLink) => res.json(deletedLink))
.catch((err) => sendError(res, err));
})
.catch((err) => sendError(res, err));
};
module.exports = {
createLink,
getLinks,
getLinkById,
editLink,
toggleLinkVisiblity,
deleteLink,
};