Skip to content

Commit

Permalink
Merge pull request #1 from fabianoSL1/develop
Browse files Browse the repository at this point in the history
restaurant use cases
  • Loading branch information
fabianosl1 authored Jan 10, 2025
2 parents 1191a1d + 5092437 commit ef40cff
Show file tree
Hide file tree
Showing 36 changed files with 594 additions and 105 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
run: npm ci

- name: run tests
run: npm run test
run: npm run test:cov

- name: SonarQube Scan
uses: SonarSource/sonarqube-scan-action@v4
Expand Down
5 changes: 3 additions & 2 deletions jest.config.js
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"]
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"scripts": {
"dev": "tsx watch src/index.ts",
"test": "jest",
"test:cov": "jest --coverage",
"sonar": "sonar-scanner",
"prepare": "husky",
"lint": "biome lint ./src/*",
Expand Down
10 changes: 0 additions & 10 deletions src/domain/entities/address.entity.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/domain/entities/product.entity.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/domain/entities/promotion.entity.ts

This file was deleted.

14 changes: 0 additions & 14 deletions src/domain/entities/restaurant.entity.ts

This file was deleted.

13 changes: 0 additions & 13 deletions src/domain/entities/schedule.entity.ts

This file was deleted.

8 changes: 8 additions & 0 deletions src/restaurant/application/dtos/address.dto.ts
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;
};
8 changes: 8 additions & 0 deletions src/restaurant/application/dtos/create-restaurant.dto.ts
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[];
};
7 changes: 7 additions & 0 deletions src/restaurant/application/dtos/list-restaurant.dto.ts
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>;
10 changes: 10 additions & 0 deletions src/restaurant/application/dtos/restaurant.dto.ts
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>;
};
8 changes: 8 additions & 0 deletions src/restaurant/application/dtos/update-restaurant.dto.ts
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;
}
>;
47 changes: 47 additions & 0 deletions src/restaurant/application/useCases/create-restaurant.ts
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();
}
}
9 changes: 9 additions & 0 deletions src/restaurant/application/useCases/delete-restaurant.ts
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);
}
}
26 changes: 26 additions & 0 deletions src/restaurant/application/useCases/get-restaurant.ts
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,
};
}
}
17 changes: 17 additions & 0 deletions src/restaurant/application/useCases/list-restaurant.ts
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(),
}));
}
}
34 changes: 34 additions & 0 deletions src/restaurant/application/useCases/update-restaurant.ts
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,
};
}
}
68 changes: 68 additions & 0 deletions src/restaurant/domain/address.ts
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,
);
}
}
14 changes: 14 additions & 0 deletions src/restaurant/domain/restaurant.entity.ts
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");
}
}
}
10 changes: 10 additions & 0 deletions src/restaurant/domain/restaurant.repository.ts
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>;
}
5 changes: 5 additions & 0 deletions src/schedule/application/dtos/create-schedule.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type CreateScheduleDto = {
begin: string;
end: string;
day: string;
};
6 changes: 6 additions & 0 deletions src/schedule/application/dtos/schedule.dto.ts
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;
};
Loading

0 comments on commit ef40cff

Please sign in to comment.