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