Skip to content

Commit 4930817

Browse files
committed
Rota p/ POST (Create)
Definida a rota para o POST de student, com teste incluso no arquivo de rotas
1 parent 613f01b commit 4930817

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

app.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import express from 'express'
1+
import bodyParser from 'body-parser'
2+
import express from 'express';
23
import { PrismaClient } from '@prisma/client';
3-
4-
4+
import { randomUUID } from "crypto";
55
//Implementando o Prisma
66
const prisma = new PrismaClient()
77

88
const app = express()
99
const port = 3000 //Aqui define em qual porta vai rodar o app
1010

11-
app.use(express.json())
11+
app.use(express.json());
12+
//Usando o bodyParser para receber o body da request
13+
app.use(bodyParser.json());
1214

1315
const baseUrl = '/api/students'
1416

@@ -19,8 +21,18 @@ app.get(`${baseUrl}`, async (req, res) => {
1921
})
2022

2123
//Rota para o post (create) do alunos
22-
app.post(`${baseUrl}`, (req, res) => {
23-
res.send('Aqui é o post dos alunos')
24+
app.post(`${baseUrl}`, async (req, res) => {
25+
const student = await prisma.students.create({
26+
data: {
27+
id: randomUUID(),
28+
name: req.body.name,
29+
email: req.body.email,
30+
age: req.body.age
31+
//O req.body é para captar o corpo da request
32+
}
33+
})
34+
35+
res.json(student)
2436
})
2537

2638
//Rota para o put (update) dos alunos

prisma/dev.db

0 Bytes
Binary file not shown.

prisma/dev.db-journal

-8.52 KB
Binary file not shown.

rotas.http

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ GET http://localhost:3000/api/students
44
###
55

66
POST http://localhost:3000/api/students
7+
Content-Type: application/json
8+
9+
{
10+
"name": "Jaciel",
11+
"email": "jaciel@upf.br",
12+
"age": 19
13+
}
714

815
###
916

0 commit comments

Comments
 (0)