-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
201 lines (175 loc) · 5.52 KB
/
index.js
File metadata and controls
201 lines (175 loc) · 5.52 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
const express = require('express');
const helmet = require('helmet');
const knex = require('knex');
const knexConfig = require('./knexfile.js');
const db = knex(knexConfig.development);
const server = express();
server.use(express.json());
server.use(helmet());
// endpoints here
server.get('/api/cohorts', async (req, res) => {
try {
const cohorts = await db('cohorts');
res.status(200).json(cohorts);
} catch (error) {
res.status(500).json({ message: " error: 'The cohorts could not be retrieved'", error: error });
}
});
server.get('/api/students', async (req, res) => {
try {
const students = await db('students');
res.status(200).json(students);
} catch (error) {
res.status(500).json({ message: " error: 'The students could not be retrieved'", error: error });
}
});
server.get('/api/cohorts/:cohortId', async (req, res) => {
const { cohortId } = req.params;
try {
const cohort = await db('cohorts').where({ id: cohortId });
{
cohort[0]
? res.status(200).json({ cohort })
: res.status(404).json({ error: 'The cohort with that ID does not exist.' });
}
} catch (error) {
console.log('The error is: ', error);
res.status(500).json(error);
}
});
server.get('/api/students/:studentId', async (req, res) => {
const { studentId } = req.params;
try {
const student = await db('students')
.select('students.id', 'students.name', 'cohorts.name as cohort')
.join('cohorts', 'students.cohort_id', '=', 'cohorts.id')
.where({ 'students.id': studentId });
{
student[0]
? res.status(200).json(student[0])
: res.status(404).json({ error: 'The student with that ID does not exist.' });
}
} catch (error) {
console.log('The error is: ', error);
res.status(500).json(error);
}
});
//:variable needs to match const { variable } = req.params
server.get('/api/cohorts/:id/students', async (req, res) => {
const { id } = req.params;
console.log('the cohortID is: ', id);
try {
const cohortStudents = await db('students').where({ cohort_id: id });
{
cohortStudents[0]
? res.status(200).json({ cohortStudents })
: res.status(404).json({ error: 'The cohort with that ID does not exist.' });
}
} catch (error) {
console.log('The error is: ', error);
res.status(500).json(error);
}
});
server.post('/api/cohorts', async (req, res) => {
const cohortData = req.body;
if (!cohortData.name) {
res.status(400).json({ errorMessage: 'Please provide a name for your cohort' });
} else {
try {
const newCohortId = await db('cohorts').insert(cohortData);
res.status(201).json(newCohortId);
} catch (error) {
console.log(error);
res.status(500).json({ message: 'Error inserting cohort', error });
}
}
});
server.post('/api/students', async (req, res) => {
const studentData = req.body;
if (!studentData.name) {
res.status(400).json({ errorMessage: 'Please provide a name for your student' });
} else {
try {
const newStudentId = await db('students').insert(studentData);
res.status(201).json(newStudentId);
} catch (error) {
console.log(error);
res.status(500).json({ message: 'Error inserting student', error });
}
}
});
server.delete('/api/cohorts/:cohortId', async (req, res) => {
const { cohortId } = req.params;
try {
const deletedCohortCount = await db('cohorts')
.where({ id: cohortId })
.del();
{
deletedCohortCount === 0
? res.status(404).json({ message: 'The cohort with the specified ID does not exist.' })
: res.status(200).json({ deletedCohortCount });
}
} catch (error) {
res.status(500).json(error);
}
});
server.delete('/api/students/:studentId', async (req, res) => {
const { studentId } = req.params;
try {
const deletedStudentCount = await db('students')
.where({ id: studentId })
.del();
{
deletedStudentCount === 0
? res.status(404).json({ message: 'The student with the specified ID does not exist.' })
: res.status(200).json({ deletedStudentCount });
}
} catch (error) {
res.status(500).json(error);
}
});
server.put('/api/cohorts/:cohortId', async (req, res) => {
const changes = req.body;
const { cohortId } = req.params;
if (!changes.name) {
res.status(400).json({ errorMessage: 'Please provide a name for the cohort.' });
}
try {
const updatedCohortCount = await db('cohorts')
.where({ id: cohortId })
.update(changes);
{
updatedCohortCount === 0
? res.status(404).json({ message: 'The cohort with the specified ID does not exist.' })
: res.status(200).json({ updatedCohortCount });
}
} catch (error) {
res.status(500).json(error);
}
});
server.put('/api/students/:studentId', async (req, res) => {
const changes = req.body;
const { studentId } = req.params;
if (!changes.name) {
res.status(400).json({ errorMessage: 'Please provide a name for the student.' });
}
try {
const updatedStudentCount = await db('students')
.where({ id: studentId })
.update(changes);
{
updatedStudentCount === 0
? res.status(404).json({ message: 'The student with the specified ID does not exist.' })
: res.status(200).json({ updatedStudentCount });
}
} catch (error) {
res.status(500).json(error);
}
});
server.get('/', (req, res) => {
res.json({ api: 'up' });
});
const port = 3300;
server.listen(port, function() {
console.log(`\n=== Web API Listening on http://localhost:${port} ===\n`);
});