-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
165 lines (131 loc) · 4.44 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
// server.js //
const express = require('express');
const app = express();
const cors = require('cors');
const environment = process.env.NODE_ENV || 'development';
const configuration = require('./knexfile')[environment];
const database = require('knex')(configuration);
app.use(cors());
app.use(express.json());
app.set('port', process.env.PORT || 3000);
app.locals.title = 'Humor Me';
app.listen(app.get('port'), () => {
console.log(`${app.locals.title} is running on http://localhost:${app.get('port')}`);
})
app.get('/api/v1/quotes', async (request, response) => {
try {
const quotes = await database('quotes').select();
response.status(200).json(quotes);
} catch(error) {
response.status(500).json({error})
}
});
app.get('/api/v1/quotes/:id', async (request, response) => {
try {
const { id } = request.params;
const quote = await database('quotes').where({ id }).first();
if (!quote) {
return response.status(404).json({ error: 'Quote not found!'})
}
response.status(200).json(quote);
} catch(error) {
response.status(500).json({error})
}
});
app.post('/api/v1/quotes', async (request, response) => {
try {
const { quote, type } = request.body;
await database('quotes').insert({ quote, type });
const lastRow = await database('quotes').select('id').orderBy('id', 'desc').first();
response.status(201).json({ message: 'Quote saved successfully', quote: { id: lastRow.id, quote, type } });
} catch(error) {
response.status(422).json({error})
}
});
app.delete('/api/v1/quotes/:id', async (request, response) => {
try {
const { id } = request.params;
await database('quotes').where({ id }).del();
response.status(200).json({ message: 'Quote deleted successfully' });
} catch(error) {
response.status(422).json({error})
}
});
app.get('/api/v1/images', async (request, response) => {
try {
const images = await database('images').select();
response.status(200).json(images);
} catch(error) {
response.status(500).json({error})
}
});
app.get('/api/v1/images/:id', async (request, response) => {
try {
const { id } = request.params;
const image = await database('images').where({ id }).first();
if (!image) {
return response.status(404).json({ error: 'Image not found!'})
}
response.status(200).json(image);
} catch(error) {
response.status(500).json({error})
}
});
app.post('/api/v1/images', async (request, response) => {
try {
const { image } = request.body;
await database('images').insert({ image });
const lastRow = await database('images').select('id').orderBy('id', 'desc').first();
response.status(201).json({ message: 'Image saved successfully', image: { id: lastRow.id, image } });
} catch(error) {
response.status(422).json({error})
}
});
app.delete('/api/v1/images/:id', async (request, response) => {
try {
const { id } = request.params;
await database('images').where({ id }).del();
response.status(200).json({ message: 'Image deleted successfully' });
} catch(error) {
response.status(422).json({error})
}
});
app.get('/api/v1/posters', async (request, response) => {
try {
const posters = await database('posters').select();
response.status(200).json(posters);
} catch(error) {
response.status(500).json({error})
}
});
app.get('/api/v1/posters/:id', async (request, response) => {
try {
const { id } = request.params;
const poster = await database('posters').where({ id }).first();
if (!poster) {
return response.status(404).json({ error: 'Poster not found!'})
}
response.status(200).json(poster);
} catch(error) {
response.status(500).json({error})
}
});
app.post('/api/v1/posters', async (request, response) => {
try {
const { quote, type, image } = request.body;
await database('posters').insert({ quote, type, image });
const lastRow = await database('posters').select('id').orderBy('id', 'desc').first();
response.status(201).json({ message: 'Poster saved successfully', poster: { id: lastRow.id, quote, type, image } });
} catch(error) {
response.status(422).json({error})
}
});
app.delete('/api/v1/posters/:id', async (request, response) => {
try {
const { id } = request.params;
await database('posters').where({ id }).del();
response.status(200).json({ message: 'Poster deleted successfully' });
} catch(error) {
response.status(422).json({error})
}
});