-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestando.js
85 lines (69 loc) · 2.08 KB
/
testando.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const express = require('express') //iniciando o express
const router = express.Router() //configurando a primeira parte da rota
const {v4: uuidv4} = require('uuid')
const app = express() //iniciando o app
app.use(express.json()) //para que sejam enviados infromações como JSON
const porta = 3333 //criando a porta
//criando lista inicial de mulheres
const mulheres = [
{ id: '1',
nome: 'Ana Maria',
imagem: 'sem imagem',
minibio: 'Estudante de desenvolvimento Backend'
},
{
id: '2',
nome: 'Simara Conceição',
imagem: 'imagem',
minibio: 'Desenvolvedora e Instrutora'
},
{
id: '3',
nome: 'Valeska Uchôa',
imagem: 'imagem',
minibio: 'IA engeneer'
}
]
//função que mostra a lista de mulheres - GET
function mostraMulheres(request, response){
response.json(mulheres)
}
//POST
function criaMulher(request, response){
const novaMulher = {
id: uuidv4(),
nome: request.body.nome,
imagem: request.body.imagem,
minibio: request.body.minibio
}
//colocar nova mulher na lista de mulheres
mulheres.push(novaMulher) //recebe novaMulher
response.json(mulheres)
}
//PATCH
function corrigeMulher(request, response){
function encontraMulher(mulher){
if (mulher.id === request.params.id){
return mulher
}
}
const mulherEncontrada = mulheres.find(encontraMulher)
if (request.body.nome) {
mulherEncontrada.nome = request.body.nome
}
if (request.body.imagem) {
mulherEncontrada.imagem = request.body.imagem
}
if (request.body.minibio) {
mulherEncontrada.minibio = request.body.minibio
}
response.json(mulheres)
}
//PORTA
function mostraPorta(){
console.log("Servidor criado e rodando na porta:", porta)
}
app.use(router.get('/mulheres', mostraMulheres)) //configurei a rota GET /mulheres
app.use(router.post('/mulheres', criaMulher))
app.use(router.patch('/mulheres/:id', corrigeMulher))
app.listen(porta, mostraPorta) //Servidor ouvindo a porta