diff --git a/src/modules/decks/decks.controller.ts b/src/modules/decks/decks.controller.ts index 0b5fa12..045ee24 100644 --- a/src/modules/decks/decks.controller.ts +++ b/src/modules/decks/decks.controller.ts @@ -37,7 +37,12 @@ import { Card, CardWithGrade, PaginatedCardsWithGrade } from '../cards/entities/ import { CreateDeckDto, GetAllDecksDto, UpdateDeckDto } from './dto' import { GetRandomCardDto } from './dto/get-random-card.dto' -import { Deck, PaginatedDecks, PaginatedDecksWithMaxCardsCount } from './entities/deck.entity' +import { + Deck, + DeckWithFavorites, + PaginatedDecks, + PaginatedDecksWithMaxCardsCount, +} from './entities/deck.entity' import { MinMaxCards } from './entities/min-max-cards.entity' import { AddDeckToFavoritesCommand, @@ -130,8 +135,8 @@ export class DecksController { @UseGuards(JwtAuthGuard) @Get(':id') @ApiBearerAuth() - findOne(@Param('id') id: string): Promise { - return this.commandBus.execute(new GetDeckByIdCommand(id)) + findOne(@Param('id') id: string, @Req() req): Promise { + return this.commandBus.execute(new GetDeckByIdCommand(id, req.user.id)) } @ApiConsumes('multipart/form-data') @@ -259,7 +264,7 @@ export class DecksController { description: 'Add deck to favorites', summary: 'Add deck to favorites', }) - async addToFavorites(@Req() req, @Param('id') deckId: string): Promise { + async addToFavorites(@Req() req, @Param('id') deckId: string): Promise { return await this.commandBus.execute(new AddDeckToFavoritesCommand(req.user.id, deckId)) } @@ -267,13 +272,13 @@ export class DecksController { @UseGuards(JwtAuthGuard) @ApiUnauthorizedResponse({ description: 'Unauthorized' }) @HttpCode(HttpStatus.NO_CONTENT) - @ApiNoContentResponse({ description: 'Added to favorites' }) + @ApiNoContentResponse({ description: 'Removed from favorites' }) @Delete(':id/favorite') @ApiOperation({ - description: 'Add deck to favorites', - summary: 'Add deck to favorites', + description: 'Remove deck from favorites', + summary: 'Remove deck from favorites', }) - async removeFromFavorites(@Req() req, @Param('id') deckId: string): Promise { + async removeFromFavorites(@Req() req, @Param('id') deckId: string): Promise { return await this.commandBus.execute(new RemoveDeckFromFavoritesCommand(req.user.id, deckId)) } } diff --git a/src/modules/decks/entities/deck.entity.ts b/src/modules/decks/entities/deck.entity.ts index c34ed5c..16e491f 100644 --- a/src/modules/decks/entities/deck.entity.ts +++ b/src/modules/decks/entities/deck.entity.ts @@ -15,18 +15,26 @@ export class DeckWithAuthor extends Deck { author: DeckAuthor } +export class DeckWithAuthorAndFavorites extends DeckWithAuthor { + isFavorite: boolean +} + +export class DeckWithFavorites extends Deck { + isFavorite: boolean +} + export class DeckAuthor { id: string name: string } export class PaginatedDecksWithMaxCardsCount { - items: DeckWithAuthor[] + items: DeckWithAuthorAndFavorites[] pagination: Pagination maxCardsCount: number } export class PaginatedDecks { - items: DeckWithAuthor[] + items: DeckWithAuthorAndFavorites[] pagination: Pagination } diff --git a/src/modules/decks/infrastructure/decks.repository.ts b/src/modules/decks/infrastructure/decks.repository.ts index 6a03774..408c002 100644 --- a/src/modules/decks/infrastructure/decks.repository.ts +++ b/src/modules/decks/infrastructure/decks.repository.ts @@ -263,7 +263,7 @@ export class DecksRepository { } } - public async findDeckById(id: string): Promise { + public async findDeckById(id: string, userId: string): Promise { try { const result = await this.prisma.deck.findUnique({ where: { @@ -275,10 +275,20 @@ export class DecksRepository { card: true, }, }, + favoritedBy: { + where: { + userId, + }, + }, }, }) - return omit({ ...result, cardsCount: result._count.card }, ['_count']) + const isFavorite = result.favoritedBy.length > 0 + + return omit({ ...result, cardsCount: result._count.card, isFavorite }, [ + '_count', + 'favoritedBy', + ]) } catch (e) { this.logger.error(e?.message) throw new InternalServerErrorException(e?.message) diff --git a/src/modules/decks/use-cases/add-deck-to-favorites.use-case.ts b/src/modules/decks/use-cases/add-deck-to-favorites.use-case.ts index fcbf4c1..f298b3a 100644 --- a/src/modules/decks/use-cases/add-deck-to-favorites.use-case.ts +++ b/src/modules/decks/use-cases/add-deck-to-favorites.use-case.ts @@ -15,7 +15,7 @@ export class AddDeckToFavoritesHandler implements ICommandHandler { - const deck = await this.decksRepository.findDeckById(command.deckId) + const deck = await this.decksRepository.findDeckById(command.deckId, command.userId) if (!deck) { throw new NotFoundException(`Deck with id ${command.deckId} not found`) diff --git a/src/modules/decks/use-cases/create-card-use-case.ts b/src/modules/decks/use-cases/create-card-use-case.ts index 0319edb..7ef940f 100644 --- a/src/modules/decks/use-cases/create-card-use-case.ts +++ b/src/modules/decks/use-cases/create-card-use-case.ts @@ -28,7 +28,7 @@ export class CreateCardHandler implements ICommandHandler { async execute(command: CreateCardCommand): Promise { let questionImg, answerImg - const deck = await this.decksRepository.findDeckById(command.deckId) + const deck = await this.decksRepository.findDeckById(command.deckId, command.userId) if (!deck) { throw new NotFoundException(`Deck with id ${command.deckId} not found`) diff --git a/src/modules/decks/use-cases/delete-deck-by-id-use-case.ts b/src/modules/decks/use-cases/delete-deck-by-id-use-case.ts index fff830e..b2e70cc 100644 --- a/src/modules/decks/use-cases/delete-deck-by-id-use-case.ts +++ b/src/modules/decks/use-cases/delete-deck-by-id-use-case.ts @@ -15,7 +15,7 @@ export class DeleteDeckByIdHandler implements ICommandHandler { - const deck = await this.decksRepository.findDeckById(command.deckId) + const deck = await this.decksRepository.findDeckById(command.deckId, command.userId) if (!deck) throw new NotFoundException(`Deck with id ${command.deckId} not found`) diff --git a/src/modules/decks/use-cases/get-deck-by-id-use-case.ts b/src/modules/decks/use-cases/get-deck-by-id-use-case.ts index 73b37c6..81fc97d 100644 --- a/src/modules/decks/use-cases/get-deck-by-id-use-case.ts +++ b/src/modules/decks/use-cases/get-deck-by-id-use-case.ts @@ -3,7 +3,10 @@ import { CommandHandler, ICommandHandler } from '@nestjs/cqrs' import { DecksRepository } from '../infrastructure/decks.repository' export class GetDeckByIdCommand { - constructor(public readonly id: string) {} + constructor( + public readonly id: string, + public readonly userId: string + ) {} } @CommandHandler(GetDeckByIdCommand) @@ -11,6 +14,6 @@ export class GetDeckByIdHandler implements ICommandHandler { constructor(private readonly deckRepository: DecksRepository) {} async execute(command: GetDeckByIdCommand) { - return await this.deckRepository.findDeckById(command.id) + return await this.deckRepository.findDeckById(command.id, command.userId) } } diff --git a/src/modules/decks/use-cases/get-random-card-in-deck-use-case.ts b/src/modules/decks/use-cases/get-random-card-in-deck-use-case.ts index e8ad5f9..3df449d 100644 --- a/src/modules/decks/use-cases/get-random-card-in-deck-use-case.ts +++ b/src/modules/decks/use-cases/get-random-card-in-deck-use-case.ts @@ -22,7 +22,7 @@ export class GetRandomCardInDeckHandler implements ICommandHandler { - const deck = await this.decksRepository.findDeckById(command.deckId) + const deck = await this.decksRepository.findDeckById(command.deckId, command.userId) if (!deck) { throw new NotFoundException(`Deck with id ${command.deckId} not found`) diff --git a/src/modules/decks/use-cases/update-deck-use-case.ts b/src/modules/decks/use-cases/update-deck-use-case.ts index dbc4dae..47a9c79 100644 --- a/src/modules/decks/use-cases/update-deck-use-case.ts +++ b/src/modules/decks/use-cases/update-deck-use-case.ts @@ -23,7 +23,7 @@ export class UpdateDeckHandler implements ICommandHandler { ) {} async execute(command: UpdateDeckCommand): Promise { - const deck = await this.deckRepository.findDeckById(command.deckId) + const deck = await this.deckRepository.findDeckById(command.deckId, command.userId) if (!deck) { throw new NotFoundException(`Deck with id ${command.deckId} not found`)