mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-16 20:59:26 +00:00
fix: update docs
This commit is contained in:
@@ -26,7 +26,7 @@ import {
|
||||
import { JwtAuthGuard } from '../auth/guards'
|
||||
|
||||
import { UpdateCardDto } from './dto'
|
||||
import { Card } from './entities/cards.entity'
|
||||
import { Card, CardWithGrade } from './entities/cards.entity'
|
||||
import { DeleteCardByIdCommand, GetDeckByIdCommand, UpdateCardCommand } from './use-cases'
|
||||
|
||||
@ApiTags('Cards')
|
||||
@@ -39,7 +39,7 @@ export class CardsController {
|
||||
@ApiUnauthorizedResponse({ description: 'Unauthorized' })
|
||||
@ApiNotFoundResponse({ description: 'Card not found' })
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string): Promise<Card> {
|
||||
findOne(@Param('id') id: string): Promise<CardWithGrade> {
|
||||
return this.commandBus.execute(new GetDeckByIdCommand(id))
|
||||
}
|
||||
|
||||
|
||||
@@ -20,11 +20,20 @@ export class PaginatedCards {
|
||||
pagination: Pagination
|
||||
}
|
||||
|
||||
export class PaginatedCardsWithGrades {
|
||||
pagination: Pagination
|
||||
items: CardWithGrades[]
|
||||
}
|
||||
|
||||
export class CardWithGrades extends Card {
|
||||
grades?: Array<{ grade: number }>
|
||||
}
|
||||
|
||||
export class PaginatedCardsWithGrade {
|
||||
pagination: Pagination
|
||||
items: CardWithGrade[]
|
||||
}
|
||||
|
||||
export class CardWithGrade extends Card {
|
||||
grades?: Array<{ grade: number }>
|
||||
grade: number
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import {
|
||||
import { Pagination } from '../../../infrastructure/common/pagination/pagination.service'
|
||||
import { PrismaService } from '../../../prisma.service'
|
||||
import { CreateCardDto, GetAllCardsInDeckDto, UpdateCardDto } from '../dto'
|
||||
import { CardWithGrade, PaginatedCardsWithGrade } from '../entities/cards.entity'
|
||||
import { CardWithGrades, PaginatedCardsWithGrades } from '../entities/cards.entity'
|
||||
|
||||
@Injectable()
|
||||
export class CardsRepository {
|
||||
@@ -50,7 +50,7 @@ export class CardsRepository {
|
||||
itemsPerPage,
|
||||
orderBy,
|
||||
}: GetAllCardsInDeckDto
|
||||
): Promise<PaginatedCardsWithGrade> {
|
||||
): Promise<PaginatedCardsWithGrades> {
|
||||
if (!orderBy || orderBy === 'null') {
|
||||
orderBy = 'updated-desc'
|
||||
}
|
||||
@@ -92,7 +92,7 @@ export class CardsRepository {
|
||||
itemsPerPage
|
||||
)) satisfies Array<any>
|
||||
|
||||
const cards: CardWithGrade[] = cardsRaw.map(({ userGrade, ...card }) => ({
|
||||
const cards: CardWithGrades[] = cardsRaw.map(({ userGrade, ...card }) => ({
|
||||
...card,
|
||||
grades: [
|
||||
{
|
||||
@@ -196,8 +196,8 @@ export class CardsRepository {
|
||||
}
|
||||
}
|
||||
|
||||
private async getSmartRandomCard(cards: Array<CardWithGrade>): Promise<CardWithGrade> {
|
||||
const selectionPool: Array<CardWithGrade> = []
|
||||
private async getSmartRandomCard(cards: Array<CardWithGrades>): Promise<CardWithGrades> {
|
||||
const selectionPool: Array<CardWithGrades> = []
|
||||
|
||||
cards.forEach(card => {
|
||||
// Calculate the average grade for the card
|
||||
@@ -218,9 +218,9 @@ export class CardsRepository {
|
||||
}
|
||||
|
||||
private async getNotDuplicateRandomCard(
|
||||
cards: Array<CardWithGrade>,
|
||||
cards: Array<CardWithGrades>,
|
||||
previousCardId: string
|
||||
): Promise<CardWithGrade> {
|
||||
): Promise<CardWithGrades> {
|
||||
const randomCard = await this.getSmartRandomCard(cards)
|
||||
|
||||
if (!randomCard) {
|
||||
|
||||
@@ -31,7 +31,7 @@ import { Pagination } from '../../infrastructure/common/pagination/pagination.se
|
||||
import { SaveGradeDto } from '../auth/dto'
|
||||
import { JwtAuthGuard } from '../auth/guards'
|
||||
import { CreateCardDto, GetAllCardsInDeckDto } from '../cards/dto'
|
||||
import { Card, PaginatedCards } from '../cards/entities/cards.entity'
|
||||
import { Card, CardWithGrade, PaginatedCardsWithGrade } from '../cards/entities/cards.entity'
|
||||
|
||||
import { CreateDeckDto, GetAllDecksDto, UpdateDeckDto } from './dto'
|
||||
import { GetRandomCardDto } from './dto/get-random-card.dto'
|
||||
@@ -134,7 +134,7 @@ export class DecksController {
|
||||
@Param('id') id: string,
|
||||
@Req() req,
|
||||
@Query() query: GetAllCardsInDeckDto
|
||||
): Promise<PaginatedCards> {
|
||||
): Promise<PaginatedCardsWithGrade> {
|
||||
const finalQuery = Pagination.getPaginationData(query)
|
||||
|
||||
return this.commandBus.execute(new GetAllCardsInDeckCommand(req.user.id, id, finalQuery))
|
||||
@@ -176,7 +176,7 @@ export class DecksController {
|
||||
@Param('id') id: string,
|
||||
@Req() req,
|
||||
@Query() query: GetRandomCardDto
|
||||
): Promise<Card> {
|
||||
): Promise<CardWithGrade> {
|
||||
return this.commandBus.execute(
|
||||
new GetRandomCardInDeckCommand(req.user.id, id, query.previousCardId)
|
||||
)
|
||||
@@ -189,14 +189,14 @@ export class DecksController {
|
||||
@ApiNoContentResponse({ description: 'Grade saved' })
|
||||
@ApiOkResponse({
|
||||
description: 'A new random card in the deck. Will never return the same card that was sent',
|
||||
type: Card,
|
||||
type: CardWithGrade,
|
||||
})
|
||||
@Post(':id/learn')
|
||||
@ApiOperation({
|
||||
description: 'Save the grade of a card',
|
||||
summary: 'Save the grade of a card',
|
||||
})
|
||||
async saveGrade(@Req() req, @Body() body: SaveGradeDto) {
|
||||
async saveGrade(@Req() req, @Body() body: SaveGradeDto): Promise<CardWithGrade> {
|
||||
return await this.commandBus.execute(
|
||||
new SaveGradeCommand(req.user.id, { cardId: body.cardId, grade: body.grade })
|
||||
)
|
||||
|
||||
@@ -12,11 +12,11 @@ export enum DecksOrderBy {
|
||||
'name-asc' = 'name-asc',
|
||||
'author.name-asc' = 'author.name-asc',
|
||||
'created-asc' = 'created-asc',
|
||||
'cardsCount-decs' = 'cardsCount-decs',
|
||||
'updated-decs' = 'updated-decs',
|
||||
'name-decs' = 'name-decs',
|
||||
'author.name-decs' = 'author.name-decs',
|
||||
'created-decs' = 'created-decs',
|
||||
'cardsCount-desc' = 'cardsCount-desc',
|
||||
'updated-desc' = 'updated-desc',
|
||||
'name-desc' = 'name-desc',
|
||||
'author.name-desc' = 'author.name-desc',
|
||||
'created-desc' = 'created-desc',
|
||||
}
|
||||
|
||||
export class GetAllDecksDto extends PaginationDto {
|
||||
@@ -50,7 +50,9 @@ export class GetAllDecksDto extends PaginationDto {
|
||||
@IsOrderBy()
|
||||
@ApiProperty({
|
||||
enum: DecksOrderBy,
|
||||
required: false,
|
||||
})
|
||||
@IsEnum(DecksOrderBy)
|
||||
@IsOptional()
|
||||
@IsEnum(DecksOrderBy, {})
|
||||
orderBy?: DecksOrderBy
|
||||
}
|
||||
|
||||
@@ -3,7 +3,11 @@ import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
|
||||
import { pick } from 'remeda'
|
||||
|
||||
import { GetAllCardsInDeckDto } from '../../cards/dto'
|
||||
import { PaginatedCards, PaginatedCardsWithGrade } from '../../cards/entities/cards.entity'
|
||||
import {
|
||||
PaginatedCards,
|
||||
PaginatedCardsWithGrade,
|
||||
PaginatedCardsWithGrades,
|
||||
} from '../../cards/entities/cards.entity'
|
||||
import { CardsRepository } from '../../cards/infrastructure/cards.repository'
|
||||
import { DecksRepository } from '../infrastructure/decks.repository'
|
||||
|
||||
@@ -21,7 +25,8 @@ export class GetAllCardsInDeckHandler implements ICommandHandler<GetAllCardsInDe
|
||||
private readonly cardsRepository: CardsRepository,
|
||||
private readonly decksRepository: DecksRepository
|
||||
) {}
|
||||
private transformGrade(cards: PaginatedCardsWithGrade): PaginatedCards {
|
||||
|
||||
private transformGrade(cards: PaginatedCardsWithGrades): PaginatedCardsWithGrade {
|
||||
return {
|
||||
...cards,
|
||||
items: cards.items.map(card =>
|
||||
@@ -31,18 +36,18 @@ export class GetAllCardsInDeckHandler implements ICommandHandler<GetAllCardsInDe
|
||||
grade: card.grades[0]?.grade || 0,
|
||||
},
|
||||
[
|
||||
'id',
|
||||
'question',
|
||||
'answer',
|
||||
'deckId',
|
||||
'questionImg',
|
||||
'answerImg',
|
||||
'questionVideo',
|
||||
'answerVideo',
|
||||
'created',
|
||||
'updated',
|
||||
'shots',
|
||||
'deckId',
|
||||
'grade',
|
||||
'id',
|
||||
'question',
|
||||
'questionImg',
|
||||
'questionVideo',
|
||||
'shots',
|
||||
'updated',
|
||||
'userId',
|
||||
]
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user