mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-17 12:33:22 +00:00
fix pagination and create pagination service
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { IsEmail, Length } from 'class-validator'
|
||||
import { IsEmail, Length, IsOptional } from 'class-validator'
|
||||
|
||||
export class RegistrationDto {
|
||||
@Length(3, 30)
|
||||
@IsOptional()
|
||||
name: string
|
||||
@Length(3, 30)
|
||||
password: string
|
||||
|
||||
@@ -1,32 +1,44 @@
|
||||
import { Injectable, InternalServerErrorException, Logger } from '@nestjs/common'
|
||||
import { PrismaService } from '../../../prisma.service'
|
||||
import { GetAllCardsInDeckDto } from '../dto/get-all-cards.dto'
|
||||
import {
|
||||
DEFAULT_PAGE_NUMBER,
|
||||
DEFAULT_PAGE_SIZE,
|
||||
} from '../../../infrastructure/common/pagination/pagination.constants'
|
||||
import { CreateCardDto } from '../dto/create-card.dto'
|
||||
|
||||
@Injectable()
|
||||
export class CardsRepository {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
private readonly logger = new Logger(CardsRepository.name)
|
||||
|
||||
async createCard(deckId: string, userId: string, card: CreateCardDto) {
|
||||
try {
|
||||
return await this.prisma.card.create({
|
||||
data: {
|
||||
user: {
|
||||
connect: {
|
||||
id: userId,
|
||||
return await this.prisma.$transaction(async tx => {
|
||||
const created = await tx.card.create({
|
||||
data: {
|
||||
author: {
|
||||
connect: {
|
||||
id: userId,
|
||||
},
|
||||
},
|
||||
decks: {
|
||||
connect: {
|
||||
id: deckId,
|
||||
},
|
||||
},
|
||||
|
||||
...card,
|
||||
},
|
||||
})
|
||||
await tx.deck.update({
|
||||
where: {
|
||||
id: deckId,
|
||||
},
|
||||
data: {
|
||||
cardsCount: {
|
||||
increment: 1,
|
||||
},
|
||||
},
|
||||
decks: {
|
||||
connect: {
|
||||
id: deckId,
|
||||
},
|
||||
},
|
||||
...card,
|
||||
},
|
||||
})
|
||||
return created
|
||||
})
|
||||
} catch (e) {
|
||||
this.logger.error(e?.message)
|
||||
@@ -36,12 +48,7 @@ export class CardsRepository {
|
||||
|
||||
async findCardsByDeckId(
|
||||
deckId: string,
|
||||
{
|
||||
answer = undefined,
|
||||
question = undefined,
|
||||
currentPage = DEFAULT_PAGE_NUMBER,
|
||||
pageSize = DEFAULT_PAGE_SIZE,
|
||||
}: GetAllCardsInDeckDto
|
||||
{ answer = undefined, question = undefined, currentPage, itemsPerPage }: GetAllCardsInDeckDto
|
||||
) {
|
||||
try {
|
||||
return await this.prisma.card.findMany({
|
||||
@@ -56,14 +63,15 @@ export class CardsRepository {
|
||||
contains: answer || undefined,
|
||||
},
|
||||
},
|
||||
skip: (currentPage - 1) * pageSize,
|
||||
take: pageSize,
|
||||
skip: (currentPage - 1) * itemsPerPage,
|
||||
take: 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({
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -22,8 +22,9 @@ export class UsersController {
|
||||
|
||||
@Get()
|
||||
async findAll(@Query() query) {
|
||||
const { page, pageSize, searchNameTerm, searchEmailTerm } = Pagination.getPaginationData(query)
|
||||
const users = await this.usersService.getUsers(page, pageSize, searchNameTerm, searchEmailTerm)
|
||||
const { page, pageSize } = Pagination.getPaginationData(query)
|
||||
|
||||
const users = await this.usersService.getUsers(page, pageSize, query.name, query.email)
|
||||
if (!users) throw new NotFoundException('Users not found')
|
||||
return users
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Prisma } from '@prisma/client'
|
||||
|
||||
export class User implements Prisma.UserUncheckedCreateInput {
|
||||
export class User implements Prisma.userUncheckedCreateInput {
|
||||
id: string
|
||||
email: string
|
||||
password: string
|
||||
|
||||
@@ -14,8 +14,8 @@ import {
|
||||
import { addHours } from 'date-fns'
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
import { PrismaService } from '../../../prisma.service'
|
||||
import { pick } from 'remeda'
|
||||
import { Prisma } from '@prisma/client'
|
||||
import { Pagination } from '../../../infrastructure/common/pagination/pagination.service'
|
||||
|
||||
@Injectable()
|
||||
export class UsersRepository {
|
||||
@@ -30,31 +30,29 @@ export class UsersRepository {
|
||||
searchEmailTerm: string
|
||||
): Promise<EntityWithPaginationType<UserViewType>> {
|
||||
try {
|
||||
const where = {
|
||||
const where: Prisma.userWhereInput = {
|
||||
name: {
|
||||
search: searchNameTerm || undefined,
|
||||
contains: searchNameTerm || undefined,
|
||||
},
|
||||
email: {
|
||||
search: searchEmailTerm || undefined,
|
||||
contains: searchEmailTerm || undefined,
|
||||
},
|
||||
}
|
||||
const [totalItems, users] = await this.prisma.$transaction([
|
||||
const res = await this.prisma.$transaction([
|
||||
this.prisma.user.count({ where }),
|
||||
this.prisma.user.findMany({
|
||||
where,
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
isEmailVerified: true,
|
||||
},
|
||||
skip: (currentPage - 1) * itemsPerPage,
|
||||
take: itemsPerPage,
|
||||
}),
|
||||
])
|
||||
const totalPages = Math.ceil(totalItems / itemsPerPage)
|
||||
const usersView = users.map(u => pick(u, ['id', 'name', 'email', 'isEmailVerified']))
|
||||
return {
|
||||
totalPages,
|
||||
currentPage,
|
||||
itemsPerPage,
|
||||
totalItems,
|
||||
items: usersView,
|
||||
}
|
||||
return Pagination.transformPaginationData(res, { currentPage, itemsPerPage })
|
||||
} catch (e) {
|
||||
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
if (e.code === 'P2025') {
|
||||
@@ -124,7 +122,7 @@ export class UsersRepository {
|
||||
}
|
||||
}
|
||||
|
||||
async findUserById(id: string, include?: Prisma.UserInclude) {
|
||||
async findUserById(id: string, include?: Prisma.userInclude) {
|
||||
try {
|
||||
const user = await this.prisma.user.findUnique({
|
||||
where: { id },
|
||||
@@ -134,7 +132,7 @@ export class UsersRepository {
|
||||
return null
|
||||
}
|
||||
|
||||
return user as Prisma.UserGetPayload<{ include: typeof include }>
|
||||
return user as Prisma.userGetPayload<{ include: typeof include }>
|
||||
} catch (e) {
|
||||
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
if (e.code === 'P2015') {
|
||||
|
||||
@@ -9,8 +9,8 @@ export class UsersService {
|
||||
|
||||
private logger = new Logger(UsersService.name)
|
||||
|
||||
async getUsers(page: number, pageSize: number, searchNameTerm: string, searchEmailTerm: string) {
|
||||
return await this.usersRepository.getUsers(page, pageSize, searchNameTerm, searchEmailTerm)
|
||||
async getUsers(page: number, pageSize: number, name: string, email: string) {
|
||||
return await this.usersRepository.getUsers(page, pageSize, name, email)
|
||||
}
|
||||
|
||||
async getUserById(id: string) {
|
||||
|
||||
Reference in New Issue
Block a user