mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-30 20:59:26 +00:00
file upload in progress
This commit is contained in:
@@ -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'
|
||||
|
||||
29
src/modules/cards/use-cases/update-card-use-case.ts
Normal file
29
src/modules/cards/use-cases/update-card-use-case.ts
Normal 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)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user