瑞吉外卖项目流程-分类相关功能

目录

一、新增分类

1、需求分析

2、代码开发

1)代码开发需要用到的类和接口

2)程序执行过程

3)页面实现分析

4)业务代码实现

二、分类信息分页查询

1、需求分析

2、程序执行流程及代码实现

1)程序执行流程

2)代码实现

三、删除分类

1、需求分析

2、代码开发

3、功能完善

四、修改分类

1、需求分析

2、代码实现


一、新增分类

1、需求分析

2、代码开发

1)代码开发需要用到的类和接口

2)程序执行过程

3)页面实现分析

分类管理的页面代码为backend/page/category/list.html ,当我们在页面中输入分类信息并点击确定之后,会执行submitForm()方法。

其中addCategory方法就是封装在category.js中的方法,用来发送请求。

4)业务代码实现

在com/itheima/reggie/controller/CategoryController.java中编写业务代码

package com.itheima.reggie.controller;


import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody Category category){
        log.info("category:{}",category);
        categoryService.save(category);
        return R.success("新增分类成功");
    }


}

二、分类信息分页查询

1、需求分析

2、程序执行流程及代码实现

1)程序执行流程

2)代码实现

在生命周期函数中的getCategoryPage方法是封装在backend/api/category.js中的方法,用于发送ajax请求。

在CategoryController中实现业务代码

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize){
        //分页构造器
        Page<Category> pageInfo=new Page<>(page,pageSize);

        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);

        //进行分页查询
        categoryService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }

三、删除分类

1、需求分析

2、代码开发

 点击页面的删除按钮的时候,会执行deleteHandle()方法并将要删除的对象的id动态传过来。

 其中deleCategory方法是封装在backend/api/category.js中,用于发送ajax请求。

并且删除成功之后会回调下面的部分,由于是对res.code进行判断,我们定义的R类中就有code,因此返回的提示信息定义为String类型就可以了。

先在CategoryController中实现简单的删除业务代码

3、功能完善

        实体类和Service的定义和之前的没什么区别,就不再展示,重点是我们要在CategoryService接口中自定义一个remove方法,通过这个方法来实现删除前对分类是否关联某一种菜品或套餐进行判断,并根据判断结果执行不同的行为。

        首先在接口中定义remove方法,然后在CategoryServiceImpl中实现这个方法。

package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService{
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    //根据id删除分类,删除之前需要进行判断
    @Override
    public void remove(Long id) {

        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        //添加查询条件,根据分类id进行查询
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count=dishService.count(dishLambdaQueryWrapper);
        //分类是否关联了菜品,如果已经关联,抛出一个业务异常
        if(count>0){
            //如果关联菜品,抛出业务异常
            throw new CustomException("当前分类下关联了菜品,不能删除");
        }

        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper=new LambdaQueryWrapper<>();
        //添加查询条件,根据分类id进行查询
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count2=setmealService.count(setmealLambdaQueryWrapper);
        //查询当前分类是否关联了套餐,如果已经关联,抛出一个业务异常
        if(count2>0){
            //如果关联套餐,抛出业务异常
            throw new CustomException("当前分类下关联了套餐,不能删除");
        }
        //正常删除分类
        super.removeById(id);

    }
}

其中异常CustomException是我们自定义的异常类,其继承RuntimeException,com/itheima/reggie/common/CustomException.java内容如下

package com.itheima.reggie.common;
public class CustomException extends RuntimeException{
    public CustomException(String message){
        super(message);
    }
}

 然后我们要到之前定义的全局异常处理器中将我们定义的异常进行处理,在com/itheima/reggie/common/GlobalExceptionHandler.java中加上一段

    @ExceptionHandler(CustomException.class)
    public R<String> exceptionHandler(CustomException ex){
        log.error(ex.getMessage());

        return R.error(ex.getMessage());

    }

四、修改分类

1、需求分析

2、代码实现

 在CategoryController中加一段

 /**
     * 根据id修改分类信息
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(Category category){
        log.info("修改分类信息:{}",category);
        categoryService.updateById(category);
        return R.success("修改成功");
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值