diff --git a/src/product/product-sub-categories.service.ts b/src/product/product-sub-categories.service.ts index 34e79a4..eb0aeab 100644 --- a/src/product/product-sub-categories.service.ts +++ b/src/product/product-sub-categories.service.ts @@ -44,10 +44,40 @@ export class ProductSubCategoriesService { ); } - findAll(page) { + async findAll(page, category) { + const baseQuery = this.productSubCategoriesRepository + .createQueryBuilder('product_sub_categories') + .leftJoinAndSelect( + 'product_sub_categories.category', + 'category', + ); + + if (category != 'null') { + baseQuery.where({ + category: category, + }); + } + + const data = await baseQuery + .skip(page * 10) + .take(10) + .getMany(); + + const totalData = await baseQuery.getCount(); + + return { + data, + count: totalData, + }; + } + + findAllByCategories(page, category) { return this.productSubCategoriesRepository.findAndCount({ skip: page * 10, take: 10, + where: { + category: category, + }, relations: ['category'], order: { version: 'DESC', diff --git a/src/product/product.controller.ts b/src/product/product.controller.ts index a3bea09..38aadc3 100644 --- a/src/product/product.controller.ts +++ b/src/product/product.controller.ts @@ -98,7 +98,7 @@ export class ProductController { @Get('by-categories-all') async findByCategoriesAll( @Query('page') page: number, - @Query('categories') categories: string, + @Query('sub-categories') categories: string, @Query('supplier') supplier: string, ) { const data = await this.productService.findAllBySubCategories( @@ -148,12 +148,11 @@ export class ProductController { } @Get('sub-categories') - async findAllSubCategories(@Query('page') page: number) { - const [data, count] = await this.productSubCategoriesService.findAll(page); + async findAllSubCategories(@Query('page') page: number,@Query('category') category: string) { + const data = await this.productSubCategoriesService.findAll(page, category); return { - data, - count, + ...data, statusCode: HttpStatus.OK, message: 'success', }; diff --git a/src/product/product.service.ts b/src/product/product.service.ts index 8e2980d..4e9c770 100644 --- a/src/product/product.service.ts +++ b/src/product/product.service.ts @@ -95,6 +95,9 @@ export class ProductService { } async findAllBySubCategories(page, subCategories, supplier) { + if (!supplier) { + supplier = await this.supplierService.findByActive(); + } const baseQuery = this.productRepository .createQueryBuilder('product') .leftJoin('product.sub_categories', 'sub_categories')