-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
175 lines (143 loc) · 5.02 KB
/
server.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
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
require('newrelic');
const express = require('express');
const app = express();
const path = require('path');
const bodyParser = require('body-parser');
const environment = process.env.NODE_ENV || 'development';
const configuration = require('./knexfile')[environment];
const database = require('knex')(configuration);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.resolve(__dirname, './react-ui/build')));
app.set('port', process.env.PORT || 3001);
app.get('/', (request, response) => {
response.readFile(path.resolve(__dirname, './react-ui/build', 'index.html', (err, file) => {
response.send(file);
}));
});
app.get('/api/v1/users', (request, response) => {
database('users').select()
.then((users) => {
response.status(200).json(users);
})
.catch((error) => {
response.status(500).json({ error });
});
});
app.get('/api/v1/users/:id', (request, response) => {
const { id } = request.params;
database('users').where('oath_id', id).select()
.then((user) => {
if (user.length === 0) {
return response.status(404).json({
error: `Could not find user with an oath_id ${id}`,
});
}
return response.status(200).json(user);
})
.catch((error) => {
response.status(500).json(error);
});
});
app.post('/api/v1/users', (request, response) => {
const user = request.body;
for (const requiredParameter of ['username', 'email', 'oath_id']) {
if (!user[requiredParameter]) {
return response
.status(422)
.send({ error: `Expected format: { name: <String>, email: <String>, street_address: <String>, oath_id: <String>}. You're missing a '${requiredParameter}' property.` });
}
}
database('users').insert(user, 'id')
.then((userResponse) => {
return response.status(201).json({ id: userResponse[0] });
})
.catch((error) => {
return response.status(500).json({ error });
});
});
app.put('/api/v1/users/:id', (request, response) => {
const { id } = request.params;
const user = request.body;
for (const requiredParameter of ['username', 'email', 'street_address', 'oath_id']) {
if (!user[requiredParameter]) {
return response
.status(422)
.json({ error: `Expected format: { name: <String>, email: <String>, street_address: <String>, oath_id: <String>}. You're missing a '${requiredParameter}' property.` });
}
}
database('users').where({ id }).update(user, 'id')
.then((res) => {
if (res.length === 0) {
return response.status(404).json('User id not found');
}
return response.status(201).json(`User id:${id} was updated.`);
})
.catch((error) => {
response.status(500).json(error);
});
});
app.patch('/api/v1/users/:id', (request, response) => {
const { id } = request.params;
const user = request.body;
database('users').where('id', id).update(user, '*')
.then((data) => {
if (!data.length) {
return response.status(404).json('User id not found');
}
return response.status(201).json(data);
})
.catch((error) => {
response.status(500).json(error);
});
});
app.get('/api/v1/favorites/:id', (request, response) => {
const { id } = request.params;
database('favorites').where('user_id', id).select()
.then((favorites) => {
if (favorites.length === 0) {
return response.status(404).json({
error: `Could not find favorites with associated with user_id: ${id}`,
});
}
return response.status(200).json(favorites);
})
.catch((error) => {
response.status(500).json(error);
});
});
app.post('/api/v1/favorites', (request, response) => {
const favorite = request.body;
for (const requiredParameter of ['school_id', 'school_address', 'school_website', 'school_name', 'school_code', 'user_id', 'commute_time', 'commute_distance', 'commute_type']) {
if (!favorite[requiredParameter]) {
return response
.status(422)
.send({ error: `Expected format: { school_id: <String>, school_address: <String>, school_website: <String>, school_name: <String>, school_code: <String>, user_id: <String>, commute_time: <String>, commute_distance: <String>}. You're missing a '${requiredParameter}' property.` });
}
}
console.log(favorite)
database('favorites').insert(favorite, '*')
.then((user) => {
return response.status(201).json(user);
})
.catch((error) => {
return response.status(500).json({ error });
});
});
app.delete('/api/v1/favorites/:id', (request, response) => {
const { id } = request.params;
database('favorites').where('school_code', id ).del()
.then((favorite) => {
if (favorite) {
return response.sendStatus(204);
}
return response.status(422).json({ error: 'Not Found' });
})
.catch((error) => {
response.status(500).json({ error });
});
});
app.listen(app.get('port'), () => {
console.log(`User Data API is running on ${app.get('port')}.`);
});
module.exports = app;