Files
flashcards-api/src/modules/decks/use-cases/delete-deck-by-id-use-case.ts
2023-07-16 19:44:58 +02:00

25 lines
920 B
TypeScript

import { BadRequestException, NotFoundException } from '@nestjs/common'
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
import { DecksRepository } from '../infrastructure/decks.repository'
export class DeleteDeckByIdCommand {
constructor(public readonly id: string, public readonly userId: string) {}
}
@CommandHandler(DeleteDeckByIdCommand)
export class DeleteDeckByIdHandler implements ICommandHandler<DeleteDeckByIdCommand> {
constructor(private readonly deckRepository: DecksRepository) {}
async execute(command: DeleteDeckByIdCommand) {
const deck = await this.deckRepository.findDeckById(command.id)
if (!deck) throw new NotFoundException(`Deck with id ${command.id} not found`)
if (deck.userId !== command.userId) {
throw new BadRequestException(`You can't delete a deck that you don't own`)
}
return await this.deckRepository.deleteDeckById(command.id)
}
}