-
Notifications
You must be signed in to change notification settings - Fork 2
Feat: Listar e buscar tipos de solo Api.v2 #504
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
493-listar-buscar-tipos-solos
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
2c9bdac
Feat: Listar e buscar tipos de solo
JosueModesto bc220de
fix: yarn lint
JosueModesto 96f85e9
fix:test
JosueModesto 39dd272
Merge remote-tracking branch 'origin/development' into 493-listar-bus…
JosueModesto af590fa
fix: correção no parseOrder e no test
JosueModesto fe390d7
fix: correções
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 { BuscaSoloPorIdUseCase } from '@/domain/solo/BuscaSoloPorIdUseCase' | ||
| 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 { | ||
| buscaSoloPorIdUseCase: BuscaSoloPorIdUseCase | ||
| } | ||
|
|
||
| export class BuscaSoloController implements RequestHandler { | ||
| private readonly buscaSoloPorIdUseCase: BuscaSoloPorIdUseCase | ||
|
|
||
| constructor(dependencies: Dependencies) { | ||
| this.buscaSoloPorIdUseCase = dependencies.buscaSoloPorIdUseCase | ||
| } | ||
|
|
||
| async handle(request: HttpRequest, _next: NextHandler): Promise<HttpResponse | HttpError> { | ||
| const { soloId } = request.params as { soloId?: string } | ||
|
|
||
| if (soloId === undefined || soloId === null || soloId === '' || !/^\d+$/.test(soloId)) { | ||
| return new BadRequestError({ message: 'soloId inválido' }) | ||
| } | ||
|
|
||
| const result = await this.buscaSoloPorIdUseCase.execute({ id: Number(soloId) }) | ||
|
|
||
| if (result.left()) { | ||
| return new InternalServerError({ message: result.value.message }) | ||
| } | ||
|
|
||
| if (!result.value) { | ||
| return new NotFoundError({ message: 'Solo 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,41 @@ | ||
| import { BuscarSoloPorIdUseCase } from '@/domain/solo/BuscarSoloPorIdUseCase' | ||
| 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 { | ||
| buscarSoloPorIdUseCase: BuscarSoloPorIdUseCase | ||
| } | ||
|
|
||
| export class BuscarSoloController implements RequestHandler { | ||
| private readonly buscarSoloPorIdUseCase: BuscarSoloPorIdUseCase | ||
|
|
||
| constructor(dependencies: Dependencies) { | ||
| this.buscarSoloPorIdUseCase = dependencies.buscarSoloPorIdUseCase | ||
| } | ||
|
|
||
| async handle(request: HttpRequest, _next: NextHandler): Promise<HttpResponse | HttpError> { | ||
| const { soloId } = request.params as { soloId?: string } | ||
|
|
||
| if (soloId === undefined || soloId === null || soloId === '' || !/^\d+$/.test(soloId)) { | ||
| return new BadRequestError({ message: 'soloId inválido' }) | ||
| } | ||
|
|
||
| const result = await this.buscarSoloPorIdUseCase.execute({ id: Number(soloId) }) | ||
|
|
||
| if (result.left()) { | ||
| return new InternalServerError({ message: result.value.message }) | ||
| } | ||
|
|
||
| if (!result.value) { | ||
| return new NotFoundError({ message: 'Solo 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 { ListaSolosUseCase } from '@/domain/solo/ListaSolosUseCase' | ||
| 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 { | ||
| listaSolosUseCase: ListaSolosUseCase | ||
| } | ||
|
|
||
| export class ListaSolosController implements RequestHandler { | ||
| private readonly listaSolosUseCase: ListaSolosUseCase | ||
|
|
||
| constructor(dependencies: Dependencies) { | ||
| this.listaSolosUseCase = dependencies.listaSolosUseCase | ||
| } | ||
|
|
||
| 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.listaSolosUseCase.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 { BuscaSoloPorIdUseCase } from '@/domain/solo/BuscaSoloPorIdUseCase' | ||
| import { ListaSolosUseCase } from '@/domain/solo/ListaSolosUseCase' | ||
| import { SoloCollectionKnexAdapter } from '@/infrastructure/SoloCollectionKnexAdapter' | ||
| import { Method } from '@/library/http/common' | ||
| import { Route } from '@/library/http/Router' | ||
|
|
||
| import { BuscaSoloController } from './BuscaSoloController' | ||
| import { ListaSolosController } from './ListaSolosController' | ||
|
|
||
| export function routes(knex: Knex): Route[] { | ||
| const soloCollection = new SoloCollectionKnexAdapter({ knex }) | ||
|
|
||
| return [ | ||
| { | ||
| handlers: [ | ||
| new ListaSolosController({ | ||
| listaSolosUseCase: new ListaSolosUseCase({ soloCollection }) | ||
| }) | ||
| ], | ||
| method: Method.Get, | ||
| path: '/v2/solos' | ||
| }, | ||
| { | ||
| handlers: [ | ||
| new BuscaSoloController({ | ||
| buscaSoloPorIdUseCase: new BuscaSoloPorIdUseCase({ soloCollection }) | ||
| }) | ||
| ], | ||
| method: Method.Get, | ||
| path: '/v2/solos/:soloId' | ||
| } | ||
| ] | ||
| } |
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 './Solo' | ||
| import { SoloCollection } from './SoloCollection' | ||
|
|
||
| interface Dependencies { | ||
| soloCollection: SoloCollection | ||
| } | ||
|
|
||
| export class BuscaSoloPorIdUseCase { | ||
| private readonly soloCollection: SoloCollection | ||
|
|
||
| constructor(dependencies: Dependencies) { | ||
| this.soloCollection = dependencies.soloCollection | ||
| } | ||
|
|
||
| execute({ id }: { id: number }): Promise<Either<Error, Attributes | null>> { | ||
| return this.soloCollection.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 './Solo' | ||
| import { SoloCollection } from './SoloCollection' | ||
|
|
||
| interface Dependencies { | ||
| soloCollection: SoloCollection | ||
| } | ||
|
|
||
| export class BuscarSoloPorIdUseCase { | ||
| private readonly soloCollection: SoloCollection | ||
|
|
||
| constructor(dependencies: Dependencies) { | ||
| this.soloCollection = dependencies.soloCollection | ||
| } | ||
|
|
||
| execute({ id }: { id: number }): Promise<Either<Error, Attributes | null>> { | ||
| return this.soloCollection.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 './Solo' | ||
| import { SoloCollection, SoloFilters } from './SoloCollection' | ||
|
|
||
| interface Dependencies { | ||
| soloCollection: SoloCollection | ||
| } | ||
|
|
||
| export class ListaSolosUseCase { | ||
| private readonly soloCollection: SoloCollection | ||
|
|
||
| constructor(dependencies: Dependencies) { | ||
| this.soloCollection = dependencies.soloCollection | ||
| } | ||
|
|
||
| execute(filters: SoloFilters): Promise<Either<Error, Attributes[]>> { | ||
| return this.soloCollection.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 Solo { | ||
| readonly id: number | ||
| readonly nome: string | ||
|
|
||
| private constructor(attributes: Attributes) { | ||
| this.id = attributes.id | ||
| this.nome = attributes.nome | ||
| } | ||
|
|
||
| static create(attributes: Attributes): Either<Error, Solo> { | ||
| if (!attributes.nome.trim()) { | ||
| return Either.left(new Error('Nome do solo não pode ser vazio')) | ||
| } | ||
|
|
||
| return Either.right(new Solo(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 './Solo' | ||
|
|
||
| export interface SoloOrder { | ||
| column: 'id' | 'nome' | ||
| direction: 'asc' | 'desc' | ||
| } | ||
|
|
||
| export interface SoloFilters { | ||
| nome?: string | ||
| order?: SoloOrder | ||
| } | ||
|
|
||
| export interface SoloCollection { | ||
| findAll(filters: SoloFilters): 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/solo/Solo' | ||
| import { SoloCollection, SoloFilters } from '@/domain/solo/SoloCollection' | ||
| import { Either } from '@/library/either/Either' | ||
|
|
||
| import { CollectionError } from './error/CollectionError' | ||
|
|
||
| interface Dependencies { | ||
| knex: Knex | ||
| } | ||
|
|
||
| export class SoloCollectionKnexAdapter implements SoloCollection { | ||
| private readonly knex: Knex | ||
|
|
||
| constructor(dependencies: Dependencies) { | ||
| this.knex = dependencies.knex | ||
| } | ||
|
|
||
| async findAll(filters: SoloFilters): Promise<Either<Error, Attributes[]>> { | ||
| try { | ||
| const query = this.knex<Attributes>('solos') | ||
| .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 solos', cause: error })) | ||
| } | ||
| } | ||
|
|
||
| async findById(id: number): Promise<Either<Error, Attributes | null>> { | ||
| try { | ||
| const solo = await this.knex<Attributes>('solos').select(['id', 'nome']).where({ id }).first() | ||
| return Either.right(solo ?? null) | ||
| } catch (error) { | ||
| return Either.left(new CollectionError({ message: 'Failed to find solo', 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 Solo = { id: number; nome: string } | ||
|
|
||
| const returning = ['id', 'nome'] as const | ||
|
|
||
| describe('GET /api/v2/solos/:soloId', () => { | ||
| const { agent, knex } = createTestApp() | ||
|
|
||
| afterAll(() => knex.destroy()) | ||
|
|
||
| test('retorna o registro encontrado', async () => { | ||
| const [solo] = await knex('solos') | ||
| .insert({ nome: 'XSOL Solo Encontrado' }) | ||
| .returning<Solo[]>(returning) | ||
|
|
||
| try { | ||
| const response = await agent.get(`/api/v2/solos/${solo.id}`).expect(200) | ||
| expect(response.body).toEqual({ id: solo.id, nome: solo.nome }) | ||
| } finally { | ||
| await knex('solos').where({ id: solo.id }).delete() | ||
| } | ||
| }) | ||
|
|
||
| test('retorna 404 para id inexistente', async () => { | ||
| const response = await agent.get('/api/v2/solos/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/solos/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.
Renomear para 3ª pessoa (
BuscaSoloController/BuscaSoloPorIdUseCase), no padrão deListaPaises*eBuscaFaseSucessionalUseCase.BuscarVegetacao*em development é drift e não deve ser copiado. Ajustar tambémsrc/domain/solo/BuscarSoloPorIdUseCase.tse os imports emsrc/application/solo/index.ts; apagar os arquivosBuscar*depois do rename.