diff --git a/src/modules/cards/cards.controller.ts b/src/modules/cards/cards.controller.ts index 0e300ce..e5acd27 100644 --- a/src/modules/cards/cards.controller.ts +++ b/src/modules/cards/cards.controller.ts @@ -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 { + findOne(@Param('id') id: string): Promise { return this.commandBus.execute(new GetDeckByIdCommand(id)) } diff --git a/src/modules/cards/entities/cards.entity.ts b/src/modules/cards/entities/cards.entity.ts index 8924adf..fa6d6b6 100644 --- a/src/modules/cards/entities/cards.entity.ts +++ b/src/modules/cards/entities/cards.entity.ts @@ -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 } diff --git a/src/modules/cards/infrastructure/cards.repository.ts b/src/modules/cards/infrastructure/cards.repository.ts index 6f34ec8..eb6e6c9 100644 --- a/src/modules/cards/infrastructure/cards.repository.ts +++ b/src/modules/cards/infrastructure/cards.repository.ts @@ -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 { + ): Promise { if (!orderBy || orderBy === 'null') { orderBy = 'updated-desc' } @@ -92,7 +92,7 @@ export class CardsRepository { itemsPerPage )) satisfies Array - 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): Promise { - const selectionPool: Array = [] + private async getSmartRandomCard(cards: Array): Promise { + const selectionPool: Array = [] cards.forEach(card => { // Calculate the average grade for the card @@ -218,9 +218,9 @@ export class CardsRepository { } private async getNotDuplicateRandomCard( - cards: Array, + cards: Array, previousCardId: string - ): Promise { + ): Promise { const randomCard = await this.getSmartRandomCard(cards) if (!randomCard) { diff --git a/src/modules/decks/decks.controller.ts b/src/modules/decks/decks.controller.ts index 5e5030e..6bce2cc 100644 --- a/src/modules/decks/decks.controller.ts +++ b/src/modules/decks/decks.controller.ts @@ -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 { + ): Promise { 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 { + ): Promise { 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 { return await this.commandBus.execute( new SaveGradeCommand(req.user.id, { cardId: body.cardId, grade: body.grade }) ) diff --git a/src/modules/decks/dto/get-all-decks.dto.ts b/src/modules/decks/dto/get-all-decks.dto.ts index 02ac9a2..13252b4 100644 --- a/src/modules/decks/dto/get-all-decks.dto.ts +++ b/src/modules/decks/dto/get-all-decks.dto.ts @@ -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 } diff --git a/src/modules/decks/use-cases/get-all-cards-in-deck-use-case.ts b/src/modules/decks/use-cases/get-all-cards-in-deck-use-case.ts index 2d7cc51..d9471d5 100644 --- a/src/modules/decks/use-cases/get-all-cards-in-deck-use-case.ts +++ b/src/modules/decks/use-cases/get-all-cards-in-deck-use-case.ts @@ -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 @@ -31,18 +36,18 @@ export class GetAllCardsInDeckHandler implements ICommandHandler