File size: 2,027 Bytes
d9eec61
 
 
 
 
 
2fc4f90
 
 
 
 
 
d9eec61
 
 
 
 
 
 
 
2fc4f90
 
4ef06a6
2fc4f90
 
 
 
 
 
 
 
 
 
4ef06a6
2fc4f90
 
 
d9eec61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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();
  }
}