Trần Viết Sơn
add branch and branch menu
f97bd0c
raw
history blame
1.48 kB
import {
BadRequestException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { CreateBranchDto } from './dto/create-branch.dto.js';
import { BranchEntity } from '../../entities/branch.entity.js';
import { Public } from '../authentication/authentication.decorator.js';
import { UpdateBranchDto } from './dto/update-branch.dto.js';
import { plainToClass } from 'class-transformer';
@Public()
@Injectable()
export class BranchService {
async create(createBranchDto: CreateBranchDto) {
const branch = await BranchEntity.findOneBy({ id: createBranchDto.id });
if (branch) {
throw new BadRequestException('Branch already exists');
}
return await BranchEntity.create({ ...createBranchDto }).save();
}
async findAll() {
return await BranchEntity.find();
}
async findOne(id: string) {
return await BranchEntity.findOneBy({ id: id });
}
async getBranchOrError(id: string) {
console.log(id);
const branch = await BranchEntity.findOneBy({ id });
if (!branch) {
throw new NotFoundException('Branch not found');
}
return branch;
}
async update(id: string, updateBranchDto: UpdateBranchDto) {
let branch = await this.getBranchOrError(id);
branch = plainToClass(BranchEntity, {
...branch,
...updateBranchDto,
});
return await branch.save();
}
async remove(id: string) {
let menuItem = await this.getBranchOrError(id);
return await menuItem.remove();
}
}