mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2026-01-03 05:09:27 +00:00
add auth docs, add eslint import/order
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
|
||||
import { CardsRepository } from '../infrastructure/cards.repository'
|
||||
import { BadRequestException, NotFoundException } from '@nestjs/common'
|
||||
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
|
||||
|
||||
import { CardsRepository } from '../infrastructure/cards.repository'
|
||||
|
||||
export class DeleteCardByIdCommand {
|
||||
constructor(public readonly id: string, public readonly userId: string) {}
|
||||
@@ -12,10 +13,12 @@ export class DeleteCardByIdHandler implements ICommandHandler<DeleteCardByIdComm
|
||||
|
||||
async execute(command: DeleteCardByIdCommand) {
|
||||
const card = await this.cardsRepository.findCardById(command.id)
|
||||
|
||||
if (!card) throw new NotFoundException(`Card with id ${command.id} not found`)
|
||||
if (card.userId !== command.userId) {
|
||||
throw new BadRequestException(`You can't delete a card that you don't own`)
|
||||
}
|
||||
|
||||
return await this.cardsRepository.deleteCardById(command.id)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
|
||||
|
||||
import { CardsRepository } from '../infrastructure/cards.repository'
|
||||
|
||||
export class GetDeckByIdCommand {
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
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'
|
||||
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
|
||||
|
||||
import { FileUploadService } from '../../../infrastructure/file-upload-service/file-upload.service'
|
||||
import { UpdateCardDto } from '../dto'
|
||||
import { CardsRepository } from '../infrastructure/cards.repository'
|
||||
|
||||
export class UpdateCardCommand {
|
||||
constructor(
|
||||
@@ -43,6 +44,7 @@ export class UpdateCardHandler implements ICommandHandler<UpdateCardCommand> {
|
||||
)
|
||||
|
||||
const result = await Promise.all([addQuestionImagePromise, addAnswerImagePromise])
|
||||
|
||||
questionImg = result[0].fileUrl
|
||||
answerImg = result[1].fileUrl
|
||||
} else if (command.answerImg) {
|
||||
@@ -51,6 +53,7 @@ export class UpdateCardHandler implements ICommandHandler<UpdateCardCommand> {
|
||||
command.answerImg?.originalname
|
||||
)
|
||||
const result = await addAnswerImagePromise
|
||||
|
||||
answerImg = result.fileUrl
|
||||
} else if (command.questionImg) {
|
||||
const addQuestionImagePromise = this.fileUploadService.uploadFile(
|
||||
@@ -58,6 +61,7 @@ export class UpdateCardHandler implements ICommandHandler<UpdateCardCommand> {
|
||||
command.questionImg?.originalname
|
||||
)
|
||||
const result = await addQuestionImagePromise
|
||||
|
||||
questionImg = result.fileUrl
|
||||
}
|
||||
if (command.card.questionImg === '') {
|
||||
@@ -66,6 +70,7 @@ export class UpdateCardHandler implements ICommandHandler<UpdateCardCommand> {
|
||||
if (command.card.answerImg === '') {
|
||||
answerImg = null
|
||||
}
|
||||
|
||||
return await this.cardsRepository.updateCardById(command.cardId, {
|
||||
...command.card,
|
||||
answerImg,
|
||||
|
||||
Reference in New Issue
Block a user