From 0b426155acaf35436fe7ef596fadbf81a7969b94 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Sun, 15 Feb 2026 09:09:20 +0330 Subject: [PATCH] category --- .../products/controllers/product.controller.ts | 5 ++--- src/modules/products/providers/category.service.ts | 9 ++++----- src/modules/products/providers/product.service.ts | 12 ++++++++++-- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/modules/products/controllers/product.controller.ts b/src/modules/products/controllers/product.controller.ts index 4179f0c..120d404 100644 --- a/src/modules/products/controllers/product.controller.ts +++ b/src/modules/products/controllers/product.controller.ts @@ -28,9 +28,8 @@ export class FoodController { @Get('public/products/shop/:slug') @ApiOperation({ summary: 'Get all products by shop slug' }) @ApiHeader(API_HEADER_SLUG) - @ApiParam({ name: 'slug', required: true, description: 'Shop Slug' }) - findAllByRestaurant(@Param('slug') slug: string) { - return this.productService.findByShop(slug); + findAllByRestaurant(@Param('slug') slug: string, @Query('categoryId') categoryId?: string) { + return this.productService.findByShop(slug, categoryId); } @Get('public/products/:id') diff --git a/src/modules/products/providers/category.service.ts b/src/modules/products/providers/category.service.ts index 9c87798..735d11b 100644 --- a/src/modules/products/providers/category.service.ts +++ b/src/modules/products/providers/category.service.ts @@ -47,14 +47,13 @@ export class CategoryService { async findAllByShopSlug(shopSlug: string): Promise { return this.categoryRepository.find( { shop: { slug: shopSlug }, isActive: true, parent: null }, - { orderBy: { order: 'ASC' }, populate: ['children','parent'] }); + { orderBy: { order: 'ASC' }, populate: ['children', 'parent'] }); } async findAllByShopId(shopId: string): Promise { - console.log('findAllByShopId',shopId) return this.categoryRepository.find( - { shop: { id: shopId } }, - { orderBy: { createdAt: 'DESC' }, populate: ['children','parent'] }); + { shop: { id: shopId }, parent: null }, + { orderBy: { createdAt: 'DESC' }, populate: ['children', 'parent'] }); } async findOne(shopId: string, categoryId: string): Promise { @@ -96,7 +95,7 @@ export class CategoryService { } async findOrFail(categoryId: string) { - const category = await this.categoryRepository.findOne({ id: categoryId },{populate:['parent','children']}); + const category = await this.categoryRepository.findOne({ id: categoryId }, { populate: ['parent', 'children'] }); if (!category) throw new NotFoundException(CategoryMessage.NOT_FOUND); return category } diff --git a/src/modules/products/providers/product.service.ts b/src/modules/products/providers/product.service.ts index 5017d39..3ff55ff 100644 --- a/src/modules/products/providers/product.service.ts +++ b/src/modules/products/providers/product.service.ts @@ -100,11 +100,19 @@ export class ProductService { } - async findByShop(slug: string): Promise { + async findByShop(slug: string, categoryId?: string): Promise { const shop = await this.shopService.findBySlug(slug); + const where: { shop: { id: string }; isActive: boolean; category?: { id: string } } = { + shop: { id: shop.id }, + isActive: true, + }; + if (categoryId) { + where.category = { id: categoryId }; + } + return this.productRepository.find( - { shop: { id: shop.id }, isActive: true }, + where, { populate: ['category', 'variants'], orderBy: { order: 'asc' } } ); }