mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2026-01-05 12:34:26 +00:00
fix pagination and create pagination service
This commit is contained in:
@@ -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