feat: add to favorites

This commit is contained in:
2024-05-30 20:33:17 +02:00
parent 6604b880bf
commit aba09f5367
6 changed files with 117 additions and 53 deletions

View File

@@ -0,0 +1,36 @@
import { BadRequestException, NotFoundException } from '@nestjs/common'
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
import { DecksRepository } from '../infrastructure/decks.repository'
export class RemoveDeckFromFavoritesCommand {
constructor(
public readonly userId: string,
public readonly deckId: string
) {}
}
@CommandHandler(RemoveDeckFromFavoritesCommand)
export class RemoveDeckFromFavoritesHandler
implements ICommandHandler<RemoveDeckFromFavoritesCommand>
{
constructor(private readonly decksRepository: DecksRepository) {}
async execute(command: RemoveDeckFromFavoritesCommand): Promise<void> {
const deck = await this.decksRepository.findDeckById(command.deckId)
if (!deck) {
throw new NotFoundException(`Deck with id ${command.deckId} not found`)
}
const favorites = await this.decksRepository.findFavoritesByUserId(
command.userId,
command.deckId
)
if (!favorites?.includes(command.deckId)) {
throw new BadRequestException(`Deck with id ${command.deckId} is not a favorite`)
}
return await this.decksRepository.removeDeckFromFavorites(command.userId, command.deckId)
}
}