mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-18 12:33:21 +00:00
add crete/get cards
This commit is contained in:
@@ -18,12 +18,16 @@ import { CommandBus } from '@nestjs/cqrs'
|
||||
import {
|
||||
CreateDeckCommand,
|
||||
DeleteDeckByIdCommand,
|
||||
GetAllCardsInDeckCommand,
|
||||
GetAllDecksCommand,
|
||||
GetDeckByIdCommand,
|
||||
UpdateDeckCommand,
|
||||
} from './use-cases'
|
||||
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'
|
||||
import { GetAllDecksDto } from './dto/get-all-decks.dto'
|
||||
import { GetAllCardsInDeckDto } from '../cards/dto/get-all-cards.dto'
|
||||
import { CreateCardCommand } from './use-cases/create-card-use-case'
|
||||
import { CreateCardDto } from '../cards/dto/create-card.dto'
|
||||
|
||||
@Controller('decks')
|
||||
export class DecksController {
|
||||
@@ -48,6 +52,18 @@ export class DecksController {
|
||||
return this.commandBus.execute(new GetDeckByIdCommand(id))
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get(':id/cards')
|
||||
findCardsInDeck(@Param('id') id: string, @Req() req, @Query() query: GetAllCardsInDeckDto) {
|
||||
return this.commandBus.execute(new GetAllCardsInDeckCommand(req.user.id, id, query))
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post(':id/cards')
|
||||
createCardInDeck(@Param('id') id: string, @Req() req, @Body() card: CreateCardDto) {
|
||||
return this.commandBus.execute(new CreateCardCommand(req.user.id, id, card))
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() updateDeckDto: UpdateDeckDto, @Req() req) {
|
||||
|
||||
@@ -8,8 +8,11 @@ import {
|
||||
GetDeckByIdHandler,
|
||||
GetAllDecksHandler,
|
||||
UpdateDeckHandler,
|
||||
GetAllCardsInDeckHandler,
|
||||
} from './use-cases'
|
||||
import { DecksRepository } from './infrastructure/decks.repository'
|
||||
import { CardsRepository } from '../cards/infrastructure/cards.repository'
|
||||
import { CreateCardHandler } from './use-cases/create-card-use-case'
|
||||
|
||||
const commandHandlers = [
|
||||
CreateDeckHandler,
|
||||
@@ -17,12 +20,14 @@ const commandHandlers = [
|
||||
GetDeckByIdHandler,
|
||||
DeleteDeckByIdHandler,
|
||||
UpdateDeckHandler,
|
||||
GetAllCardsInDeckHandler,
|
||||
CreateCardHandler,
|
||||
]
|
||||
|
||||
@Module({
|
||||
imports: [CqrsModule],
|
||||
controllers: [DecksController],
|
||||
providers: [DecksService, DecksRepository, ...commandHandlers],
|
||||
providers: [DecksService, DecksRepository, CardsRepository, ...commandHandlers],
|
||||
exports: [CqrsModule],
|
||||
})
|
||||
export class DecksModule {}
|
||||
|
||||
20
src/modules/decks/use-cases/create-card-use-case.ts
Normal file
20
src/modules/decks/use-cases/create-card-use-case.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
|
||||
import { CreateCardDto } from '../../cards/dto/create-card.dto'
|
||||
import { CardsRepository } from '../../cards/infrastructure/cards.repository'
|
||||
|
||||
export class CreateCardCommand {
|
||||
constructor(
|
||||
public readonly userId: string,
|
||||
public readonly deckId: string,
|
||||
public readonly card: CreateCardDto
|
||||
) {}
|
||||
}
|
||||
|
||||
@CommandHandler(CreateCardCommand)
|
||||
export class CreateCardHandler implements ICommandHandler<CreateCardCommand> {
|
||||
constructor(private readonly cardsRepository: CardsRepository) {}
|
||||
|
||||
async execute(command: CreateCardCommand) {
|
||||
return await this.cardsRepository.createCard(command.deckId, command.userId, command.card)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
|
||||
import { CardsRepository } from '../../cards/infrastructure/cards.repository'
|
||||
import { GetAllCardsInDeckDto } from '../../cards/dto/get-all-cards.dto'
|
||||
import { ForbiddenException, NotFoundException } from '@nestjs/common'
|
||||
|
||||
export class GetAllCardsInDeckCommand {
|
||||
constructor(
|
||||
public readonly userId: string,
|
||||
public readonly deckId: string,
|
||||
public readonly params: GetAllCardsInDeckDto
|
||||
) {}
|
||||
}
|
||||
|
||||
@CommandHandler(GetAllCardsInDeckCommand)
|
||||
export class GetAllCardsInDeckHandler implements ICommandHandler<GetAllCardsInDeckCommand> {
|
||||
constructor(private readonly cardsRepository: CardsRepository) {}
|
||||
|
||||
async execute(command: GetAllCardsInDeckCommand) {
|
||||
const deck = await this.cardsRepository.findDeckById(command.deckId)
|
||||
if (!deck) throw new NotFoundException(`Deck with id ${command.deckId} not found`)
|
||||
|
||||
if (deck.userId !== command.userId && deck.isPrivate) {
|
||||
throw new ForbiddenException(`You can't get a private deck that you don't own`)
|
||||
}
|
||||
|
||||
return await this.cardsRepository.findCardsByDeckId(command.deckId, command.params)
|
||||
}
|
||||
}
|
||||
@@ -3,3 +3,4 @@ export * from './get-all-decks-use-case'
|
||||
export * from './get-deck-by-id-use-case'
|
||||
export * from './delete-deck-by-id-use-case'
|
||||
export * from './update-deck-use-case'
|
||||
export * from './get-all-cards-in-deck-use-case'
|
||||
|
||||
Reference in New Issue
Block a user