Projeto 02 - Criação do CRUD de usuários e Rota (mockado)
Criar a rota users (src/routes/users.route.ts) e dentro dela criar o CRUD de usuários (endpoints).
import { Router, Request, Response, NextFunction } from 'express';
import { StatusCodes } from 'http-status-codes';
const usersRoute = Router();
// Listar todos os usuários
usersRoute.get('/users', (req: Request, res: Response, next: NextFunction) => {
const users = [{ userName: 'Heviane'}, { userName: 'Maria'}];
res.status(StatusCodes.OK).json(users);
});
// Listar um usuário especifico
usersRoute.get('/users/:uuid', (req: Request<{ uuid: string }>, res: Response, next: NextFunction) => {
const uuid = req.params.uuid;
res.status(StatusCodes.OK).json({ uuid });
});
// Cadastrar um usuário
usersRoute.post('/users', (req: Request, res: Response, next: NextFunction) => {
const newUser = req.body;
console.log(req.body);
res.status(StatusCodes.CREATED).send(newUser);
});
// Atualizar um usuário
usersRoute.put('/users/:uuid', (req: Request<{ uuid: string }>, res: Response, next: NextFunction) => {
const uuid = req.params.uuid;
const modifiedUser = req.body;
modifiedUser.uuid = uuid;
res.status(StatusCodes.OK).json( modifiedUser );
});
// Excluir um usuário
usersRoute.delete('/users/:uuid', (req: Request<{ uuid: string }>, res: Response, next: NextFunction) => {
const uuid = req.params.uuid;
res.status(StatusCodes.OK);
});
export default usersRoute;