mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-18 05:09:29 +00:00
fix pagination and create pagination service
This commit is contained in:
@@ -1,15 +1,14 @@
|
||||
import { Injectable, InternalServerErrorException, Logger } from '@nestjs/common'
|
||||
import { PrismaService } from '../../../prisma.service'
|
||||
import { GetAllDecksDto } from '../dto/get-all-decks.dto'
|
||||
import {
|
||||
DEFAULT_PAGE_NUMBER,
|
||||
DEFAULT_PAGE_SIZE,
|
||||
} from '../../../infrastructure/common/pagination/pagination.constants'
|
||||
import { Pagination } from '../../../infrastructure/common/pagination/pagination.service'
|
||||
|
||||
@Injectable()
|
||||
export class DecksRepository {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
private readonly logger = new Logger(DecksRepository.name)
|
||||
|
||||
async createDeck({
|
||||
name,
|
||||
userId,
|
||||
@@ -24,7 +23,7 @@ export class DecksRepository {
|
||||
try {
|
||||
return await this.prisma.deck.create({
|
||||
data: {
|
||||
user: {
|
||||
author: {
|
||||
connect: {
|
||||
id: userId,
|
||||
},
|
||||
@@ -45,42 +44,74 @@ export class DecksRepository {
|
||||
name = undefined,
|
||||
authorId = undefined,
|
||||
userId,
|
||||
currentPage = DEFAULT_PAGE_NUMBER,
|
||||
pageSize = DEFAULT_PAGE_SIZE,
|
||||
currentPage,
|
||||
itemsPerPage,
|
||||
minCardsCount,
|
||||
maxCardsCount,
|
||||
}: GetAllDecksDto) {
|
||||
console.log({ name, authorId, userId, currentPage, itemsPerPage, minCardsCount, maxCardsCount })
|
||||
try {
|
||||
return await this.prisma.deck.findMany({
|
||||
where: {
|
||||
name: {
|
||||
contains: name,
|
||||
},
|
||||
user: {
|
||||
id: authorId || undefined,
|
||||
},
|
||||
OR: [
|
||||
{
|
||||
AND: [
|
||||
{
|
||||
isPrivate: true,
|
||||
},
|
||||
{
|
||||
userId: userId,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
isPrivate: false,
|
||||
},
|
||||
],
|
||||
const where = {
|
||||
cardsCount: {
|
||||
gte: Number(minCardsCount) ?? undefined,
|
||||
lte: Number(maxCardsCount) ?? undefined,
|
||||
},
|
||||
skip: (currentPage - 1) * pageSize,
|
||||
take: pageSize,
|
||||
})
|
||||
name: {
|
||||
contains: name,
|
||||
},
|
||||
author: {
|
||||
id: authorId || undefined,
|
||||
},
|
||||
OR: [
|
||||
{
|
||||
AND: [
|
||||
{
|
||||
isPrivate: true,
|
||||
},
|
||||
{
|
||||
userId: userId,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
isPrivate: false,
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const [count, items, max] = await this.prisma.$transaction([
|
||||
this.prisma.deck.count({
|
||||
where,
|
||||
}),
|
||||
this.prisma.deck.findMany({
|
||||
where,
|
||||
orderBy: {
|
||||
created: 'desc',
|
||||
},
|
||||
include: {
|
||||
author: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
skip: (currentPage - 1) * itemsPerPage,
|
||||
take: itemsPerPage,
|
||||
}),
|
||||
this.prisma
|
||||
.$queryRaw`SELECT MAX(card_count) as maxCardsCount FROM (SELECT COUNT(*) as card_count FROM card GROUP BY deckId) AS card_counts;`,
|
||||
])
|
||||
return {
|
||||
maxCardsCount: Number(max[0].maxCardsCount),
|
||||
...Pagination.transformPaginationData([count, items], { currentPage, itemsPerPage }),
|
||||
}
|
||||
} catch (e) {
|
||||
this.logger.error(e?.message)
|
||||
throw new InternalServerErrorException(e?.message)
|
||||
}
|
||||
}
|
||||
|
||||
public async findDeckById(id: string) {
|
||||
try {
|
||||
return await this.prisma.deck.findUnique({
|
||||
|
||||
Reference in New Issue
Block a user