-
Notifications
You must be signed in to change notification settings - Fork 0
/
events1.js
58 lines (50 loc) · 1.64 KB
/
events1.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
const express = require("express");
const fetchuser = require("./middleware/fetchuser");
const router = express.Router();
const client = require("./db");
router.post("/enrolled", fetchuser, async (req, res) => {
try {
const username = req.user_name;
const { event_name } = req.body;
console.log(event_name);
const data = await client.query(
`select *from users where username = '${username}' ;`
);
// console.log(data);
const enrolled_events = data.rows[0].events;
if (enrolled_events.includes(event_name)) {
return res
.status(200)
.json({ message: "you are already registered", value: -2 });
}
let data1 = await client.query(
`update users set events = events || '{${event_name}}' where username = '${username}' ;`
);
res.status(200).json({ value: 0 });
} catch (error) {
console.log(error);
res.status(200).json({ message: "can't enrolled", value: -1 });
}
});
router.post("/revoke", fetchuser, async (req, res) => {
try {
const username = req.user_name;
const { event_name } = req.body;
console.log(event_name);
const data = await client.query(
`select *from users where username = '${username}' ;`
);
// console.log(data);
const enrolled_events = data.events;
let data1;
data1 = await client.query(
`update users set events = array_remove( events , '${event_name}') where username = '${username}' ;`
);
res.status(200).json({ value: 0 });
} catch (error) {
console.log(error);
res.status(200).json({ message: "can't enrolled", value: -1 });
}
});
module.exports = client;
module.exports = router;