Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adicionar funcionalidades para o Book Club #54

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import ChatService from "../../../domain/service/chatService";
import SaveBookSuggestionForBookClubUseCaseInput from "./saveBookSuggestionForBookClubUseCaseInput";
import BookSuggestionsRepository from "../../../domain/repository/bookSuggestionsRepository";
import BookSuggestion from "../../../domain/entity/bookSuggestion";

export default class SaveBookSuggestionForBookClubUseCase {
private bookSuggestionsRepository: BookSuggestionsRepository;

private chatService: ChatService;

constructor({
chatService,
bookSuggestionsRepository,
}: {
chatService: ChatService;
bookSuggestionsRepository: BookSuggestionsRepository;
}) {
this.chatService = chatService;
this.bookSuggestionsRepository = bookSuggestionsRepository;
}

async execute({ title, link, channelId }: SaveBookSuggestionForBookClubUseCaseInput): Promise<void> {
const bookSuggestionAlreadyExists = this.bookSuggestionsRepository.findByTitle(title);
if (bookSuggestionAlreadyExists) {
this.chatService.sendMessageToChannel(
`Infelizmente o conteúdo **"${title}"** já tinha sido sugerido.`,
channelId
);
return;
}

this.bookSuggestionsRepository.add(new BookSuggestion({ title, link }));
this.chatService.sendMessageToChannel(`A sugestão **"${title}"** foi enviada.`, channelId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default interface SaveBookSuggestionForBookClubUseCaseInput {
title: string;
link: string;
channelId: string;
}
18 changes: 18 additions & 0 deletions domain/entity/bookSuggestion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export default class BookSuggestion {
private readonly title: string;

private readonly link: string;

constructor({ title, link }: { title: string; link: string }) {
this.title = title;
this.link = link;
}

getTitle(): string {
return this.title;
}

getLink(): string {
return this.link;
}
}
6 changes: 6 additions & 0 deletions domain/repository/bookSuggestionsRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import BookSuggestion from "../entity/bookSuggestion";

export default interface BookSuggestionsRepository {
add(bookSuggestion: BookSuggestion): void;
findByTitle(title: string): BookSuggestion | null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { vi, describe, it, expect } from "vitest";
import SaveBookSuggestionForBookClubUseCase from "../../../application/usecases/saveBookSuggestionForBookClubUseCase/saveBookSuggestionForBookClubUseCase";
import BookSuggestion from "../../../domain/entity/bookSuggestion";

describe("send book suggestion for book club use case", () => {
it("should save in book suggestions repository the suggested book", async () => {
const bookSuggestionsRepository = {
add: vi.fn(),
findByTitle: vi.fn(),
};

const chatService = {
sendMessageToChannel: vi.fn(),
};

await new SaveBookSuggestionForBookClubUseCase({
bookSuggestionsRepository,
chatService,
}).execute({
title: "The Lord of the Rings",
link: "https://www.amazon.com/Lord-Rings-1-3-J-R-R-Tolkien/dp/0618260307",
channelId: "855861944930402342",
});

expect(bookSuggestionsRepository.add).toHaveBeenCalledWith({
title: "The Lord of the Rings",
link: "https://www.amazon.com/Lord-Rings-1-3-J-R-R-Tolkien/dp/0618260307",
});
});

it("should send a message from chatService thanking the user", async () => {
const bookSuggestionsRepository = {
add: vi.fn(),
findByTitle: vi.fn(),
};

const chatService = {
sendMessageToChannel: vi.fn(),
};

const spy = vi.spyOn(chatService, "sendMessageToChannel");

await new SaveBookSuggestionForBookClubUseCase({
bookSuggestionsRepository,
chatService,
}).execute({
title: "The Lord of the Rings",
link: "https://www.amazon.com/Lord-Rings-1-3-J-R-R-Tolkien/dp/0618260307",
channelId: "855861944930402342",
});

expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith('A sugestão **"The Lord of the Rings"** foi enviada.', "855861944930402342");
});

it("should send a message from chatService in case the book was already suggested", async () => {
const bookSuggestionsRepository = {
add: vi.fn(),
findByTitle: () =>
new BookSuggestion({
title: "The Lord of the Rings",
link: "https://www.amazon.com/Lord-Rings-1-3-J-R-R-Tolkien/dp/0618260307",
}),
};

const chatService = {
sendMessageToChannel: vi.fn(),
};

const spy = vi.spyOn(chatService, "sendMessageToChannel");

await new SaveBookSuggestionForBookClubUseCase({
bookSuggestionsRepository,
chatService,
}).execute({
title: "The Lord of the Rings",
link: "https://www.amazon.com/Lord-Rings-1-3-J-R-R-Tolkien/dp/0618260307",
channelId: "855861944930402342",
});

expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith(
'Infelizmente o conteúdo **"The Lord of the Rings"** já tinha sido sugerido.',
"855861944930402342"
);
});
});