const express = require ('express')
const app = express();
app.use(express.json())
let users = [
{
"id" : 1,
"name": "Jorge",
"lastname": "caloir",
"position": "usuario"
},
{
"id" : 2,
"name": "Oscar",
"lastname": "Zabrano",
"position": "Programador"
},
{
"id" : 3,
"name": "Ender",
"lastname": "Perez",
"position": "Gerente"
}
]
app.get('/api/users', (request, response) => {
response.json(users)
})
app.get('/api/users/:id', (request, response) => {
const id = Number(request.params.id)
const user = users.find( user => user.id === id)
if (user) {
response.json(user)
} else {
response.status(404).end()
}
})
app.delete('/api/users/:id', (request, response) => {
const id = Number(request.params.id)
users = users.filter( user => user.id !== id)
response.status(204).end()
})
app.post('/api/users', (request, response) => {
const user = request.body
console.log(user);
response.json(user)
const ids = users.map( user => user.id)
const maxId = Math.max(...ids)
console.log(user)
const newUser = [{
id : maxId + 1,
name : user.name,
lastname : user.lastname,
position : user.position,
}]
users = [... users, newUser]
})
const PORT = 3001
app.listen(PORT), () => {