-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #258 from CtrI-Alt-Del/getSuppliers
Get Suppliers
- Loading branch information
Showing
6 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
apps/server/src/api/controllers/suppliers/get-supplier-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,18 @@ | ||
import type { IHttp } from '@stocker/core/interfaces' | ||
import { GetSupplierUseCase } from '@stocker/core/use-cases' | ||
|
||
import { suppliersRepository } from '@/database' | ||
|
||
type RouteParams = { | ||
supplierId: string | ||
} | ||
|
||
export class GetSupplierController { | ||
async handle(http: IHttp) { | ||
const { supplierId } = http.getRouteParams<RouteParams>() | ||
const useCase = new GetSupplierUseCase(suppliersRepository) | ||
const supplierDto = await useCase.execute({ supplierId }) | ||
|
||
return http.send(supplierDto) | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
packages/core/src/use-cases/suppliers/get-supplier-use-case.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,23 @@ | ||
import type { ISuppliersRepository } from '../../interfaces' | ||
import { NotFoundError } from '../../errors' | ||
|
||
type Request = { | ||
supplierId: string | ||
} | ||
|
||
export class GetSupplierUseCase { | ||
private readonly suppliersRepository: ISuppliersRepository | ||
constructor(suppliersRepository: ISuppliersRepository) { | ||
this.suppliersRepository = suppliersRepository | ||
} | ||
|
||
async execute({ supplierId }: Request) { | ||
const supplier = await this.suppliersRepository.findById(supplierId) | ||
|
||
if (!supplier) { | ||
throw new NotFoundError('Fornecedor não encontrado') | ||
} | ||
|
||
return supplier.dto | ||
} | ||
} |
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
32 changes: 32 additions & 0 deletions
32
packages/core/src/use-cases/suppliers/tests/get-supplier-use-case.test.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,32 @@ | ||
import { beforeEach, describe, expect, it } from 'vitest' | ||
import { GetSupplierUseCase } from '../get-supplier-use-case' | ||
import { SuppliersRepositoryMock } from '../../../../__tests__/mocks/repositories' | ||
import { NotAllowedError, NotFoundError } from '../../../errors' | ||
import { SuppliersFaker } from '../../../../__tests__/fakers' | ||
|
||
let useCase: GetSupplierUseCase | ||
let suppliersRepository: SuppliersRepositoryMock | ||
|
||
describe('Get supplier use case', () => { | ||
beforeEach(() => { | ||
suppliersRepository = new SuppliersRepositoryMock() | ||
useCase = new GetSupplierUseCase(suppliersRepository) | ||
}) | ||
|
||
it('should not get a supplier if the supplier does not exist', async () => { | ||
const fakeSupplier = SuppliersFaker.fake() | ||
|
||
expect(async () => { | ||
await useCase.execute({ supplierId: fakeSupplier.id }) | ||
}).rejects.toThrowError(NotFoundError) | ||
}) | ||
|
||
it('should get a supplier', async () => { | ||
const fakeSupplier = SuppliersFaker.fake() | ||
await suppliersRepository.add(fakeSupplier) | ||
|
||
const supplier = await useCase.execute({ supplierId: fakeSupplier.id }) | ||
|
||
expect(supplier).toEqual(fakeSupplier.dto) | ||
}) | ||
}) |