From 3c76eda482ca7f44d6aec2ed32d7cbc952e7615f Mon Sep 17 00:00:00 2001 From: Kosta <68782786+KostaD02@users.noreply.github.com> Date: Tue, 15 Aug 2023 21:38:16 +0400 Subject: [PATCH] feat(shop): add pagination to some endpoints --- .../shop/product/products.controler.ts | 14 ++++-- src/modules/shop/product/products.service.ts | 47 +++++++++++++++---- 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/src/modules/shop/product/products.controler.ts b/src/modules/shop/product/products.controler.ts index 8bcc4d2..56ba399 100644 --- a/src/modules/shop/product/products.controler.ts +++ b/src/modules/shop/product/products.controler.ts @@ -59,8 +59,11 @@ export class ProductsController { } @Get('category/:category_id') - getProductsByCategoryId(@Param('category_id') id: string) { - return this.productsService.getByCategoryId(id); + getProductsByCategoryId( + @Param('category_id') id: string, + @Query() query: PaginationProductQueryDto, + ) { + return this.productsService.getByCategoryId(id, query); } @Get('brands') @@ -69,7 +72,10 @@ export class ProductsController { } @Get('brand/:brand_name') - getBrandProducts(@Param('brand_name') brandName: string) { - return this.productsService.getBrandProducts(brandName); + getBrandProducts( + @Param('brand_name') brandName: string, + @Query() query: PaginationProductQueryDto, + ) { + return this.productsService.getBrandProducts(brandName, query); } } diff --git a/src/modules/shop/product/products.service.ts b/src/modules/shop/product/products.service.ts index bddde07..68b2f8d 100644 --- a/src/modules/shop/product/products.service.ts +++ b/src/modules/shop/product/products.service.ts @@ -149,7 +149,9 @@ export class ProductsService { } else { sortObject['price.current'] = 1; } - const productsCount = await this.productModel.countDocuments({}); + const productsCount = await this.productModel.countDocuments({ + ...queryObject, + }); const products = await this.productModel .find({ ...queryObject }) .sort({ ...sortObject }) @@ -181,11 +183,24 @@ export class ProductsService { return category; } - async getByCategoryId(categoryId: string): Promise { - const products = await this.productModel.find({ + async getByCategoryId(categoryId: string, query: PaginationProductQueryDto) { + const { currentPage, responsePerPage, skip } = + this.getPaginationData(query); + const products = await this.productModel + .find({ 'category.id': categoryId }) + .sort({ 'price.current': 1 }) + .limit(responsePerPage) + .skip(skip); + const productsCount = await this.productModel.countDocuments({ 'category.id': categoryId, }); - return products; + return { + total: productsCount, + limit: responsePerPage, + page: currentPage, + skip, + products, + }; } async getBrands(): Promise { @@ -199,12 +214,24 @@ export class ProductsService { return brands; } - async getBrandProducts(brandName: string): Promise { - const products = await this.getAllProduct(); - const brands = products.filter( - (product) => product.brand.toLowerCase() === brandName.toLowerCase(), - ); - return brands; + async getBrandProducts(brandName: string, query: PaginationProductQueryDto) { + const { currentPage, responsePerPage, skip } = + this.getPaginationData(query); + const products = await this.productModel + .find({ brand: brandName }) + .sort({ 'price.current': 1 }) + .limit(responsePerPage) + .skip(skip); + const productsCount = await this.productModel.countDocuments({ + brand: brandName, + }); + return { + total: productsCount, + limit: responsePerPage, + page: currentPage, + skip, + products, + }; } deleteAllProduct() {