-
Notifications
You must be signed in to change notification settings - Fork 2
/
cards.js
126 lines (114 loc) · 3.51 KB
/
cards.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
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
const express = require('express');
const router = express.Router();
//middleware
const authorization = require('../middleware/authorization');
//models
const cardsModel = require('../../db/models/cards');
const usersModel = require('../../db/models/users');
//utils
const tryCatch = require('../utils/tryCatch');
//get card info by cardID
router.get('/:id', authorization.userOwnsCard, (req, res) => {
const cardId = req.params.id;
tryCatch(async () => {
const card = await cardsModel.getCardByID(cardId);
res.status(200).send(card);
}, res);
});
//create new card
router.post('/', (req, res) => {
const card = req.body;
//@TODO: change this line
tryCatch(async () => {
let newCard = await cardsModel.createNewCard(card);
let newPopulatedCard = await cardsModel.getCardByID(newCard.id);
res.status(200).send(newPopulatedCard);
}, res);
});
//update card
router.put('/:id', authorization.userOwnsCard, async (req, res) => {
const card = req.body;
card.id = req.params.id;
tryCatch(async () => {
await cardsModel.updateCard(card);
const updatedCard = await cardsModel.getCardByID(card.id);
res.status(200).send(updatedCard);
}, res);
});
//delete card by cardID
router.delete('/:id', authorization.userOwnsCard, (req, res) => {
const cardId = req.params.id;
tryCatch(async () => {
await cardsModel.delete(cardId);
res.status(200).send({ message: 'Card Deleted' });
}, res);
});
router.post('/:cardId/member/:userId', async (req, res) => {
tryCatch(async () => {
const cardId = req.params.cardId;
const userId = req.params.userId;
const user = await usersModel.getUserByID(userId);
if (!user) {
res.status(404).json({ error: 'not found' });
return;
} else {
await cardsModel.addUserToCard(cardId, userId);
const updatedCard = await cardsModel.getCardByID(cardId);
res.status(200).send(updatedCard);
}
}, res);
});
router.delete(
'/:id/member/:userId',
authorization.userOwnsCard,
async (req, res) => {
tryCatch(async () => {
const cardId = req.params.id;
const userId = req.params.userId;
let result = await cardsModel.removeUserFromCard(cardId, userId);
if (result) {
const updatedCard = await cardsModel.getCardByID(cardId);
res.status(200).send(updatedCard);
} else {
res.status(404).json({ error: 'not found' });
}
}, res);
}
);
router.post(
'/:id/label/:labelId',
authorization.userOwnsCard,
async (req, res) => {
tryCatch(async () => {
const cardId = req.params.id;
const labelId = req.params.labelId;
if (labelId < 5 || labelId > 14) {
return res.status(400).send({ message: 'Invalid label Id' });
}
await cardsModel.addLabelToCard(cardId, labelId);
const updatedCard = await cardsModel.getCardByID(cardId);
res.status(200).send(updatedCard);
}, res);
}
);
router.delete(
'/:id/label/:labelId',
authorization.userOwnsCard,
async (req, res) => {
tryCatch(async () => {
const cardId = req.params.id;
const labelId = req.params.labelId;
if (labelId < 5 || labelId > 14) {
return res.status(400).send({ message: 'Invalid label Id' });
}
let result = await cardsModel.removeLabelFromCard(cardId, labelId);
if (result) {
const updatedCard = await cardsModel.getCardByID(cardId);
res.status(200).send(updatedCard);
} else {
res.status(404).json({ message: 'card not found' });
}
}, res);
}
);
module.exports = router;