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