Skip to content

Commit

Permalink
feat(shop): add pagination to some endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
KostaD02 committed Aug 15, 2023
1 parent d035e95 commit 3c76eda
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
14 changes: 10 additions & 4 deletions src/modules/shop/product/products.controler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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);
}
}
47 changes: 37 additions & 10 deletions src/modules/shop/product/products.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down Expand Up @@ -181,11 +183,24 @@ export class ProductsService {
return category;
}

async getByCategoryId(categoryId: string): Promise<Product[]> {
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<string[]> {
Expand All @@ -199,12 +214,24 @@ export class ProductsService {
return brands;
}

async getBrandProducts(brandName: string): Promise<Product[]> {
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() {
Expand Down

0 comments on commit 3c76eda

Please sign in to comment.