-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdminUsersEndpoint.js
93 lines (83 loc) · 2.57 KB
/
AdminUsersEndpoint.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
const Express = require("express");
const router = Express.Router();
const _ = require("lodash");
const AuthenticateUser = require("../middleware/AuthenticateUser");
const CheckAdminUser = require("../middleware/AuthAdminUser");
const { Events } = require("../models/Events");
router.get(
"/",
[AuthenticateUser, CheckAdminUser],
async (request, response) => {
//get request is made return the list of all the events happening
const AllEvents = await Events.find({});
var SelectedAttributedForCardView = [];
AllEvents.forEach((event) => {
SelectedAttributedForCardView.push(
_.pick(event, ["_id", "ImageUrl", "Title", "Category", "Date"])
);
});
return response.status(200).send(SelectedAttributedForCardView);
}
);
//expanded event details view admin side
router.get(
"/:SelectedEventId",
[AuthenticateUser, CheckAdminUser],
async (request, response) => {
const SelectedEvent = await Events.find({
_id: request.params.SelectedEventId,
});
if (!SelectedEvent) {
return response.status(400).send("Bad Request..!");
}
response.status(200).send(SelectedEvent);
}
);
//admins are allowed to make post requests then new events can be added
router.post(
"/",
[AuthenticateUser, CheckAdminUser],
async (request, response) => {
const event = new Events({
ImageUrl: request.body.ImageUrl,
Title: request.body.Title,
Description: request.body.Description,
Category: request.body.OrganizingClub,
location: request.body.Venue,
RegistrationLink: request.body.RegistrationLink,
Prerequisites: request.body.Prerequisites,
});
const StatusSave = await event.save();
//debugging purposes
//console.log(event);
response.status(200).send("Event Successfully Created...!");
}
);
// Delete requests
router.delete(
"/:DeleteEventId",
[AuthenticateUser, CheckAdminUser],
async (request, response) => {
//delete the event with the id provided in the params
const DeleteEvent = await Events.findByIdAndRemove(
request.params.DeleteEventId
);
if (!DeleteEvent)
return response.status(400).send("Sorry..! .Event Not found...!");
response.status(200).send(DeleteEvent);
}
);
router.put(
"/:id",
[AuthenticateUser, CheckAdminUser],
async (request, response) => {
const UpdateEvent = await Events.findByIdAndUpdate(
request.params.id,
request.body
);
if (!UpdateEvent)
return response.status(400).send("Sorry..! .Event Not found...!");
response.status(200).send(UpdateEvent);
}
);
module.exports = router;