-
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 #1 from fabianoSL1/develop
restaurant use cases
- Loading branch information
Showing
36 changed files
with
594 additions
and
105 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} **/ | ||
export default { | ||
preset: 'ts-jest', | ||
testEnvironment: "node", | ||
transform: { | ||
"^.+.tsx?$": ["ts-jest",{}], | ||
}, | ||
moduleNameMapper: { | ||
'^@/(.*)$': '<rootDir>/src/$1', // Mapeamento para suportar aliases | ||
'^@/(.*)$': '<rootDir>/src/$1', | ||
'^tests/(.*)$': '<rootDir>/tests/$1', | ||
}, | ||
collectCoverage: true, | ||
coverageDirectory: "coverage", | ||
coverageReporters: ["lcov", "text"] | ||
}; |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,8 @@ | ||
export type AddressDto = { | ||
street: string; | ||
number: string | null; | ||
state: string; | ||
city: string; | ||
neighborhood: string; | ||
zipcode: string; | ||
}; |
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,8 @@ | ||
import type { AddressDto } from "@/restaurant/application/dtos/address.dto"; | ||
import type { CreateScheduleDto } from "@/schedule/application/dtos/create-schedule.dto"; | ||
|
||
export type CreateRestaurantRequestDto = { | ||
name: string; | ||
address: AddressDto; | ||
schedules: CreateScheduleDto[]; | ||
}; |
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,7 @@ | ||
import type { RestaurantResponseDto } from "@/restaurant/application/dtos/restaurant.dto"; | ||
|
||
type RestaurantItem = Omit<RestaurantResponseDto, "schedules" | "address"> & { | ||
address: string; | ||
}; | ||
|
||
export type ListRestaurantResponseDto = Array<RestaurantItem>; |
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 @@ | ||
import type { AddressDto } from "@/restaurant/application/dtos/address.dto"; | ||
import type { ScheduleDto } from "@/schedule/application/dtos/schedule.dto"; | ||
|
||
export type RestaurantResponseDto = { | ||
id: string; | ||
name: string; | ||
picture: string | null; | ||
address: AddressDto; | ||
schedules: Array<ScheduleDto>; | ||
}; |
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,8 @@ | ||
import type { AddressDto } from "@/restaurant/application/dtos/address.dto"; | ||
import type { Restaurant } from "@/restaurant/domain/restaurant.entity"; | ||
|
||
export type UpdateRestaurantRequestDto = Partial< | ||
Omit<Restaurant, "id" | "address"> & { | ||
address: AddressDto; | ||
} | ||
>; |
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,47 @@ | ||
import type { AddressDto } from "@/restaurant/application/dtos/address.dto"; | ||
import type { CreateRestaurantRequestDto } from "@/restaurant/application/dtos/create-restaurant.dto"; | ||
import type { RestaurantResponseDto } from "@/restaurant/application/dtos/restaurant.dto"; | ||
import { Address } from "@/restaurant/domain/address"; | ||
import type { RestaurantRepository } from "@/restaurant/domain/restaurant.repository"; | ||
import type { ScheduleRestaurantRepository } from "@/schedule/domain/scheduleRestaurant.repository"; | ||
|
||
export class CreateRestaurantUseCase { | ||
constructor( | ||
private readonly restaurantRepository: RestaurantRepository, | ||
private readonly scheduleRepository: ScheduleRestaurantRepository, | ||
) {} | ||
|
||
async execute( | ||
dto: CreateRestaurantRequestDto, | ||
): Promise<RestaurantResponseDto> { | ||
const address = this.parseAddress(dto.address); | ||
const resturant = await this.restaurantRepository.create(dto.name, address); | ||
const schedules = await this.scheduleRepository.createBach( | ||
resturant.id, | ||
dto.schedules, | ||
); | ||
|
||
return { | ||
...resturant, | ||
schedules, | ||
}; | ||
} | ||
|
||
private parseAddress({ | ||
street, | ||
number, | ||
neighborhood, | ||
city, | ||
state, | ||
zipcode, | ||
}: AddressDto) { | ||
return Address.builder() | ||
.withStreet(street) | ||
.withNumber(number) | ||
.withNeighborhood(neighborhood) | ||
.withCity(city) | ||
.withState(state) | ||
.withZipcode(zipcode) | ||
.build(); | ||
} | ||
} |
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,9 @@ | ||
import type { RestaurantRepository } from "@/restaurant/domain/restaurant.repository"; | ||
|
||
export class DeleteRestaurantUseCase { | ||
constructor(private readonly restaurantRepository: RestaurantRepository) {} | ||
|
||
async execute(restaurantId: string) { | ||
await this.restaurantRepository.destroy(restaurantId); | ||
} | ||
} |
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,26 @@ | ||
import type { RestaurantResponseDto } from "@/restaurant/application/dtos/restaurant.dto"; | ||
import type { RestaurantRepository } from "@/restaurant/domain/restaurant.repository"; | ||
import type { ScheduleRestaurantRepository } from "@/schedule/domain/scheduleRestaurant.repository"; | ||
|
||
export class GetRestaurantUseCase { | ||
constructor( | ||
private readonly restaurantRepository: RestaurantRepository, | ||
private readonly scheduleRepository: ScheduleRestaurantRepository, | ||
) {} | ||
|
||
async execute(restaurantId: string): Promise<RestaurantResponseDto> { | ||
const restaurant = await this.restaurantRepository.get(restaurantId); | ||
|
||
if (!restaurant) { | ||
throw new Error("restaurant not found"); | ||
} | ||
|
||
const schedules = | ||
await this.scheduleRepository.listByRestaurantId(restaurantId); | ||
|
||
return { | ||
...restaurant, | ||
schedules, | ||
}; | ||
} | ||
} |
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,17 @@ | ||
import type { ListRestaurantResponseDto } from "@/restaurant/application/dtos/list-restaurant.dto"; | ||
import type { RestaurantRepository } from "@/restaurant/domain/restaurant.repository"; | ||
|
||
export class ListRestaurantUseCase { | ||
constructor(private readonly restaurantRepository: RestaurantRepository) {} | ||
|
||
async execute(): Promise<ListRestaurantResponseDto> { | ||
const resturants = await this.restaurantRepository.list(); | ||
|
||
return resturants.map(({ id, name, picture, address }) => ({ | ||
id, | ||
name, | ||
picture, | ||
address: address.toString(), | ||
})); | ||
} | ||
} |
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 type { RestaurantResponseDto } from "@/restaurant/application/dtos/restaurant.dto"; | ||
import type { UpdateRestaurantRequestDto } from "@/restaurant/application/dtos/update-restaurant.dto"; | ||
import type { RestaurantRepository } from "@/restaurant/domain/restaurant.repository"; | ||
import type { ScheduleRestaurantRepository } from "@/schedule/domain/scheduleRestaurant.repository"; | ||
|
||
export class UpdateRestaurantUseCase { | ||
constructor( | ||
private readonly restaurantRepository: RestaurantRepository, | ||
private readonly scheduleRepository: ScheduleRestaurantRepository, | ||
) {} | ||
|
||
async execute( | ||
restaurantId: string, | ||
update: UpdateRestaurantRequestDto, | ||
): Promise<RestaurantResponseDto> { | ||
const restaurant = await this.restaurantRepository.get(restaurantId); | ||
|
||
if (!restaurant) { | ||
throw new Error("resturant not found"); | ||
} | ||
|
||
Object.assign(restaurant, update); | ||
|
||
await this.restaurantRepository.save(restaurant); | ||
|
||
const schedules = | ||
await this.scheduleRepository.listByRestaurantId(restaurantId); | ||
|
||
return { | ||
...restaurant, | ||
schedules, | ||
}; | ||
} | ||
} |
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,68 @@ | ||
export class Address { | ||
constructor( | ||
public readonly street: string, | ||
public readonly number: string | null, | ||
public readonly state: string, | ||
public readonly city: string, | ||
public readonly neighborhood: string, | ||
public readonly zipcode: string, | ||
) {} | ||
|
||
static builder() { | ||
return new AddressBuilder(); | ||
} | ||
|
||
toString() { | ||
return `${this.street}, ${this.number ?? "SN"} - ${this.neighborhood}, ${this.city} - ${this.state}, ${this.zipcode}`; | ||
} | ||
} | ||
|
||
class AddressBuilder { | ||
private street!: string; | ||
private number!: string | null; | ||
private state!: string; | ||
private city!: string; | ||
private neighborhood!: string; | ||
private zipcode!: string; | ||
|
||
withStreet(street: string) { | ||
this.street = street; | ||
return this; | ||
} | ||
|
||
withNumber(number: string | null) { | ||
this.number = number; | ||
return this; | ||
} | ||
|
||
withState(state: string) { | ||
this.state = state; | ||
return this; | ||
} | ||
|
||
withCity(city: string) { | ||
this.city = city; | ||
return this; | ||
} | ||
|
||
withNeighborhood(neighborhood: string) { | ||
this.neighborhood = neighborhood; | ||
return this; | ||
} | ||
|
||
withZipcode(zipcode: string) { | ||
this.zipcode = zipcode; | ||
return this; | ||
} | ||
|
||
build() { | ||
return new Address( | ||
this.street, | ||
this.number, | ||
this.state, | ||
this.city, | ||
this.neighborhood, | ||
this.zipcode, | ||
); | ||
} | ||
} |
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,14 @@ | ||
import type { Address } from "@/restaurant/domain/address"; | ||
|
||
export class Restaurant { | ||
constructor( | ||
public readonly id: string, | ||
public readonly name: string, | ||
public readonly picture: string | null, | ||
public readonly address: Address, | ||
) { | ||
if (name === "") { | ||
throw new Error("o nome não pode estar vazio"); | ||
} | ||
} | ||
} |
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 @@ | ||
import type { Address } from "@/restaurant/domain/address"; | ||
import type { Restaurant } from "@/restaurant/domain/restaurant.entity"; | ||
|
||
export interface RestaurantRepository { | ||
create(name: string, address: Address): Promise<Restaurant>; | ||
save(resturant: Restaurant): Promise<Restaurant>; | ||
list(): Promise<Restaurant[]>; | ||
get(id: string): Promise<Restaurant | null>; | ||
destroy(id: string): Promise<void>; | ||
} |
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,5 @@ | ||
export type CreateScheduleDto = { | ||
begin: string; | ||
end: string; | ||
day: string; | ||
}; |
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,6 @@ | ||
export type ScheduleDto = { | ||
id: string; | ||
begin: string; | ||
end: string; | ||
day: string; | ||
}; |
Oops, something went wrong.