Trần Viết Sơn
add branch and branch menu
f97bd0c
raw
history blame
2.43 kB
import { Injectable } from '@nestjs/common';
import { CreateBranchMenuDto } from './dto/create-branch-menu.dto.js';
import { UpdateBranchMenuDto } from './dto/update-branch-menu.dto.js';
import { BranchService } from '../branch/branch.service.js';
import { MenuItemService } from '../menu-item/menu-item.service.js';
import { BranchMenuEntity } from '../../entities/branch-menu.entity.js';
import { paginate, PaginateConfig, PaginateQuery } from 'nestjs-paginate';
import { isUUID } from 'class-validator';
@Injectable()
export class BranchMenusService {
constructor(
private readonly branchService: BranchService,
private readonly menuItemService: MenuItemService,
) {}
async create(branchId: string, createBranchMenuDto: CreateBranchMenuDto) {
const branch = await this.branchService.getBranchOrError(branchId);
const menuItem = await this.menuItemService.getMenuItemOrError(
createBranchMenuDto.menu_id,
);
if (createBranchMenuDto.description) {
return await BranchMenuEntity.create({
...createBranchMenuDto,
branch_id: branchId,
}).save();
} else {
return await BranchMenuEntity.create({
...createBranchMenuDto,
branch_id: branchId,
description: menuItem.description,
}).save();
}
}
async findAll(branchId: string, query: PaginateQuery) {
const paginateConfig: PaginateConfig<BranchMenuEntity> = {
sortableColumns: ['id', 'branch_id', 'menu_id', 'description'],
nullSort: 'last',
defaultSortBy: [['id', 'DESC']],
searchableColumns: ['description'],
filterableColumns: {
// price: [],
// item_type: [FilterOperator.EQ],
},
};
return paginate(
query,
BranchMenuEntity.createQueryBuilder('bm')
.leftJoinAndSelect('bm.menu_item', 'menu_item')
.where('bm.branch_id = :branchId', { branchId: branchId }),
paginateConfig,
);
}
async findOne(branchId: string, id: string) {
if (isUUID(id)) return await BranchMenuEntity.findOneBy({ id });
else {
return await BranchMenuEntity.findOne({
where: { branch_id: branchId, menu_id: id },
relations: ['menu_item'],
});
}
}
update(id: number, updateBranchMenuDto: UpdateBranchMenuDto) {
return `This action updates a #${id} branchMenu`;
}
remove(id: number) {
return `This action removes a #${id} branchMenu`;
}
}