file upload in progress

This commit is contained in:
andres
2023-07-15 20:32:00 +02:00
parent dc09cf2238
commit df1f041961
11 changed files with 94 additions and 55 deletions

View File

@@ -1,3 +1,3 @@
export * from './get-deck-by-id-use-case'
export * from './delete-card-by-id-use-case'
export * from './update-deck-use-case'
export * from './update-card-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 UpdateCardCommand {
constructor(
public readonly cardId: string,
public readonly card: UpdateCardDto,
public readonly userId: string
) {}
}
@CommandHandler(UpdateCardCommand)
export class UpdateCardHandler implements ICommandHandler<UpdateCardCommand> {
constructor(private readonly cardsRepository: CardsRepository) {}
async execute(command: UpdateCardCommand) {
const card = await this.cardsRepository.findCardById(command.cardId)
if (!card) throw new NotFoundException(`Card with id ${command.cardId} not found`)
if (card.userId !== command.userId) {
throw new BadRequestException(`You can't change a card that you don't own`)
}
return await this.cardsRepository.updateCardById(command.cardId, command.card)
}
}

View File

@@ -1,29 +0,0 @@
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.findCardById(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)
}
}