feat: add filter on endpoint product by sub category

This commit is contained in:
caturbgs 2021-12-22 13:42:38 +07:00
parent ad81712bfa
commit 41856f7a02

View File

@ -1,5 +1,5 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { EntityNotFoundError, Repository } from 'typeorm';
import { EntityNotFoundError, In, Repository } from 'typeorm';
import { ProductSubCategories } from './entities/product-sub-category.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { CreateSubCategoriesProductDto } from './dto/sub-categories/create-sub-categories-product.dto';
@ -45,13 +45,19 @@ export class ProductSubCategoriesService {
}
async findAll(page, category) {
let filterCategories = [];
if (category !== 'null') {
filterCategories = category.split(',').map((data) => data.trim());
}
const baseQuery = this.productSubCategoriesRepository
.createQueryBuilder('product_sub_categories')
.leftJoinAndSelect('product_sub_categories.category', 'category');
if (category != 'null' && category) {
if (filterCategories.length > 0) {
baseQuery.where({
category: category,
category: In(filterCategories),
});
}