-
Notifications
You must be signed in to change notification settings - Fork 2
Feat: Listar e buscar tipos de relevo Api.v2 #506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JosueModesto
wants to merge
6
commits into
development
Choose a base branch
from
491-listar-buscar-tipos-relevos
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5fb6467
Feat: Listar e buscar tipos de relevo Api.v2
JosueModesto 1215edb
fix: test
JosueModesto 072b782
Merge remote-tracking branch 'origin/development' into 491-listar-bus…
JosueModesto c18d6c9
fix: Correção no parserOrder e no test
JosueModesto 109fc8e
correções review
JosueModesto f66291f
Merge branch 'development' into 491-listar-buscar-tipos-relevos
JosueModesto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,41 @@ | ||
| import { BuscaRelevoPorIdUseCase } from '@/domain/relevo/BuscaRelevoPorIdUseCase' | ||
| import { | ||
| HttpRequest, HttpResponse, StatusCode | ||
| } from '@/library/http/common' | ||
| import { BadRequestError } from '@/library/http/error/BadRequestError' | ||
| import { HttpError } from '@/library/http/error/HttpError' | ||
| import { InternalServerError } from '@/library/http/error/InternalServerError' | ||
| import { NotFoundError } from '@/library/http/error/NotFoundError' | ||
| import { NextHandler, RequestHandler } from '@/library/http/Server' | ||
|
|
||
| interface Dependencies { | ||
| buscaRelevoPorIdUseCase: BuscaRelevoPorIdUseCase | ||
| } | ||
|
|
||
| export class BuscaRelevoController implements RequestHandler { | ||
| private readonly buscaRelevoPorIdUseCase: BuscaRelevoPorIdUseCase | ||
|
|
||
| constructor(dependencies: Dependencies) { | ||
| this.buscaRelevoPorIdUseCase = dependencies.buscaRelevoPorIdUseCase | ||
| } | ||
|
|
||
| async handle(request: HttpRequest, _next: NextHandler): Promise<HttpResponse | HttpError> { | ||
| const { relevoId } = request.params as { relevoId?: string } | ||
|
|
||
| if (relevoId === undefined || relevoId === null || relevoId === '' || !/^\d+$/.test(relevoId)) { | ||
| return new BadRequestError({ message: 'relevoId inválido' }) | ||
| } | ||
|
|
||
| const result = await this.buscaRelevoPorIdUseCase.execute({ id: Number(relevoId) }) | ||
|
|
||
| if (result.left()) { | ||
| return new InternalServerError({ message: result.value.message }) | ||
| } | ||
|
|
||
| if (!result.value) { | ||
| return new NotFoundError({ message: 'Relevo não encontrado' }) | ||
| } | ||
|
|
||
| return { statusCode: StatusCode.Ok, body: result.value } | ||
| } | ||
| } |
This file contains hidden or 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,65 @@ | ||
| import { ListaRelevosUseCase } from '@/domain/relevo/ListaRelevosUseCase' | ||
| import { | ||
| HttpRequest, HttpResponse, StatusCode | ||
| } from '@/library/http/common' | ||
| import { BadRequestError } from '@/library/http/error/BadRequestError' | ||
| import { HttpError } from '@/library/http/error/HttpError' | ||
| import { InternalServerError } from '@/library/http/error/InternalServerError' | ||
| import { NextHandler, RequestHandler } from '@/library/http/Server' | ||
|
|
||
| interface Dependencies { | ||
| listaRelevosUseCase: ListaRelevosUseCase | ||
| } | ||
|
|
||
| export class ListaRelevosController implements RequestHandler { | ||
| private readonly listaRelevosUseCase: ListaRelevosUseCase | ||
|
|
||
| constructor(dependencies: Dependencies) { | ||
| this.listaRelevosUseCase = dependencies.listaRelevosUseCase | ||
| } | ||
|
|
||
| async handle(request: HttpRequest, _next: NextHandler): Promise<HttpResponse | HttpError> { | ||
| const { nome, order } = request.params as { | ||
| nome?: string | ||
| order?: string | ||
| } | ||
|
|
||
| const parsedOrder = parseOrder(order) | ||
| if (parsedOrder instanceof Error) { | ||
| return new BadRequestError({ message: parsedOrder.message }) | ||
| } | ||
|
|
||
| const result = await this.listaRelevosUseCase.execute({ | ||
| nome, | ||
| order: parsedOrder | ||
| }) | ||
|
|
||
| if (result.left()) { | ||
| return new InternalServerError({ message: result.value.message }) | ||
| } | ||
|
|
||
| return { statusCode: StatusCode.Ok, body: result.value } | ||
| } | ||
| } | ||
|
|
||
| function parseOrder(order?: string): { column: 'id' | 'nome'; direction: 'asc' | 'desc' } | Error | undefined { | ||
| if (!order) return undefined | ||
|
|
||
| const pieces = order.split(':') | ||
| if (pieces.length !== 2) { | ||
| return new Error('order inválido. Use o formato "id:asc", "id:desc", "nome:asc" ou "nome:desc"') | ||
| } | ||
|
|
||
| const [rawColumn, rawDirection] = pieces | ||
| const column = rawColumn.trim().toLowerCase() | ||
| const direction = rawDirection.trim().toLowerCase() | ||
|
|
||
| if ((column !== 'id' && column !== 'nome') || (direction !== 'asc' && direction !== 'desc')) { | ||
| return new Error('order inválido. Use o formato "id:asc", "id:desc", "nome:asc" ou "nome:desc"') | ||
| } | ||
|
|
||
| return { | ||
| column, | ||
| direction | ||
| } | ||
| } |
This file contains hidden or 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,35 @@ | ||
| import { type Knex } from 'knex' | ||
|
|
||
| import { BuscaRelevoPorIdUseCase } from '@/domain/relevo/BuscaRelevoPorIdUseCase' | ||
| import { ListaRelevosUseCase } from '@/domain/relevo/ListaRelevosUseCase' | ||
| import { RelevoCollectionKnexAdapter } from '@/infrastructure/RelevoCollectionKnexAdapter' | ||
| import { Method } from '@/library/http/common' | ||
| import { Route } from '@/library/http/Router' | ||
|
|
||
| import { BuscaRelevoController } from './BuscaRelevoController' | ||
| import { ListaRelevosController } from './ListaRelevosController' | ||
|
|
||
| export function routes(knex: Knex): Route[] { | ||
| const relevoCollection = new RelevoCollectionKnexAdapter({ knex }) | ||
|
|
||
| return [ | ||
| { | ||
| handlers: [ | ||
| new ListaRelevosController({ | ||
| listaRelevosUseCase: new ListaRelevosUseCase({ relevoCollection }) | ||
| }) | ||
| ], | ||
| method: Method.Get, | ||
| path: '/v2/relevos' | ||
| }, | ||
| { | ||
| handlers: [ | ||
| new BuscaRelevoController({ | ||
| buscaRelevoPorIdUseCase: new BuscaRelevoPorIdUseCase({ relevoCollection }) | ||
| }) | ||
| ], | ||
| method: Method.Get, | ||
| path: '/v2/relevos/:relevoId' | ||
| } | ||
| ] | ||
| } |
This file contains hidden or 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,20 @@ | ||
| import { Either } from '@/library/either/Either' | ||
|
|
||
| import { Attributes } from './Relevo' | ||
| import { RelevoCollection } from './RelevoCollection' | ||
|
|
||
| interface Dependencies { | ||
| relevoCollection: RelevoCollection | ||
| } | ||
|
|
||
| export class BuscaRelevoPorIdUseCase { | ||
| private readonly relevoCollection: RelevoCollection | ||
|
|
||
| constructor(dependencies: Dependencies) { | ||
| this.relevoCollection = dependencies.relevoCollection | ||
| } | ||
|
|
||
| execute({ id }: { id: number }): Promise<Either<Error, Attributes | null>> { | ||
| return this.relevoCollection.findById(id) | ||
| } | ||
| } |
This file contains hidden or 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,20 @@ | ||
| import { Either } from '@/library/either/Either' | ||
|
|
||
| import { Attributes } from './Relevo' | ||
| import { RelevoCollection, RelevoFilters } from './RelevoCollection' | ||
|
|
||
| interface Dependencies { | ||
| relevoCollection: RelevoCollection | ||
| } | ||
|
|
||
| export class ListaRelevosUseCase { | ||
| private readonly relevoCollection: RelevoCollection | ||
|
|
||
| constructor(dependencies: Dependencies) { | ||
| this.relevoCollection = dependencies.relevoCollection | ||
| } | ||
|
|
||
| execute(filters: RelevoFilters): Promise<Either<Error, Attributes[]>> { | ||
| return this.relevoCollection.findAll(filters) | ||
| } | ||
| } |
This file contains hidden or 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,24 @@ | ||
| import { Either } from '@/library/either/Either' | ||
|
|
||
| export interface Attributes { | ||
| id: number | ||
| nome: string | ||
| } | ||
|
|
||
| export class Relevo { | ||
| readonly id: number | ||
| readonly nome: string | ||
|
|
||
| private constructor(attributes: Attributes) { | ||
| this.id = attributes.id | ||
| this.nome = attributes.nome | ||
| } | ||
|
|
||
| static create(attributes: Attributes): Either<Error, Relevo> { | ||
| if (!attributes.nome.trim()) { | ||
| return Either.left(new Error('Nome do relevo não pode ser vazio')) | ||
| } | ||
|
|
||
| return Either.right(new Relevo(attributes)) | ||
| } | ||
| } |
This file contains hidden or 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,18 @@ | ||
| import { Either } from '@/library/either/Either' | ||
|
|
||
| import { Attributes } from './Relevo' | ||
|
|
||
| export interface RelevoOrder { | ||
| column: 'id' | 'nome' | ||
| direction: 'asc' | 'desc' | ||
| } | ||
|
|
||
| export interface RelevoFilters { | ||
| nome?: string | ||
| order?: RelevoOrder | ||
| } | ||
|
|
||
| export interface RelevoCollection { | ||
| findAll(filters: RelevoFilters): Promise<Either<Error, Attributes[]>> | ||
| findById(id: number): Promise<Either<Error, Attributes | null>> | ||
| } |
This file contains hidden or 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,46 @@ | ||
| import { Knex } from 'knex' | ||
|
|
||
| import { Attributes } from '@/domain/relevo/Relevo' | ||
| import { RelevoCollection, RelevoFilters } from '@/domain/relevo/RelevoCollection' | ||
| import { Either } from '@/library/either/Either' | ||
|
|
||
| import { CollectionError } from './error/CollectionError' | ||
|
|
||
| interface Dependencies { | ||
| knex: Knex | ||
| } | ||
|
|
||
| export class RelevoCollectionKnexAdapter implements RelevoCollection { | ||
| private readonly knex: Knex | ||
|
|
||
| constructor(dependencies: Dependencies) { | ||
| this.knex = dependencies.knex | ||
| } | ||
|
|
||
| async findAll(filters: RelevoFilters): Promise<Either<Error, Attributes[]>> { | ||
| try { | ||
| const query = this.knex<Attributes>('relevos') | ||
| .select(['id', 'nome']) | ||
|
|
||
| if (filters.nome) { | ||
| query.whereILike('nome', `%${filters.nome}%`) | ||
| } | ||
|
|
||
| const order = filters.order ?? { column: 'id', direction: 'desc' } | ||
| query.orderBy(order.column, order.direction) | ||
|
|
||
| return Either.right(await query) | ||
| } catch (error) { | ||
| return Either.left(new CollectionError({ message: 'Failed to list relevos', cause: error })) | ||
| } | ||
| } | ||
|
|
||
| async findById(id: number): Promise<Either<Error, Attributes | null>> { | ||
| try { | ||
| const relevo = await this.knex<Attributes>('relevos').select(['id', 'nome']).where({ id }).first() | ||
| return Either.right(relevo ?? null) | ||
| } catch (error) { | ||
| return Either.left(new CollectionError({ message: 'Failed to find relevo', cause: error })) | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,42 @@ | ||
| import { | ||
| afterAll, describe, expect, test | ||
| } from 'vitest' | ||
|
|
||
| import { createTestApp } from '../setup/app-factory' | ||
|
|
||
| type Relevo = { id: number; nome: string } | ||
|
|
||
| const returning = ['id', 'nome'] as const | ||
|
|
||
| describe('GET /api/v2/relevos/:relevoId', () => { | ||
| const { agent, knex } = createTestApp() | ||
|
|
||
| afterAll(() => knex.destroy()) | ||
|
|
||
| test('retorna o registro encontrado', async () => { | ||
| const [relevo] = await knex('relevos') | ||
| .insert({ nome: 'XREL Relevo Encontrado' }) | ||
| .returning<Relevo[]>(returning) | ||
|
|
||
| try { | ||
| const response = await agent.get(`/api/v2/relevos/${relevo.id}`).expect(200) | ||
| expect(response.body).toEqual({ id: relevo.id, nome: relevo.nome }) | ||
| } finally { | ||
| await knex('relevos').where({ id: relevo.id }).delete() | ||
| } | ||
| }) | ||
|
|
||
| test('retorna 404 para id inexistente', async () => { | ||
| const response = await agent.get('/api/v2/relevos/999999').expect(404) | ||
| const body = response.body as { error: { message: string } } | ||
|
|
||
| expect(body.error.message).toMatch(/não encontrad[ao]|not found/i) | ||
| }) | ||
|
|
||
| test('retorna 400 para id inválido', async () => { | ||
| const response = await agent.get('/api/v2/relevos/abc').expect(400) | ||
| const body = response.body as { error: { message: string } } | ||
|
|
||
| expect(body.error.message).toMatch(/inválido|invalid/i) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
O PR está em conflito com
development. No rebase, registrecreateRelevoRoutessem removercreateVegetacaoRoutes, que já existe na base.