-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
66 lines (53 loc) · 1.42 KB
/
routes.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
import { Router } from 'express';
import knexfile from './knexfile.js';
import knex from 'knex';
import { randomUUID } from 'crypto';
const routes = Router();
const client = knex(knexfile);
async function setMessage(req, res, next) {
try {
const { message } = req.body
const data = {
id: randomUUID(),
createdAt: new Date(),
message,
like: 0
}
await client('messages')
.insert(data)
.catch((error) => {
throw new Error(error.detail);
});
return res.status(200).json({});
} catch (error) {
next(error);
}
}
async function listMessages(req, res, next) {
try {
const messages = await client.select('id', 'createdAt', 'message', 'like').from('messages').catch((error) => {
throw new Error(error.detail);
});
return res.status(200).json({ messages });
} catch (error) {
next(error);
}
}
async function updatedMessageLikes(req, res, next) {
try {
const { id } = req.body
await client("messages").increment('like').where('id',id).catch((error) => {
throw new Error(error.detail);
});
return res.status(200).json({});
} catch (error) {
next(error);
}
}
routes.post('/messages', setMessage);
routes.get('/messages', listMessages);
routes.put('/messages', updatedMessageLikes);
routes.get('/health', (req, res) => {
res.sendStatus(200);
});
export default routes;