Artteiv's picture
fix: update menu item
4ef06a6
import { Injectable, NotFoundException } from '@nestjs/common';
import { CreateMenuItemDto } from './dto/create-menu-item.dto.js';
import { UpdateMenuItemDto } from './dto/update-menu-item.dto.js';
import { MenuItemEntity } from '../../entities/menu-item.entity.js';
import { Public } from '../authentication/authentication.decorator.js';
import { plainToClass } from 'class-transformer';
import {
FilterOperator,
paginate,
PaginateConfig,
PaginateQuery,
} from 'nestjs-paginate';
@Public()
@Injectable()
export class MenuItemService {
async create(createMenuItemDto: CreateMenuItemDto) {
return await MenuItemEntity.create({ ...createMenuItemDto }).save();
}
async findAll(query: PaginateQuery) {
const paginateConfig: PaginateConfig<MenuItemEntity> = {
sortableColumns: ['id', 'item_type', 'item_name', 'price', 'create_at'],
nullSort: 'last',
defaultSortBy: [['id', 'DESC']],
searchableColumns: ['item_name'],
filterableColumns: {
price: [
FilterOperator.LT,
FilterOperator.LTE,
FilterOperator.GT,
FilterOperator.GTE,
],
item_type: [FilterOperator.EQ]
},
};
return paginate(query, MenuItemEntity.createQueryBuilder(), paginateConfig);
}
async findOne(id: string) {
return await MenuItemEntity.findOneBy({ id: id });
}
async getMenuItemOrError(id: string) {
let menuItem = await MenuItemEntity.findOneBy({ id });
if (!menuItem) {
throw new NotFoundException('Menu item not found');
}
return menuItem;
}
async update(id: string, updateMenuItemDto: UpdateMenuItemDto) {
let menuItem = await this.getMenuItemOrError(id);
menuItem = plainToClass(MenuItemEntity, {
...menuItem,
...updateMenuItemDto,
});
return await menuItem.save();
}
async remove(id: string) {
let menuItem = await this.getMenuItemOrError(id);
return await menuItem.remove();
}
}