-
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 branch 'main' of https://github.com/CtrI-Alt-Del/stocker
- Loading branch information
Showing
13 changed files
with
166 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { RegisterLocationController } from './register-location-controller' | ||
export { DeleteLocationsController } from './delete-locations-controller' | ||
export { DeleteLocationsController } from './delete-locations-controller' | ||
export { UpdateLocationController } from './update-location-controller' |
22 changes: 22 additions & 0 deletions
22
apps/server/src/api/controllers/locations/list-locations-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,22 @@ | ||
import type { IHttp } from '@stocker/core/interfaces' | ||
import { ListLocationsUseCase } from '@stocker/core/use-cases' | ||
|
||
import { locationsRepository } from '@/database' | ||
import { HTTP_STATUS_CODE } from '@stocker/core/constants' | ||
|
||
type RouteParams = { | ||
page: string | ||
} | ||
|
||
export class ListLocationsController { | ||
async handle(http: IHttp) { | ||
const { companyId } = await http.getUser() | ||
const { page } = http.getQueryParams<RouteParams>() | ||
const pageNumber = parseInt(page || '1', 10) | ||
|
||
const useCase = new ListLocationsUseCase(locationsRepository) | ||
const response = await useCase.execute({ page: pageNumber, companyId: companyId }) | ||
|
||
return http.send(response, HTTP_STATUS_CODE.ok) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
apps/server/src/api/controllers/locations/update-location-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,24 @@ | ||
import type { LocationDto } from "@stocker/core/dtos"; | ||
import type { IHttp } from "@stocker/core/interfaces"; | ||
|
||
import { locationsRepository } from "@/database"; | ||
import { UpdateLocationUseCase } from "@stocker/core/use-cases"; | ||
import { HTTP_STATUS_CODE } from "@stocker/core/constants"; | ||
import { User } from "@stocker/core/entities"; | ||
|
||
type RouteParams = { | ||
locationId: string | ||
} | ||
|
||
export class UpdateLocationController { | ||
async handle(http: IHttp) { | ||
const { locationId } = http.getRouteParams<RouteParams>() | ||
const {companyId} = await http.getUser() | ||
const locationDto = http.getBody<Partial<LocationDto>>() | ||
|
||
const useCase = new UpdateLocationUseCase(locationsRepository) | ||
await useCase.execute({ locationId, locationDto, companyId }) | ||
|
||
return http.send(companyId, HTTP_STATUS_CODE.ok) | ||
} | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export { RegisterLocationUseCase } from './register-location-use-case' | ||
export { DeleteLocationsUseCase } from './delete-locations-use-case' | ||
export { DeleteLocationsUseCase } from './delete-locations-use-case' | ||
export { UpdateLocationUseCase } from './update-location-use-case' | ||
export { ListLocationsUseCase } from './list-locations-use-case' |
20 changes: 20 additions & 0 deletions
20
packages/core/src/use-cases/locations/list-locations-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,20 @@ | ||
import type { ILocationsRepository } from '../../interfaces' | ||
import { PaginationResponse } from '../../responses' | ||
|
||
type Request = { | ||
page: number | ||
companyId: string | ||
} | ||
|
||
export class ListLocationsUseCase { | ||
private readonly locationsRepository: ILocationsRepository | ||
|
||
constructor(locationsRepository: ILocationsRepository) { | ||
this.locationsRepository = locationsRepository | ||
} | ||
|
||
async execute({ page, companyId }: Request) { | ||
const locations = await this.locationsRepository.findMany({ page, companyId }) | ||
return new PaginationResponse(locations) | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
packages/core/src/use-cases/locations/update-location-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,34 @@ | ||
import { string } from "zod"; | ||
import { Company } from "../../domain/entities"; | ||
import type { LocationDto } from "../../dtos"; | ||
import { ConflictError, NotFoundError } from "../../errors"; | ||
import type { ILocationsRepository } from "../../interfaces"; | ||
|
||
type Request = { | ||
locationDto: Partial<LocationDto> | ||
locationId: string | ||
companyId: string | ||
} | ||
|
||
export class UpdateLocationUseCase { | ||
private readonly locationsRepository: ILocationsRepository | ||
|
||
constructor(locationsRepository: ILocationsRepository) { | ||
this.locationsRepository = locationsRepository | ||
} | ||
async execute({ locationDto, locationId, companyId }: Request) { | ||
const location = await this.locationsRepository.findById(locationId) | ||
|
||
if (!location) { | ||
throw new NotFoundError('Localização não encontrada') | ||
} | ||
if (locationDto.name) { | ||
const locationName = await this.locationsRepository.findByName(locationDto.name) | ||
if (locationName !== null && locationName.dto.companyId === companyId) { | ||
throw new ConflictError('Nome já em uso por outra localização no sistema') | ||
} | ||
} | ||
const updatedLocation = location.update(locationDto) | ||
await this.locationsRepository.update(updatedLocation, locationId) | ||
} | ||
} |