fix pagination and create pagination service

This commit is contained in:
andres
2023-07-12 13:06:37 +02:00
parent 3db8bfb0f8
commit 68942e904f
14 changed files with 233 additions and 141 deletions

View File

@@ -26,8 +26,9 @@ import {
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'
import { GetAllDecksDto } from './dto/get-all-decks.dto'
import { GetAllCardsInDeckDto } from '../cards/dto/get-all-cards.dto'
import { CreateCardCommand } from './use-cases/create-card-use-case'
import { CreateCardCommand } from './use-cases'
import { CreateCardDto } from '../cards/dto/create-card.dto'
import { Pagination } from '../../infrastructure/common/pagination/pagination.service'
@Controller('decks')
export class DecksController {
@@ -43,7 +44,8 @@ export class DecksController {
@UseGuards(JwtAuthGuard)
@Get()
findAll(@Query() query: GetAllDecksDto, @Req() req) {
return this.commandBus.execute(new GetAllDecksCommand({ ...query, userId: req.user.id }))
const finalQuery = Pagination.getPaginationData(query)
return this.commandBus.execute(new GetAllDecksCommand({ ...finalQuery, userId: req.user.id }))
}
@UseGuards(JwtAuthGuard)

View File

@@ -1,10 +1,15 @@
import { IsOptional, IsUUID, Length } from 'class-validator'
import { IsUUID } from 'class-validator'
import { IsOptionalOrEmptyString } from '../../../infrastructure/decorators/is-optional-or-empty-string'
import { PaginationDto } from '../../../infrastructure/common/pagination/pagination.dto'
export class GetAllDecksDto extends PaginationDto {
@IsOptional()
@Length(3, 30)
@IsOptionalOrEmptyString()
minCardsCount?: string
@IsOptionalOrEmptyString()
maxCardsCount?: string
@IsOptionalOrEmptyString()
name?: string
@IsOptionalOrEmptyString()

View File

@@ -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({