Skip to content

Commit

Permalink
Merge pull request #69 from werlleyg/refactor/change-usecase-structure
Browse files Browse the repository at this point in the history
Refactor/change usecase structure
  • Loading branch information
werlleyg authored Dec 6, 2024
2 parents e92ccb7 + 1d4e03c commit 6b6f8b9
Show file tree
Hide file tree
Showing 23 changed files with 87 additions and 87 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ The project was built adapting clean arch concepts to the context of Next.js. It

- **Domain:** Concentrates important information for business rules, such as entities, enums, errors, service, providers, protocols, and usecases contracts;

- **Infrastructure:** The implementation of service contracts, proviers, protocols and usecases is carried out;
- **Infrastructure:** The implementation of service contracts, proviers and protocols is carried out;

- **Main:** In this layer, the factory pattern is carried out to instantiate the elements created in the infrastructure, in addition to aggregating project configurations such as environment variables and build settings;

Expand Down
Binary file modified docs/images/architecture-diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "quefilme",
"version": "0.0.6",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
Expand Down
14 changes: 7 additions & 7 deletions src/domain/usecases/getMovie.usecase.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { MovieEntity } from "../entities";
import { MoviesRepository } from "@/domain/repositories";
import { GetMovieUsecase } from "@/domain/usecases/interfaces";

export interface GetMovieUsecase {
exec: (params: GetMovieUsecase.Params) => Promise<GetMovieUsecase.Model>;
}
export class GetMovieUsecaseImpl implements GetMovieUsecase {
constructor(private readonly repository: MoviesRepository) {}

export namespace GetMovieUsecase {
export type Params = string;
export type Model = MovieEntity;
async exec(imdbID: GetMovieUsecase.Params): Promise<GetMovieUsecase.Model> {
return await this.repository.getMovie(imdbID);
}
}
34 changes: 26 additions & 8 deletions src/domain/usecases/getMovieSuggestion.usecase.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
import { ListMoviesEntity, MovieEntity } from "../entities";
import { UnexpectedError } from "@/domain/errors";
import { MoviesRepository } from "@/domain/repositories";
import { AiService } from "@/domain/services";
import { GetMovieSuggestionUsecase } from "@/domain/usecases/interfaces";

export interface GetMovieSuggestionUsecase {
exec: (
export class GetMovieSuggestionUsecaseImpl
implements GetMovieSuggestionUsecase
{
constructor(
private readonly repository: MoviesRepository,
private readonly service: AiService,
) {}
async exec(
params: GetMovieSuggestionUsecase.Params,
) => Promise<GetMovieSuggestionUsecase.Model>;
}
): Promise<GetMovieSuggestionUsecase.Model> {
const listOfMovies = params.movies.map((movie) => movie.title).join(", ");

const prompt = `Seja direto e siga exatamente o exemplo proposto a seguir após os dois pontos, me indique apenas um filme baseado na lista ${listOfMovies}, mas não pode ser nenhum dessa lista e nem repetir a sugestão anterior, seja criativo na escolha mas retorne algo que combine com os itens de lista, e coloque seu imdb CORRETO no final, ex: Cidade de Deus - tt0317248`;

const promptResponse = await this.service.generateResponse(prompt);

const parts = promptResponse?.split(" - ");
if (!parts || parts?.length < 2) {
throw new UnexpectedError();
}
const suggestMovieImdb = parts[1];

export namespace GetMovieSuggestionUsecase {
export type Params = ListMoviesEntity;
export type Model = MovieEntity;
return await this.repository.getMovie(suggestMovieImdb);
}
}
14 changes: 7 additions & 7 deletions src/domain/usecases/getMovies.usecase.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ListMoviesEntity } from "../entities";
import { MoviesRepository } from "@/domain/repositories";
import { GetMoviesUsecase } from "@/domain/usecases/interfaces";

export interface GetMoviesUsecase {
exec: (params: GetMoviesUsecase.Params) => Promise<GetMoviesUsecase.Model>;
}
export class GetMoviesUsecaseImpl implements GetMoviesUsecase {
constructor(private readonly repository: MoviesRepository) {}

export namespace GetMoviesUsecase {
export type Params = string;
export type Model = ListMoviesEntity;
async exec(title: GetMoviesUsecase.Params): Promise<GetMoviesUsecase.Model> {
return await this.repository.getMovieList(title.trim().toLowerCase());
}
}
1 change: 0 additions & 1 deletion src/domain/usecases/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from "./getMovie.usecase";
export * from "./getMovies.usecase";
export * from "./getMovieSuggestion.usecase";
10 changes: 10 additions & 0 deletions src/domain/usecases/interfaces/getMovie.usecase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { MovieEntity } from "../../entities";

export interface GetMovieUsecase {
exec: (params: GetMovieUsecase.Params) => Promise<GetMovieUsecase.Model>;
}

export namespace GetMovieUsecase {
export type Params = string;
export type Model = MovieEntity;
}
12 changes: 12 additions & 0 deletions src/domain/usecases/interfaces/getMovieSuggestion.usecase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ListMoviesEntity, MovieEntity } from "../../entities/";

export interface GetMovieSuggestionUsecase {
exec: (
params: GetMovieSuggestionUsecase.Params,
) => Promise<GetMovieSuggestionUsecase.Model>;
}

export namespace GetMovieSuggestionUsecase {
export type Params = ListMoviesEntity;
export type Model = MovieEntity;
}
10 changes: 10 additions & 0 deletions src/domain/usecases/interfaces/getMovies.usecase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ListMoviesEntity } from "../../entities";

