mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-17 05:09:26 +00:00
fix pagination and /users
This commit is contained in:
@@ -1,9 +1,14 @@
|
|||||||
import { isObject } from 'remeda'
|
import { isObject } from 'remeda'
|
||||||
|
|
||||||
import { DEFAULT_PAGE_NUMBER, DEFAULT_PAGE_SIZE } from './pagination.constants'
|
import { DEFAULT_PAGE_NUMBER, DEFAULT_PAGE_SIZE } from './pagination.constants'
|
||||||
|
interface PaginationQuery {
|
||||||
|
currentPage?: string
|
||||||
|
itemsPerPage?: string
|
||||||
|
}
|
||||||
export class Pagination {
|
export class Pagination {
|
||||||
static getPaginationData<T>(query: T) {
|
static getPaginationData<T extends Partial<PaginationQuery>>(
|
||||||
|
query: T
|
||||||
|
): { currentPage: number; itemsPerPage: number } & Omit<T, keyof PaginationQuery> {
|
||||||
if (!isObject(query)) throw new Error('Pagination.getPaginationData: query is not an object')
|
if (!isObject(query)) throw new Error('Pagination.getPaginationData: query is not an object')
|
||||||
|
|
||||||
const currentPage =
|
const currentPage =
|
||||||
@@ -12,6 +17,7 @@ export class Pagination {
|
|||||||
!isNaN(Number(query.currentPage))
|
!isNaN(Number(query.currentPage))
|
||||||
? +query.currentPage
|
? +query.currentPage
|
||||||
: DEFAULT_PAGE_NUMBER
|
: DEFAULT_PAGE_NUMBER
|
||||||
|
|
||||||
const itemsPerPage =
|
const itemsPerPage =
|
||||||
'itemsPerPage' in query &&
|
'itemsPerPage' in query &&
|
||||||
typeof query.itemsPerPage === 'string' &&
|
typeof query.itemsPerPage === 'string' &&
|
||||||
@@ -19,7 +25,9 @@ export class Pagination {
|
|||||||
? +query.itemsPerPage
|
? +query.itemsPerPage
|
||||||
: DEFAULT_PAGE_SIZE
|
: DEFAULT_PAGE_SIZE
|
||||||
|
|
||||||
return { currentPage, itemsPerPage, ...query }
|
const { currentPage: _, itemsPerPage: __, ...rest } = query
|
||||||
|
|
||||||
|
return { currentPage, itemsPerPage, ...rest }
|
||||||
}
|
}
|
||||||
|
|
||||||
static transformPaginationData<T>(
|
static transformPaginationData<T>(
|
||||||
|
|||||||
@@ -25,9 +25,14 @@ export class UsersController {
|
|||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
async findAll(@Query() query) {
|
async findAll(@Query() query) {
|
||||||
const { page, pageSize } = Pagination.getPaginationData(query)
|
const { currentPage, itemsPerPage } = Pagination.getPaginationData(query)
|
||||||
|
|
||||||
const users = await this.usersService.getUsers(page, pageSize, query.name, query.email)
|
const users = await this.usersService.getUsers(
|
||||||
|
currentPage,
|
||||||
|
itemsPerPage,
|
||||||
|
query.name,
|
||||||
|
query.email
|
||||||
|
)
|
||||||
|
|
||||||
if (!users) throw new NotFoundException('Users not found')
|
if (!users) throw new NotFoundException('Users not found')
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user