-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
57 lines (43 loc) · 1.45 KB
/
server.js
File metadata and controls
57 lines (43 loc) · 1.45 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
// import { createServer } from 'node:http';
// const server = createServer((request, response) => {
// response.write("Server started!")
// return response.end();
// })
// server.listen(3333);
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("Video created");
})
server.get('/videos', async (request, reply) => {
const searchParams = request.query?.search;
const videos = await database.read(searchParams);
return reply.status(200).send(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).send("Video updated");
})
server.delete('/videos/:id', async (request, reply) => {
const videoId = request.params.id;
await database.delete(videoId);
return reply.status(204).send("Video deleted");
})
server.listen({
host: '0.0.0.0',
port: process.env.PORT ?? 3333
})