chore: update cardsOrderByEnum

This commit is contained in:
2024-06-12 12:08:36 +02:00
parent ef54dc14e7
commit f697747124
2 changed files with 30 additions and 4 deletions

View File

@@ -1,8 +1,23 @@
import { Length } from 'class-validator' import { ApiProperty } from '@nestjs/swagger'
import { IsEnum, IsOptional, Length } from 'class-validator'
import { PaginationDto } from '../../../infrastructure/common/pagination/pagination.dto' import { PaginationDto } from '../../../infrastructure/common/pagination/pagination.dto'
import { IsOptionalOrEmptyString, IsOrderBy } from '../../../infrastructure/decorators' import { IsOptionalOrEmptyString, IsOrderBy } from '../../../infrastructure/decorators'
export enum CardsOrderBy {
'null' = 'null',
'question-asc' = 'question-asc',
'question-desc' = 'question-desc',
'answer-asc' = 'answer-asc',
'answer-desc' = 'answer-desc',
'created-asc' = 'created-asc',
'created-desc' = 'created-desc',
'grade-asc' = 'grade-asc',
'grade-desc' = 'grade-desc',
'updated-asc' = 'updated-asc',
'updated-desc' = 'updated-desc',
}
export class GetAllCardsInDeckDto extends PaginationDto { export class GetAllCardsInDeckDto extends PaginationDto {
@IsOptionalOrEmptyString() @IsOptionalOrEmptyString()
@Length(1, 30) @Length(1, 30)
@@ -12,6 +27,17 @@ export class GetAllCardsInDeckDto extends PaginationDto {
@Length(1, 30) @Length(1, 30)
answer?: string answer?: string
/** A string that represents the name of the field to order by and the order direction.
* The format is: "field_name-order_direction".
* Available directions: "asc" and "desc".
* @example "grade-desc"
* */
@IsOrderBy() @IsOrderBy()
orderBy?: string | null @ApiProperty({
enum: CardsOrderBy,
required: false,
})
@IsOptional()
@IsEnum(CardsOrderBy, {})
orderBy?: CardsOrderBy
} }

View File

@@ -7,7 +7,7 @@ import {
} from '../../../infrastructure/common/helpers/get-order-by-object' } from '../../../infrastructure/common/helpers/get-order-by-object'
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 { CardsOrderBy, CreateCardDto, GetAllCardsInDeckDto, UpdateCardDto } from '../dto'
import { CardWithGrades, PaginatedCardsWithGrades } from '../entities/cards.entity' import { CardWithGrades, PaginatedCardsWithGrades } from '../entities/cards.entity'
@Injectable() @Injectable()
@@ -52,7 +52,7 @@ export class CardsRepository {
}: GetAllCardsInDeckDto }: GetAllCardsInDeckDto
): Promise<PaginatedCardsWithGrades> { ): Promise<PaginatedCardsWithGrades> {
if (!orderBy || orderBy === 'null') { if (!orderBy || orderBy === 'null') {
orderBy = 'updated-desc' orderBy = CardsOrderBy['updated-desc']
} }
try { try {
const where = { const where = {