export interface GetMoviesUsecase {
exec: (params: GetMoviesUsecase.Params) => Promise<GetMoviesUsecase.Model>;
}

export namespace GetMoviesUsecase {
export type Params = string;
export type Model = ListMoviesEntity;
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./getMovie.usecase";
export * from "./getMovies.usecase";
export * from "./getMovieSuggestion.usecase";
10 changes: 0 additions & 10 deletions src/infrastructure/usecases/getMovie.usecase.ts

This file was deleted.

30 changes: 0 additions & 30 deletions src/infrastructure/usecases/getMovieSuggestion.usecase.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/infrastructure/usecases/getMovies.usecase.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/factories/usecases/getMovie.factory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GetMovieUsecase } from "@/domain/usecases";
import { GetMovieUsecase } from "@/domain/usecases/interfaces";
import { makeMoviesRepository } from "../repositories/movies.factory";
import { GetMovieUsecaseImpl } from "@/infrastructure/usecases";
import { GetMovieUsecaseImpl } from "@/domain/usecases";

export const makeGetMovieUsecase = (): GetMovieUsecase =>
new GetMovieUsecaseImpl(makeMoviesRepository());
4 changes: 2 additions & 2 deletions src/main/factories/usecases/getMovieSuggestion.factory.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { GetMovieSuggestionUsecase } from "@/domain/usecases";
import { GetMovieSuggestionUsecase } from "@/domain/usecases/interfaces";

import { makeMoviesRepository } from "../repositories/movies.factory";
import { makeAiService } from "../services/ai.factory";
import { GetMovieSuggestionUsecaseImpl } from "@/infrastructure/usecases/getMovieSuggestion.usecase";
import { GetMovieSuggestionUsecaseImpl } from "@/domain/usecases/getMovieSuggestion.usecase";

export const makeGetMovieSuggestionUsecase = (): GetMovieSuggestionUsecase =>
new GetMovieSuggestionUsecaseImpl(makeMoviesRepository(), makeAiService());
4 changes: 2 additions & 2 deletions src/main/factories/usecases/getMovies.factory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GetMoviesUsecase } from "@/domain/usecases";
import { GetMoviesUsecase } from "@/domain/usecases/interfaces";
import { makeMoviesRepository } from "../repositories/movies.factory";
import { GetMoviesUsecaseImpl } from "@/infrastructure/usecases";
import { GetMoviesUsecaseImpl } from "@/domain/usecases";

