-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨feat(catalogo): aprimora a API REST com Padrão Front Controller | Pa…
…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
1 parent
dab739c
commit 5d29092
Showing
8 changed files
with
120 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/modules/catalogo/presentation/http/rest/categoria.http
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
src/modules/catalogo/presentation/http/rest/categoria.routes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
src/modules/catalogo/presentation/http/rest/controllers/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
28 changes: 28 additions & 0 deletions
28
...alogo/presentation/http/rest/controllers/recuperar-categoria-por-id.express.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
27 changes: 27 additions & 0 deletions
27
...alogo/presentation/http/rest/controllers/recuperar-todas-categorias.express.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |