-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
127 lines (111 loc) · 3.2 KB
/
server.js
File metadata and controls
127 lines (111 loc) · 3.2 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
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const enviroment = process.env.NODE_ENV || 'development';
const configuration = require('./knexfile')[enviroment];
const database = require('knex')(configuration);
app.set('port', process.env.PORT || 3000);
app.locals.title = 'Palette Picker';
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static('public'));
app.get('/', (request, response) => {
});
app.get('/api/v1/projects', (request, response) => {
database('projects').select()
.then(projects => {
response.status(200).json(projects);
})
.catch(error => {
response.status(500).json({error})
});
});
app.post('/api/v1/projects', (request, response) => {
const project = request.body;
if (!project.name) {
return response
.status(422)
.send({error:`Expected fromat: {name: <string>}. You're missing a name property`});
}
database('projects').insert(project, 'id')
.then(project => {
response.status(201).json({
name: project.name,
id: project[0]
})
})
.catch(error => {
response.status(500).json({error});
});
});
app.get('/api/v1/palettes', (request, response) => {
database('palettes').select()
.then(palettes => {
response.status(200).json(palettes)
})
.catch(error => {
response.status(500).json({error})
})
});
app.get('api/v1/palettes/:id', (request, response) => {
database('palettes').where('id', request.params.id).select()
.then(palette => {
if (palette.length) {
request.status(200).json(palette)
} else {
request.status(404).json({
error: `Could not find palette with id ${request.params.id}`
})
}
})
.catch(error => {
request.status(500).json({error})
})
});
app.delete('/api/v1/palettes/:id', (request, response) => {
database('palettes').where('id', request.params.id).del()
.then(id => {
if (id) {
response.status(204).json({ id })
} else {
response.status(404).json({
error: `Could not find palette with id ${request.params.id}`
})
}
})
.catch(error => {
response.status(500).json({ error });
})
})
app.post('/api/v1/palettes', (request, response) => {
const usersData = request.body;
for (let requiredParameter of ['name', 'colors', 'project_id']) {
if(!usersData[requiredParameter]) {
return response
.status(422)
.send({error: `Expected format: { name: <String>, project_id: <number>, colors: <array> }. You're missing a "${requiredParameter}" property.`
});
}
}
const { name, colors, project_id } = usersData;
const palette = {
name,
color1:colors[0],
color2:colors[1],
color3:colors[2],
color4:colors[3],
color5:colors[4],
project_id
}
database('palettes').insert(palette, 'id')
.then(palette => {
response.status(201).json({id: palette[0]})
})
.catch(error => {
response.status(500).json({error})
});
})
app.listen(app.get('port'), () => {
console.log(`${app.locals.title} is running on ${app.get('port')}.`)
})
module.exports = app