forked from bloominstituteoftechnology/webdb-iii-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
83 lines (76 loc) · 1.76 KB
/
index.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
const express = require('express');
const server = express();
server.use(express.json());
const knex = require('knex');
const knexConfig = require('./knexfile');
const db = knex(knexConfig.development);
server.post('/api/cohorts', (req, res) => {
const cohort = req.body;
db('cohorts')
.insert(cohort)
.then(ids => {
res.status(201).json(ids);
})
.catch(err => {
res.status(500).json(err);
});
});
server.get('/api/cohorts', (req, res) => {
db.from('cohorts')
.then(list => {
res.status(200).json(list);
})
.catch(err => {
res.status(500).json(err);
});
});
server.get('/api/cohorts/:id', (req, res) => {
const uniqueCohort = req.params.id;
db.from('cohorts')
.where({ id: uniqueCohort })
.then(cohort => {
res.status(200).json(cohort);
})
.catch(err => {
res.status(500).json(err);
});
});
server.get('/api/cohorts/:id/students', (req, res) => {
const cohortForStudents = req.params.id;
db.from('students')
.where({ cohort_id: '1' })
.then(students => {
res.status(200).json(students);
})
.catch(err => {
res.status(500).json(err);
});
});
server.put('/api/cohorts/:id', (req, res) => {
const cohortToModify = req.params.id;
db('cohorts')
.where({ id: cohortToModify })
.update(req.body)
.then(numberUpdated => {
res.status(200).json(numberUpdated);
})
.catch(err => {
res.status(500).json(err);
});
});
server.delete('/api/cohorts/:id/', (req, res) => {
const cohortToDelete = req.params.id;
db('cohorts')
.where({ id: cohortToDelete })
.del()
.then(numDeleted => {
res.status(200).json(numDeleted);
})
.catch(err => {
res.status(500).json(err);
});
});
const port = 3300;
server.listen(port, function() {
console.log(`\n=== Web API Listening on http://localhost:${port} ===\n`);
});