Skip to content

Commit

Permalink
refactor(shop): pagination part
Browse files Browse the repository at this point in the history
  • Loading branch information
KostaD02 committed Aug 15, 2023
1 parent d10b34d commit d035e95
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/modules/shop/dtos/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/shop/product/products.controler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '@nestjs/common';
import { ProductsService } from './products.service';
import {
AllProductsQueryDto,
PaginationProductQueryDto,
CreateProductDto,
SearchProductsQueryDto,
UpdateProductDto,
Expand Down Expand Up @@ -39,7 +39,7 @@ export class ProductsController {
}

@Get('all')
getAllProduct(@Query() query: AllProductsQueryDto) {
getAllProduct(@Query() query: PaginationProductQueryDto) {
return this.productsService.getAllProductsDetailed(query);
}

Expand Down
28 changes: 12 additions & 16 deletions src/modules/shop/product/products.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
CreateProductDto,
SearchProductsQueryDto,
UpdateProductDto,
AllProductsQueryDto,
PaginationProductQueryDto,
} from '../dtos';
import { Product, ProductDocument } from 'src/schemas';
import { ExceptionService } from 'src/shared';
Expand Down Expand Up @@ -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 })
Expand All @@ -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;
Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -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`,
);
}
}
}

0 comments on commit d035e95

Please sign in to comment.