-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
148 lines (124 loc) · 2.84 KB
/
Copy pathapi.js
File metadata and controls
148 lines (124 loc) · 2.84 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import express from "express";
import bodyParser from "body-parser";
const app = express();
const port = 4000;
//Events API
// In-memory data store
let events = [
{
id: 1,
name: "Battle Of Bands",
venue:
"Old Mess Lawn",
time: "9:00 - 12:00 AM, 15 Feb",
},
{
id: 2,
name: "Group Dance",
venue:
"Auditoriam",
time: "1:00 - 3:00 PM, 15 Feb",
},
{
id: 3,
name: "Prom Night",
venue:
"Old Mess Lawn",
time: "8:00 - 11:00 PM, 15 Feb",
},
{
id: 4,
name: "Sugar Rocketry",
venue:
"Football Ground",
time: "9:00 - 11:00 AM, 16 Feb",
},
{
id: 5,
name: "Freestyle",
venue:
"Auditorium",
time: "12:00 - 2:00 PM, 16 Feb",
},
];
let lastEventId = 5;
// Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//GET All events
app.get("/events", (req, res) => {
console.log(events);
res.json(events);
});
//POST a new event
app.post("/addevent", (req, res) => {
const newId = lastEventId += 1;
const event = {
id: newId,
name: req.body.name,
venue: req.body.venue,
time: req.body.time,
};
lastEventId = newId;
events.push(event);
res.status(201).json(event);
});
app.delete("/deleteevent", (req, res) => {
const id = parseInt(req.body.id);
const index = events.findIndex((p) => p.id === id);
if (index === -1) return res.status(404).json({ message: "Event not found" });
events.splice(index, 1);
res.json({ message: "Event deleted" });
});
//Notification API
// In-memory data store
let notifications = [
{
id: 1,
content: "Battle Of Bands is starting in 5 min",
sendTime: "8:55 AM 15 Feb 2024",
},
{
id: 2,
content: "Group Dance will start at 1:30 PM",
sendTime: "10:56 AM 15 Feb 2024",
},
{
id: 3,
content: "Hurry up guys prom night is starting in 10 min",
sendTime: "8:07 PM 15 Feb 2024",
},
{
id: 4,
content: "Venue for freestyle is changed to hostel circle",
sendTime: "11:02 PM 15 Feb 2024",
},
];
let lastNotifyId = 4;
//GET All events
app.get("/notifications", (req, res) => {
console.log(events);
res.json(notifications);
});
//POST a new notification
app.post("/sendnotification", (req, res) => {
const newId = lastNotifyId += 1;
const noti = {
id: newId,
content: req.body.content,
sendTime: req.body.sendTime,
};
lastEventId = newId;
notifications.push(noti);
res.status(201).json(noti);
});
app.delete("/deletenotification", (req, res) => {
const id = parseInt(req.body.id);
const index = notifications.findIndex((p) => p.id === id);
if (index === -1) return res.status(404).json({ message: "event not found" });
notifications.splice(index, 1);
res.json({ message: "event deleted" });
});
app.listen(port, () => {
console.log(`API is running at http://localhost:${port}`);
});