Skip to content

Commit

Permalink
✨feat(catalogo): aprimora a API REST com Padrão Front Controller | Pa…
Browse files Browse the repository at this point in the history
…rte 29

- Inicializa o Banco de Dados
- Implementa uma Classe Abstrata para Um "Controller Express"
- Implementa o "Front Controller" para o Caso de Uso "Recuperar Categoria por ID"
- Associa Rotas da API para Manipular Rotas da Categoria
- Cria arquivos e casos de teste manuais para requisições nas rotas com a extensão REST Client
- Implementa "Front Controller" para o Caso de Uso "Recuperar Todas as Categoria"
  • Loading branch information
diegoarmandoo committed Dec 1, 2023
1 parent dab739c commit 5d29092
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import dotenv from 'dotenv';
import { createHTTPServer } from './presentation/http/server';
import { prisma } from '@main/infra/database/orm/prisma/client';

async function bootstrap() {

Expand All @@ -18,6 +19,12 @@ async function bootstrap() {
console.log(`[${api_name}] ✅ Servidor HTTP pronto e ouvindo em http://${host_name}:${port}`);
});

prisma.$connect().then(
async () => {
console.log(`[${api_name}] ✅ Banco de dados conectado`);
}
);

}

bootstrap()
Expand Down
5 changes: 2 additions & 3 deletions src/main/presentation/http/rest/api.v1.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { categoriaRouter } from '@modules/catalogo/presentation/http/rest/categoria.routes';
import express, { Router } from 'express';

const apiv1Router: Router = express.Router();

apiv1Router.use(
'/categorias',
function (request, response, next) {
response.json({"entidade":"Categoria"});
}
categoriaRouter
);

apiv1Router.use(
Expand Down
10 changes: 10 additions & 0 deletions src/modules/catalogo/presentation/http/rest/categoria.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@host = localhost
@port = 3000
@apiversion = api/v1

### Recuperar Um Categoria por ID
GET http://{{host}}:{{port}}/{{apiversion}}/categorias/5e66e5b0-4ecc-4cc0-882d-ea3ea53cc238

### Recuperar Todas as Categorias
GET http://{{host}}:{{port}}/{{apiversion}}/categorias

16 changes: 16 additions & 0 deletions src/modules/catalogo/presentation/http/rest/categoria.routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import express from 'express';
import { recuperarCategoriaPorIdController, recuperarTodasCategoriasController } from './controllers';

const categoriaRouter = express.Router();

categoriaRouter.get(
'/:id',
(request, response, next) => recuperarCategoriaPorIdController.recuperar(request, response, next)
)

categoriaRouter.get(
'/',
(request, response, next) => recuperarTodasCategoriasController.recuperar(request, response, next)
)

export { categoriaRouter };
11 changes: 11 additions & 0 deletions src/modules/catalogo/presentation/http/rest/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { recuperarCategoriaPorIdUseCase, recuperarTodasCategoriasUseCase } from "@modules/catalogo/application/use-cases";
import { RecuperarCategoriaPorIdExpressController } from "./recuperar-categoria-por-id.express.controller";
import { RecuperarTodasCategoriaExpressController } from "./recuperar-todas-categorias.express.controller";

const recuperarCategoriaPorIdController = new RecuperarCategoriaPorIdExpressController(recuperarCategoriaPorIdUseCase);
const recuperarTodasCategoriasController = new RecuperarTodasCategoriaExpressController(recuperarTodasCategoriasUseCase);

export {
recuperarCategoriaPorIdController,
recuperarTodasCategoriasController
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { RecuperarCategoriaPorIdUseCase } from "@modules/catalogo/application/use-cases/recuperar-categoria-por-id/recuperar-categoria-por-id.use-case";
import { ICategoria } from "@modules/catalogo/domain/categoria/categoria.types";
import { ExpressController } from "@shared/presentation/http/express.controller";
import { NextFunction, Request, Response } from "express";

class RecuperarCategoriaPorIdExpressController extends ExpressController {

private _recuperarCategoriaPorIdUseCase: RecuperarCategoriaPorIdUseCase;

constructor(recuperarCategoriaPorIdUseCase: RecuperarCategoriaPorIdUseCase) {
super();
this._recuperarCategoriaPorIdUseCase = recuperarCategoriaPorIdUseCase;
}

async recuperar(request: Request, response: Response, next: NextFunction) {
try {
const uuid:string = request.params.id;
const categoriaOutputDTO: ICategoria = await this._recuperarCategoriaPorIdUseCase.execute(uuid);
this.sendSuccessResponse(response,categoriaOutputDTO);
}
catch (error) {
next(error);
}
}

}

export { RecuperarCategoriaPorIdExpressController }
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { RecuperarTodasCategoriasUseCase } from "@modules/catalogo/application/use-cases/recuperar-todas-categorias/recuperar-todas-categorias.use-case";
import { ICategoria } from "@modules/catalogo/domain/categoria/categoria.types";
import { ExpressController } from "@shared/presentation/http/express.controller";
import { NextFunction, Request, Response } from "express";

class RecuperarTodasCategoriaExpressController extends ExpressController {

private _recuperarTodasCategoriaUseCase: RecuperarTodasCategoriasUseCase;

constructor(recuperarTodasCategoriaUseCase: RecuperarTodasCategoriasUseCase) {
super();
this._recuperarTodasCategoriaUseCase = recuperarTodasCategoriaUseCase;
}

async recuperar(request: Request, response: Response, next: NextFunction) {
try {
const listaCategoriasDTO: Array<ICategoria> = await this._recuperarTodasCategoriaUseCase.execute();
this.sendSuccessResponse(response,listaCategoriasDTO);
} catch (error) {
next(error);
}

}

}

export { RecuperarTodasCategoriaExpressController }
19 changes: 19 additions & 0 deletions src/shared/presentation/http/express.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Response } from "express";

abstract class ExpressController {

protected sendSuccessResponse(response: Response, data: any, status = 200) {
response.status(status).json(data);
}

protected sendErrorResponse(response: Response, error: Error, status = 500) {
response.status(status).json({ error: error.message });
}

protected sendNotFoundResponse(response: Response, message = 'Not found') {
response.status(404).json({ error: message });
}

}

export { ExpressController }

0 comments on commit 5d29092

Please sign in to comment.