mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-31 12:34:18 +00:00
add crete/get cards
This commit is contained in:
21
src/modules/cards/use-cases/delete-deck-by-id-use-case.ts
Normal file
21
src/modules/cards/use-cases/delete-deck-by-id-use-case.ts
Normal 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)
|
||||
}
|
||||
}
|
||||
15
src/modules/cards/use-cases/get-deck-by-id-use-case.ts
Normal file
15
src/modules/cards/use-cases/get-deck-by-id-use-case.ts
Normal 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)
|
||||
}
|
||||
}
|
||||
5
src/modules/cards/use-cases/index.ts
Normal file
5
src/modules/cards/use-cases/index.ts
Normal 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'
|
||||
29
src/modules/cards/use-cases/update-deck-use-case.ts
Normal file
29
src/modules/cards/use-cases/update-deck-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 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user