add crete/get cards

This commit is contained in:
2023-06-18 12:16:03 +02:00
parent 0794238f0d
commit ca976cfe5d
19 changed files with 358 additions and 7 deletions

View File

@@ -0,0 +1,21 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
import { CardsRepository } from '../infrastructure/cards.repository'
import { BadRequestException, NotFoundException } from '@nestjs/common'
export class DeleteDeckByIdCommand {
constructor(public readonly id: string, public readonly userId: string) {}
}
@CommandHandler(DeleteDeckByIdCommand)
export class DeleteDeckByIdHandler implements ICommandHandler<DeleteDeckByIdCommand> {
constructor(private readonly deckRepository: CardsRepository) {}
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)
}
}

View File

@@ -0,0 +1,15 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
import { CardsRepository } from '../infrastructure/cards.repository'
export class GetDeckByIdCommand {
constructor(public readonly id: string) {}
}
@CommandHandler(GetDeckByIdCommand)
export class GetDeckByIdHandler implements ICommandHandler<GetDeckByIdCommand> {
constructor(private readonly deckRepository: CardsRepository) {}
async execute(command: GetDeckByIdCommand) {
return await this.deckRepository.findDeckById(command.id)
}
}

View File

@@ -0,0 +1,5 @@
export * from '../../decks/use-cases/create-card-use-case'
export * from '../../decks/use-cases/get-all-cards-in-deck-use-case'
export * from './get-deck-by-id-use-case'
export * from './delete-deck-by-id-use-case'
export * from './update-deck-use-case'

View File

@@ -0,0 +1,29 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
import { CardsRepository } from '../infrastructure/cards.repository'
import { UpdateCardDto } from '../dto/update-card.dto'
import { BadRequestException, NotFoundException } from '@nestjs/common'
export class UpdateDeckCommand {
constructor(
public readonly deckId: string,
public readonly deck: UpdateCardDto,
public readonly userId: string
) {}
}
@CommandHandler(UpdateDeckCommand)
export class UpdateDeckHandler implements ICommandHandler<UpdateDeckCommand> {
constructor(private readonly deckRepository: CardsRepository) {}
async execute(command: UpdateDeckCommand) {
const deck = await this.deckRepository.findDeckById(command.deckId)
if (!deck) throw new NotFoundException(`Deck with id ${command.deckId} not found`)
if (deck.userId !== command.userId) {
throw new BadRequestException(`You can't change a deck that you don't own`)
}
return await this.deckRepository.updateDeckById(command.deckId, command.deck)
}
}