export const makeGetMoviesUsecase = (): GetMoviesUsecase =>
new GetMoviesUsecaseImpl(makeMoviesRepository());
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { ListMoviesEntity } from "@/domain/entities";
import { UnexpectedError } from "@/domain/errors";
import { AiServiceImpl } from "@/infrastructure/services";
import { GetMovieSuggestionUsecaseImpl } from "@/infrastructure/usecases/getMovieSuggestion.usecase";
import { GetMovieSuggestionUsecaseImpl } from "@/domain/usecases/getMovieSuggestion.usecase";

type SutTypes = {
repository: ReturnType<typeof mock<MoviesRepositoryImpl>>;
Expand Down
2 changes: 1 addition & 1 deletion tests/infrastructure/usecases/getMovieUsecaseImpl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { MoviesRepositoryImpl } from "@/infrastructure/repositories";

import { mock } from "jest-mock-extended";
import { movieMock } from "../mocks/getMovieUsecaseImpl.mock";
import { GetMovieUsecaseImpl } from "@/infrastructure/usecases";
import { GetMovieUsecaseImpl } from "@/domain/usecases";

type SutTypes = {
movieRepository: ReturnType<typeof mock<MoviesRepositoryImpl>>;
Expand Down
2 changes: 1 addition & 1 deletion tests/infrastructure/usecases/getMoviesUsecaseImpl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { MoviesRepositoryImpl } from "@/infrastructure/repositories";

import { mock } from "jest-mock-extended";
import { listMoviesMock, movieMock } from "../mocks/getMovieUsecaseImpl.mock";
import { GetMoviesUsecaseImpl } from "@/infrastructure/usecases";
import { GetMoviesUsecaseImpl } from "@/domain/usecases";

type SutTypes = {
movieRepository: ReturnType<typeof mock<MoviesRepositoryImpl>>;
Expand Down
2 changes: 1 addition & 1 deletion tests/main/factories/usecases/getMovie.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { MoviesRepositoryImpl } from "@/infrastructure/repositories";
import { MoviesRepository } from "@/domain/repositories";
import { GetMovieUsecase } from "@/domain/usecases";
import { AxiosHttpClient } from "@/infrastructure/http";
import { GetMovieUsecaseImpl } from "@/infrastructure/usecases";
import { GetMovieUsecaseImpl } from "@/domain/usecases";

type SutTypes = {
axiosHttpClient: AxiosHttpClient;
Expand Down
2 changes: 1 addition & 1 deletion tests/main/factories/usecases/getMovieSuggestion.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { GetMovieSuggestionUsecase, GetMoviesUsecase } from "@/domain/usecases";
import { AxiosHttpClient } from "@/infrastructure/http";
import { Environment } from "@/main/config";
import { AiServiceImpl } from "@/infrastructure/services";
import { GetMovieSuggestionUsecaseImpl } from "@/infrastructure/usecases/getMovieSuggestion.usecase";
import { GetMovieSuggestionUsecaseImpl } from "@/domain/usecases/getMovieSuggestion.usecase";

type SutTypes = {
axiosHttpClient: AxiosHttpClient;
Expand Down
2 changes: 1 addition & 1 deletion tests/main/factories/usecases/getMovies.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { MoviesRepositoryImpl } from "@/infrastructure/repositories";
import { MoviesRepository } from "@/domain/repositories";
import { GetMoviesUsecase } from "@/domain/usecases";
import { AxiosHttpClient } from "@/infrastructure/http";
import { GetMoviesUsecaseImpl } from "@/infrastructure/usecases";
import { GetMoviesUsecaseImpl } from "@/domain/usecases";

type SutTypes = {
axiosHttpClient: AxiosHttpClient;
Expand Down

0 comments on commit 6b6f8b9

Please sign in to comment.