-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
135 lines (116 loc) · 3.83 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
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.join(__dirname, 'public')));
app.set('port', process.env.PORT || 3000);
app.locals.title = 'Shindig';
app.listen(app.get('port'), () => {
console.log(`${app.locals.title} is running on ${app.get('port')}.`);
});
// Endpoints
/* eslint-disable no-alert, consistent-return */
app.get('/api/v1/users', (request, response) => {
database('users')
.select()
.then(users => {
if (!users.length) {
return response.status(404).json({ error: 'Users not found.' });
}
return users;
})
.then(users => response.status(200).json(users))
.catch(error => response.status(500).json({ error }));
});
app.get('/api/v1/categories', (request, response) => {
database('categories')
.select()
.then(categories => {
if (!categories.length) {
return response.status(404).json({ error: 'Categories not found.' });
}
return categories;
})
.then(categories => response.status(200).json(categories))
.catch(error => response.status(500).json({ error }));
});
app.get('/api/v1/joint_tables', (request, response) => {
database('joint_tables')
.select()
.then(preferences => {
if (!preferences.length) {
return response.status(404).json({ error: 'Preferences not found.' });
}
return preferences;
})
.then(preferences => response.status(200).json(preferences))
.catch(error => response.status(500).json({ error }));
});
app.get('/api/v1/joint_tables/:userId', (request, response) => {
const { userId } = request.params;
database('joint_tables')
.where({ userId })
.select()
.then(userId => {
if (!userId.length) {
return response.status(404).json({ error: 'No user with that Id found' });
}
response.status(200).json(userId);
})
.catch(error => {
response.status(500).json({ error });
});
});
app.post('/api/v1/users', (request, response) => {
const users = request.body.user;
if (!users.name || !users.authID || !users.photo || !users.email) {
return response.status(422).send({
error: 'Your user is missing a required property.',
});
}
database('users')
.insert(users, '*')
.then(users => response.status(201).json(users))
.catch(error => console.log(error));
});
app.post('/api/v1/joint_tables', (request, response) => {
const newPreference = request.body;
for (const userPreferences of ['categoryId', 'userId', 'prefName', 'categoryNumber']) {
if (!newPreference[userPreferences]) {
return response.status(422).send('Missing parameters');
}
}
database('joint_tables')
.insert(
{
name: newPreference.prefName,
categoryId: newPreference.categoryId,
userId: newPreference.userId,
categoryNumber: newPreference.categoryNumber,
},
'*'
)
.then(preference => response.status(201).json(preference))
.catch(error => response.status(500).json({ error }));
});
// DELETE a preference from the joint table
app.delete('/api/v1/joint_tables/:uid/:categoryId', (request, response) => {
const { uid, categoryId } = request.params;
database('joint_tables')
.where('userId', uid)
.andWhere('categoryId', categoryId)
.delete()
.then(response => {
if (!response) {
return response.status(404).json({ error: 'User preference matching id not found' });
}
response.sendStatus(204);
})
.catch(error => response.status(500).json({ error }));
});
module.exports = app;