mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-17 05:09:26 +00:00
fix orderby
This commit is contained in:
@@ -1,7 +1,10 @@
|
|||||||
type OrderByDirection = 'asc' | 'desc'
|
type OrderByDirection = 'asc' | 'desc'
|
||||||
|
|
||||||
export function createPrismaOrderBy(input: string | null) {
|
export function createPrismaOrderBy(input: string | null) {
|
||||||
const { key, direction, relation, field } = getOrderByObject(input)
|
const orderByObject = getOrderByObject(input)
|
||||||
|
|
||||||
|
if (!orderByObject) return null
|
||||||
|
const { key, direction, relation, field } = orderByObject
|
||||||
|
|
||||||
if (relation && field) {
|
if (relation && field) {
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ export class CardsRepository {
|
|||||||
question = undefined,
|
question = undefined,
|
||||||
currentPage,
|
currentPage,
|
||||||
itemsPerPage,
|
itemsPerPage,
|
||||||
orderBy,
|
orderBy = 'updated-desc',
|
||||||
}: GetAllCardsInDeckDto
|
}: GetAllCardsInDeckDto
|
||||||
): Promise<PaginatedCardsWithGrade> {
|
): Promise<PaginatedCardsWithGrade> {
|
||||||
try {
|
try {
|
||||||
@@ -78,7 +78,7 @@ export class CardsRepository {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
const { key, direction } = getOrderByObject(orderBy)
|
const { key, direction } = getOrderByObject(orderBy) || {}
|
||||||
|
|
||||||
if (key === 'grade') {
|
if (key === 'grade') {
|
||||||
const start = (currentPage - 1) * itemsPerPage
|
const start = (currentPage - 1) * itemsPerPage
|
||||||
@@ -122,7 +122,7 @@ export class CardsRepository {
|
|||||||
const result = await this.prisma.$transaction([
|
const result = await this.prisma.$transaction([
|
||||||
this.prisma.card.count({ where }),
|
this.prisma.card.count({ where }),
|
||||||
this.prisma.card.findMany({
|
this.prisma.card.findMany({
|
||||||
orderBy: createPrismaOrderBy(orderBy) || { updated: 'desc' },
|
orderBy: createPrismaOrderBy(orderBy),
|
||||||
where,
|
where,
|
||||||
include: {
|
include: {
|
||||||
grades: {
|
grades: {
|
||||||
|
|||||||
Reference in New Issue
Block a user