update
This commit is contained in:
@@ -31,7 +31,7 @@ export class CategoryController {
|
||||
@ApiParam({ name: 'slug', required: true, description: 'Shop Slug' })
|
||||
@ApiOkResponse({ description: 'List of all categories for the shop' })
|
||||
findAllByRestaurant(@Param('slug') slug: string) {
|
||||
return this.categoryService.findAllByRestaurant(slug);
|
||||
return this.categoryService.findAllByShop(slug);
|
||||
}
|
||||
|
||||
/*** Admin ***/
|
||||
@@ -51,7 +51,7 @@ export class CategoryController {
|
||||
@ApiBearerAuth()
|
||||
@Get('admin/categories')
|
||||
findAll(@ShopId() restId: string) {
|
||||
return this.categoryService.findAllByRestaurantId(restId);
|
||||
return this.categoryService.findAllByShopId(restId);
|
||||
}
|
||||
|
||||
@Permissions(Permission.MANAGE_CATEGORIES)
|
||||
|
||||
@@ -7,6 +7,9 @@ import { Shop } from '../../shops/entities/shop.entity';
|
||||
@Index({ properties: ['shop', 'isActive'] })
|
||||
@Index({ properties: ['isActive'] })
|
||||
export class Category extends BaseEntity {
|
||||
@ManyToOne(() => Category, { nullable: true })
|
||||
parent?: Category
|
||||
|
||||
@Property()
|
||||
title!: string;
|
||||
|
||||
|
||||
@@ -15,8 +15,8 @@ export class CategoryService {
|
||||
private readonly em: EntityManager,
|
||||
) { }
|
||||
|
||||
async create(restId: string, dto: CreateCategoryDto): Promise<Category> {
|
||||
const shop = await this.em.findOne(Shop, { id: restId });
|
||||
async create(shopId: string, dto: CreateCategoryDto): Promise<Category> {
|
||||
const shop = await this.em.findOne(Shop, { id: shopId });
|
||||
if (!shop) {
|
||||
throw new NotFoundException(RestMessage.NOT_FOUND);
|
||||
}
|
||||
@@ -32,7 +32,7 @@ export class CategoryService {
|
||||
return category;
|
||||
}
|
||||
|
||||
async findAllByRestaurant(slug: string): Promise<Category[]> {
|
||||
async findAllByShop(slug: string): Promise<Category[]> {
|
||||
const shop = await this.em.findOne(Shop, { slug });
|
||||
if (!shop || !shop.id) {
|
||||
throw new NotFoundException(RestMessage.NOT_FOUND);
|
||||
@@ -41,14 +41,14 @@ export class CategoryService {
|
||||
{ shop: shop, isActive: true }, { orderBy: { order: 'ASC' } });
|
||||
}
|
||||
|
||||
async findAllByRestaurantId(restId: string): Promise<Category[]> {
|
||||
async findAllByShopId(shopId: string): Promise<Category[]> {
|
||||
return this.categoryRepository.find(
|
||||
{ shop: { id: restId } },
|
||||
{ shop: { id: shopId } },
|
||||
{ orderBy: { order: 'asc' } });
|
||||
}
|
||||
|
||||
async findOne(restId: string, id: string): Promise<Category> {
|
||||
const cat = await this.categoryRepository.findOne({ id, shop: { id: restId } }, { populate: ['products'] });
|
||||
async findOne(shopId: string, id: string): Promise<Category> {
|
||||
const cat = await this.categoryRepository.findOne({ id, shop: { id: shopId } }, { populate: ['products'] });
|
||||
if (!cat) throw new NotFoundException(CategoryMessage.NOT_FOUND);
|
||||
|
||||
return {
|
||||
@@ -64,16 +64,16 @@ export class CategoryService {
|
||||
}
|
||||
|
||||
|
||||
async update(restId: string, id: string, dto: UpdateCategoryDto): Promise<Category> {
|
||||
const cat = await this.categoryRepository.findOne({ id, shop: { id: restId } });
|
||||
async update(shopId: string, id: string, dto: UpdateCategoryDto): Promise<Category> {
|
||||
const cat = await this.categoryRepository.findOne({ id, shop: { id: shopId } });
|
||||
if (!cat) throw new NotFoundException(CategoryMessage.NOT_FOUND);
|
||||
this.em.assign(cat, dto);
|
||||
await this.em.persistAndFlush(cat);
|
||||
return cat;
|
||||
}
|
||||
|
||||
async remove(restId: string, id: string): Promise<void> {
|
||||
const cat = await this.categoryRepository.findOne({ id, shop: { id: restId } });
|
||||
async remove(shopId: string, id: string): Promise<void> {
|
||||
const cat = await this.categoryRepository.findOne({ id, shop: { id: shopId } });
|
||||
if (!cat) throw new NotFoundException(CategoryMessage.NOT_FOUND);
|
||||
cat.deletedAt = new Date();
|
||||
await this.em.persistAndFlush(cat);
|
||||
|
||||
@@ -71,8 +71,8 @@ export class ProductService {
|
||||
return product;
|
||||
}
|
||||
|
||||
findAll(restId: string, dto: FindProductsDto) {
|
||||
return this.productRepository.findAllPaginated(restId, dto);
|
||||
findAll(shopId: string, dto: FindProductsDto) {
|
||||
return this.productRepository.findAllPaginated(shopId, dto);
|
||||
}
|
||||
|
||||
|
||||
@@ -113,14 +113,15 @@ export class ProductService {
|
||||
});
|
||||
}
|
||||
|
||||
async update(restId: string, productId: string, dto: Partial<CreateProductDto>): Promise<Product> {
|
||||
|
||||
async update(shopId: string, productId: string, dto: Partial<CreateProductDto>): Promise<Product> {
|
||||
const { categoryId, variants, ...rest } = dto;
|
||||
|
||||
const product = await this.findOrFail(productId)
|
||||
|
||||
// attach new categories if provided (adds, does not attempt to preserve/remove existing ones)
|
||||
if (categoryId) {
|
||||
const category = await this.categoryRepository.findOne({ id: categoryId, shop: { id: restId } });
|
||||
const category = await this.categoryRepository.findOne({ id: categoryId, shop: { id: shopId } });
|
||||
|
||||
if (!category) {
|
||||
throw new NotFoundException(CategoryMessage.NOT_FOUND);
|
||||
@@ -178,6 +179,7 @@ export class ProductService {
|
||||
|
||||
return product;
|
||||
}
|
||||
|
||||
|
||||
async remove(shopId: string, id: string): Promise<void> {
|
||||
const product = await this.findOrFail(id)
|
||||
@@ -207,8 +209,8 @@ export class ProductService {
|
||||
}
|
||||
|
||||
|
||||
async getMyFavorites(userId: string, restId: string) {
|
||||
return this.em.find(Favorite, { user: userId, product: { shop: { id: restId } } }, { populate: ['product'] });
|
||||
async getMyFavorites(userId: string, shopId: string) {
|
||||
return this.em.find(Favorite, { user: userId, product: { shop: { id: shopId } } }, { populate: ['product'] });
|
||||
}
|
||||
/**
|
||||
* Helper
|
||||
|
||||
Reference in New Issue
Block a user