forked from sphoebs/examware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assignments.js
59 lines (46 loc) · 1.72 KB
/
assignments.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
/**
* Created by fabio on 02/12/2017.
*/
const express = require('express'),
bodyParser = require('body-parser');
const assignments = express.Router()
var uuid = require('uuid-v4');
const deliveredAssignments = []
assignments.get('/', function (req, res) {
//console.log('get, delivering:',deliveredAssignments)
//TODO: check if there is a filter, and filter
res.json(deliveredAssignments)
})
assignments.post('/', function (req, res) {
const newAssignment = req.body
newAssignment.assignmentID = uuid()
newAssignment.dateUpdated = new Date()
deliveredAssignments.push(newAssignment)
res.json(newAssignment)
})
assignments.get('/:assignmentID', function (req, res) {
const assignmentID = req.params.assignmentID
const i = deliveredAssignments.findIndex(item => {return item.assignmentID === assignmentID})
if (i==-1) res.sendStatus(404)
else {
res.status=200
res.json(deliveredAssignments[i])
}
})
assignments.put('/:assignmentID', function (req, res) {
const assignmentID = req.params.assignmentID
const i = deliveredAssignments.findIndex(item => {return item.assignmentID === assignmentID})
deliveredAssignments[i] = req.body
deliveredAssignments[i].assignmentID = assignmentID
deliveredAssignments[i].dateUpdated = new Date()
res.json(deliveredAssignments[i])
})
assignments.delete('/:assignmentID', function (req, res) {
const assignmentID = req.params.assignmentID
if (!assignmentID) res.sendStatus(404)
const i = deliveredAssignments.findIndex(item => {return item.assignmentID === assignmentID})
const deleted = deliveredAssignments[i]
deliveredAssignments.splice(i,1)
res.sendStatus(204)
})
module.exports = assignments