コード例 #1
0
  /**
   * 根据菜品ID查找菜品列表
   *
   * @param categoryId 菜品ID
   * @return 菜品实体列表
   */
  public List<Dish> getDishesByCategoryId(Long categoryId) {
    List<Dish> result = new ArrayList<Dish>();

    Category category = categoryDao.get(categoryId);

    if (null != category) {
      for (Dish dish : category.getDishes()) {
        // REST服务只获取在售的菜品
        if (dish.isOnSell()) {
          result.add(dish);
        }
      }
    }

    return result;
  }
コード例 #2
0
  /**
   * 删除菜品分类
   *
   * @param categoryId 菜品分类id
   * @throws CategoryNotFoundException 菜品分类不存在异常
   */
  @Transactional(
      propagation = Propagation.REQUIRED,
      readOnly = false,
      rollbackFor = Exception.class)
  public void deleteCategory(Long categoryId) throws CategoryNotFoundException {
    // 1. 检查需要删除的菜品分类是否存在
    Category category = this.getCategoryById(categoryId);

    // 2. 将要删除菜品分类下的菜品重置为默认分类
    Category defaultCategory = categoryDao.getCategoryByName(Constants.DEFAULT_CATEGORY);

    if (null == defaultCategory) {
      throw new CategoryNotFoundException(
          MessageUtil.getMessage("category_name_msg", Constants.DEFAULT_CATEGORY));
    } else {
      for (Dish dish : category.getDishes()) {
        dish.setCategory(defaultCategory);
        dishDao.update(dish);
      }
    }

    // 3. 删除菜品分类
    categoryDao.delete(category);
  }