-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
50 lines (39 loc) · 1.11 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
import { fastify } from 'fastify';
// import { DatabaseMemory } from './database-memory.js';
import { DatabasePostgres } from './database-postgres.js';
const server = fastify();
// const database = new DatabaseMemory();
const database = new DatabasePostgres();
server.post('/videos', async (request, reply) => {
const { title, description, duration } = request.body;
await database.create({
title,
description,
duration,
});
return reply.status(201).send();
});
server.get('/videos', async (request) => {
const search = request.query.search;
const videos = await database.list(search);
return videos;
});
server.put('/videos/:id', async (request, reply) => {
const videoId = request.params.id;
const { title, description, duration } = request.body;
await database.update(videoId, {
title,
description,
duration,
});
return reply.status(204);
});
server.delete('/videos/:id', (request, reply) => {
const videoId = request.params.id;
database.delete(videoId);
return reply.status(204).send();
});
server.listen({
host: '0.0.0.0',
port: process.env.PORT ?? 3333,
});