From 3c8b01893797365c3c7179d956992c8144787960 Mon Sep 17 00:00:00 2001 From: ilham Date: Fri, 17 Dec 2021 01:32:45 +0700 Subject: [PATCH] add: get product all --- src/product/product.controller.ts | 24 +++++++++++---- src/product/product.service.ts | 51 +++++++++++++++++++++++++------ 2 files changed, 59 insertions(+), 16 deletions(-) diff --git a/src/product/product.controller.ts b/src/product/product.controller.ts index 312f8c2..2a0ef46 100644 --- a/src/product/product.controller.ts +++ b/src/product/product.controller.ts @@ -69,13 +69,22 @@ export class ProductController { }; } - @Get() - async findAll(@Query('page') page: number) { - const [data, count] = await this.productService.findAll(page); + @Get('get-all') + async findAll( + @Query('page') page: number, + @Query('sub-category') subcategory: string, + @Query('category') category: string, + @Query('supplier') supplier: string, + ) { + const data = await this.productService.findAll( + page, + supplier, + category, + subcategory, + ); return { - data, - count, + ...data, statusCode: HttpStatus.OK, message: 'success', }; @@ -148,7 +157,10 @@ export class ProductController { } @Get('sub-categories') - async findAllSubCategories(@Query('page') page: number,@Query('category') category: string) { + async findAllSubCategories( + @Query('page') page: number, + @Query('category') category: string, + ) { const data = await this.productSubCategoriesService.findAll(page, category); return { diff --git a/src/product/product.service.ts b/src/product/product.service.ts index da8e81a..daef8a0 100644 --- a/src/product/product.service.ts +++ b/src/product/product.service.ts @@ -53,15 +53,47 @@ export class ProductService { return this.productRepository.findOneOrFail(result.identifiers[0].id); } - findAll(page) { - return this.productRepository.findAndCount({ - skip: page * 10, - relations: ['sub_categories'], - take: 10, - order: { - version: 'DESC', - }, - }); + async findAll(page, supplier, categories, subCategories) { + if (supplier != 'null' && !supplier) { + supplier = (await this.supplierService.findByActive()).id; + } + const baseQuery = this.productRepository + .createQueryBuilder('product') + .leftJoin('product.sub_categories', 'sub_categories') + .leftJoin('sub_categories.category', 'category') + .where('product.supplier_id = :supplier_id', { + supplier_id: supplier, + }) + .leftJoinAndMapOne( + 'product.currentPrice', + 'product.priceHistory', + 'current_price', + 'current_price.partner_id is null', + ); + + if (subCategories != 'null' && subCategories) { + baseQuery.where('product.sub_categories_id = :id', { + id: subCategories, + }); + } + + if (categories != 'null' && categories) { + baseQuery.where('sub_categories.category_id = :id', { + id: categories, + }); + } + + const data = await baseQuery + .skip(page * 10) + .take(10) + .getMany(); + + const totalData = await baseQuery.getCount(); + + return { + data, + count: totalData, + }; } async findAllByCategories(page, subCategories, supplier) { @@ -113,7 +145,6 @@ export class ProductService { ); if (subCategories != 'null' && subCategories) { - console.log(!subCategories,"testingan") baseQuery.where('product.sub_categories_id = :id', { id: subCategories, });