From d035e954f03d510fcd83e284bfea3b6e3e8b1dd5 Mon Sep 17 00:00:00 2001 From: Kosta <68782786+KostaD02@users.noreply.github.com> Date: Tue, 15 Aug 2023 17:47:15 +0400 Subject: [PATCH] refactor(shop): pagination part --- src/modules/shop/dtos/index.ts | 2 +- ...oduct.dto.ts => pagination-product.dto.ts} | 2 +- .../shop/product/products.controler.ts | 4 +-- src/modules/shop/product/products.service.ts | 28 ++++++++----------- 4 files changed, 16 insertions(+), 20 deletions(-) rename src/modules/shop/dtos/{all-product.dto.ts => pagination-product.dto.ts} (94%) diff --git a/src/modules/shop/dtos/index.ts b/src/modules/shop/dtos/index.ts index ca28bb3..7b8d8bd 100644 --- a/src/modules/shop/dtos/index.ts +++ b/src/modules/shop/dtos/index.ts @@ -2,4 +2,4 @@ export * from './create-product.dto'; export * from './product.dto'; export * from './update-product.dto'; export * from './search-product-query.dto'; -export * from './all-product.dto'; +export * from './pagination-product.dto'; diff --git a/src/modules/shop/dtos/all-product.dto.ts b/src/modules/shop/dtos/pagination-product.dto.ts similarity index 94% rename from src/modules/shop/dtos/all-product.dto.ts rename to src/modules/shop/dtos/pagination-product.dto.ts index 78cb0af..2de41ed 100644 --- a/src/modules/shop/dtos/all-product.dto.ts +++ b/src/modules/shop/dtos/pagination-product.dto.ts @@ -2,7 +2,7 @@ import { IsOptional, IsNumber, Min, Max } from 'class-validator'; import { API_CONFIG } from 'src/consts'; import { GlobalExceptionKeys } from 'src/enums'; -export class AllProductsQueryDto { +export class PaginationProductQueryDto { @IsOptional() @IsNumber({}, { message: GlobalExceptionKeys.PageIndexNotNumber }) @Min(API_CONFIG.MINIMUM_PAGE_INDEX, { diff --git a/src/modules/shop/product/products.controler.ts b/src/modules/shop/product/products.controler.ts index cedb66b..8bcc4d2 100644 --- a/src/modules/shop/product/products.controler.ts +++ b/src/modules/shop/product/products.controler.ts @@ -10,7 +10,7 @@ import { } from '@nestjs/common'; import { ProductsService } from './products.service'; import { - AllProductsQueryDto, + PaginationProductQueryDto, CreateProductDto, SearchProductsQueryDto, UpdateProductDto, @@ -39,7 +39,7 @@ export class ProductsController { } @Get('all') - getAllProduct(@Query() query: AllProductsQueryDto) { + getAllProduct(@Query() query: PaginationProductQueryDto) { return this.productsService.getAllProductsDetailed(query); } diff --git a/src/modules/shop/product/products.service.ts b/src/modules/shop/product/products.service.ts index 9461866..bddde07 100644 --- a/src/modules/shop/product/products.service.ts +++ b/src/modules/shop/product/products.service.ts @@ -6,7 +6,7 @@ import { CreateProductDto, SearchProductsQueryDto, UpdateProductDto, - AllProductsQueryDto, + PaginationProductQueryDto, } from '../dtos'; import { Product, ProductDocument } from 'src/schemas'; import { ExceptionService } from 'src/shared'; @@ -68,10 +68,16 @@ export class ProductsService { return this.productModel.find({}); } - async getAllProductsDetailed(query: AllProductsQueryDto) { + getPaginationData(query: { page_index: number; page_size: number }) { const currentPage = query.page_index || API_CONFIG.MINIMUM_PAGE_INDEX; const responsePerPage = query.page_size || API_CONFIG.RESPONSE_PER_PAGE; const skip = responsePerPage * (Math.floor(currentPage) - 1); + return { currentPage, responsePerPage, skip }; + } + + async getAllProductsDetailed(query: PaginationProductQueryDto) { + const { currentPage, responsePerPage, skip } = + this.getPaginationData(query); const products = await this.productModel .find({}) .sort({ 'price.current': 1 }) @@ -88,9 +94,8 @@ export class ProductsService { } async searchProduct(query: SearchProductsQueryDto) { - const currentPage = query.page_index || API_CONFIG.MINIMUM_PAGE_INDEX; - const responsePerPage = query.page_size || API_CONFIG.RESPONSE_PER_PAGE; - const skip = responsePerPage * (Math.floor(currentPage) - 1); + const { currentPage, responsePerPage, skip } = + this.getPaginationData(query); const queryObject: { 'price.current'?: object; 'category.id'?: string; @@ -154,6 +159,8 @@ export class ProductsService { total: productsCount, limit: responsePerPage, page: currentPage, + sortedBy: Object.keys(sortObject)[0], + sortedDirection: query.sort_direction ?? 'asc', skip, products, }; @@ -203,15 +210,4 @@ export class ProductsService { deleteAllProduct() { return this.productModel.deleteMany({}); } - - private checkQueryNumberParam(param: number, key: string) { - if (isNaN(param) || param <= 0) { - this.exceptionService.throwError( - ExceptionStatusKeys.BadRequest, - param <= 0 - ? `${key} should be greater than 0` - : `${key} should be number`, - ); - } - } }