mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-18 05:09:29 +00:00
add auth docs, add eslint import/order
This commit is contained in:
@@ -10,13 +10,17 @@ import {
|
||||
UseGuards,
|
||||
UseInterceptors,
|
||||
} from '@nestjs/common'
|
||||
import { CommandBus } from '@nestjs/cqrs'
|
||||
import { FileFieldsInterceptor } from '@nestjs/platform-express'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
|
||||
import { JwtAuthGuard } from '../auth/guards'
|
||||
|
||||
import { CardsService } from './cards.service'
|
||||
import { UpdateCardDto } from './dto'
|
||||
import { CommandBus } from '@nestjs/cqrs'
|
||||
import { DeleteCardByIdCommand, GetDeckByIdCommand, UpdateCardCommand } from './use-cases'
|
||||
import { JwtAuthGuard } from '../auth/guards'
|
||||
import { FileFieldsInterceptor } from '@nestjs/platform-express'
|
||||
|
||||
@ApiTags('Cards')
|
||||
@Controller('cards')
|
||||
export class CardsController {
|
||||
constructor(private readonly decksService: CardsService, private commandBus: CommandBus) {}
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
import { Module } from '@nestjs/common'
|
||||
import { CardsService } from './cards.service'
|
||||
import { CardsController } from './cards.controller'
|
||||
import { CqrsModule } from '@nestjs/cqrs'
|
||||
import { DeleteCardByIdHandler, GetDeckByIdHandler, UpdateCardHandler } from './use-cases'
|
||||
import { CardsRepository } from './infrastructure/cards.repository'
|
||||
|
||||
import { FileUploadService } from '../../infrastructure/file-upload-service/file-upload.service'
|
||||
|
||||
import { CardsController } from './cards.controller'
|
||||
import { CardsService } from './cards.service'
|
||||
import { CardsRepository } from './infrastructure/cards.repository'
|
||||
import { DeleteCardByIdHandler, GetDeckByIdHandler, UpdateCardHandler } from './use-cases'
|
||||
|
||||
const commandHandlers = [GetDeckByIdHandler, DeleteCardByIdHandler, UpdateCardHandler]
|
||||
|
||||
@Module({
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Length } from 'class-validator'
|
||||
|
||||
import { PaginationDto } from '../../../infrastructure/common/pagination/pagination.dto'
|
||||
import { IsOptionalOrEmptyString, IsOrderBy } from '../../../infrastructure/decorators'
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { PartialType } from '@nestjs/mapped-types'
|
||||
|
||||
import { CreateCardDto } from './create-card.dto'
|
||||
|
||||
export class UpdateCardDto extends PartialType(CreateCardDto) {}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import { Injectable, InternalServerErrorException, Logger } from '@nestjs/common'
|
||||
import { PrismaService } from '../../../prisma.service'
|
||||
import { GetAllCardsInDeckDto } from '../dto/get-all-cards.dto'
|
||||
import { CreateCardDto } from '../dto/create-card.dto'
|
||||
import { Pagination } from '../../../infrastructure/common/pagination/pagination.service'
|
||||
|
||||
import { createPrismaOrderBy } from '../../../infrastructure/common/helpers/get-order-by-object'
|
||||
import { UpdateCardDto } from '../dto/update-card.dto'
|
||||
import { Pagination } from '../../../infrastructure/common/pagination/pagination.service'
|
||||
import { PrismaService } from '../../../prisma.service'
|
||||
import { CreateCardDto, GetAllCardsInDeckDto, UpdateCardDto } from '../dto'
|
||||
|
||||
@Injectable()
|
||||
export class CardsRepository {
|
||||
@@ -31,6 +30,7 @@ export class CardsRepository {
|
||||
...card,
|
||||
},
|
||||
})
|
||||
|
||||
await tx.deck.update({
|
||||
where: {
|
||||
id: deckId,
|
||||
@@ -41,6 +41,7 @@ export class CardsRepository {
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
return created
|
||||
})
|
||||
} catch (e) {
|
||||
@@ -80,6 +81,7 @@ export class CardsRepository {
|
||||
take: itemsPerPage,
|
||||
}),
|
||||
])
|
||||
|
||||
return Pagination.transformPaginationData(result, { currentPage, itemsPerPage })
|
||||
} catch (e) {
|
||||
this.logger.error(e?.message)
|
||||
@@ -129,6 +131,7 @@ export class CardsRepository {
|
||||
id,
|
||||
},
|
||||
})
|
||||
|
||||
await tx.deck.update({
|
||||
where: {
|
||||
id: deleted.deckId,
|
||||
@@ -139,6 +142,7 @@ export class CardsRepository {
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
return deleted
|
||||
})
|
||||
} catch (e) {
|
||||
|
||||
@@ -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