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