add: get product all

This commit is contained in:
ilham 2021-12-17 01:32:45 +07:00
parent 8df907287a
commit 3c8b018937
2 changed files with 59 additions and 16 deletions

View File

@ -69,13 +69,22 @@ export class ProductController {
}; };
} }
@Get() @Get('get-all')
async findAll(@Query('page') page: number) { async findAll(
const [data, count] = await this.productService.findAll(page); @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 { return {
data, ...data,
count,
statusCode: HttpStatus.OK, statusCode: HttpStatus.OK,
message: 'success', message: 'success',
}; };
@ -148,7 +157,10 @@ export class ProductController {
} }
@Get('sub-categories') @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); const data = await this.productSubCategoriesService.findAll(page, category);
return { return {

View File

@ -53,15 +53,47 @@ export class ProductService {
return this.productRepository.findOneOrFail(result.identifiers[0].id); return this.productRepository.findOneOrFail(result.identifiers[0].id);
} }
findAll(page) { async findAll(page, supplier, categories, subCategories) {
return this.productRepository.findAndCount({ if (supplier != 'null' && !supplier) {
skip: page * 10, supplier = (await this.supplierService.findByActive()).id;
relations: ['sub_categories'], }
take: 10, const baseQuery = this.productRepository
order: { .createQueryBuilder('product')
version: 'DESC', .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) { async findAllByCategories(page, subCategories, supplier) {
@ -113,7 +145,6 @@ export class ProductService {
); );
if (subCategories != 'null' && subCategories) { if (subCategories != 'null' && subCategories) {
console.log(!subCategories,"testingan")
baseQuery.where('product.sub_categories_id = :id', { baseQuery.where('product.sub_categories_id = :id', {
id: subCategories, id: subCategories,
}); });