-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
54 lines (47 loc) · 1.52 KB
/
routes.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
const express = require('express');
const router = express.Router();
const Item = require('./models/Item');
const User = require('./models/User');
router.post('/toggle-reaction', (req, res, next) => {
const {itemId, ownerId, reactionType} = req.body;
User.findOne({_id: ownerId})
.then(owner => {
return Item.findOne({ _id: itemId })
.then(item => {
return item.toggleReaction(reactionType, owner)
})
.then(i => {
res.send(i);
});
})
.catch(e => next(e));
});
router.post('/add-reaction', (req, res, next) => {
const {itemId, ownerId, reactionType} = req.body;
User.findOne({_id: ownerId})
.then(owner => {
return Item.findOne({ _id: itemId })
.then(item => {
return item.addReaction(reactionType, owner)
})
.then(i => {
res.send(i);
});
})
.catch(e => next(e));
});
router.post('/remove-reaction', (req, res, next) => {
const {itemId, ownerId, reactionType} = req.body;
User.findOne({_id: ownerId})
.then(owner => {
return Item.findOne({ _id: itemId })
.then(item => {
return item.removeReaction(reactionType, owner)
})
.then(i => {
res.send(i);
});
})
.catch(e => next(e));
});
module.exports = router;