瑞吉外卖(26)- 添加购物车功能开发

需求分析

在这里插入图片描述

数据模型

在这里插入图片描述

代码开发

在这里插入图片描述

搭建框架

在这里插入图片描述

实体类ShoppingCart.java

package com.taotao.reggie.entity;

import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 购物车
 */
@Data
public class ShoppingCart implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;

    //名称
    private String name;

    //用户id
    private Long userId;

    //菜品id
    private Long dishId;

    //套餐id
    private Long setmealId;

    //口味
    private String dishFlavor;

    //数量
    private Integer number;

    //金额
    private BigDecimal amount;

    //图片
    private String image;

    private LocalDateTime createTime;
}

Mapper接口ShoppingCartMapper.java

package com.taotao.reggie.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.taotao.reggie.entity.ShoppingCart;
import org.apache.ibatis.annotations.Mapper;

/**
 * create by 刘鸿涛
 * 2022/9/29 11:19
 */
@SuppressWarnings({"all"})
@Mapper
public interface ShoppingCartMapper extends BaseMapper<ShoppingCart> {
    
}

业务层接口ShoppingCartService.java

package com.taotao.reggie.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.taotao.reggie.entity.ShoppingCart;

/**
 * create by 刘鸿涛
 * 2022/9/29 11:21
 */
@SuppressWarnings({"all"})
public interface ShoppingCartService extends IService<ShoppingCart> {
}

业务层实现类ShoppingCartServiceImpl.java

package com.taotao.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taotao.reggie.entity.ShoppingCart;
import com.taotao.reggie.mapper.ShoppingCartMapper;
import com.taotao.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * create by 刘鸿涛
 * 2022/9/29 11:23
 */
@SuppressWarnings({"all"})
@Service
@Slf4j
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
}

控制层ShoppingCartController.java

package com.taotao.reggie.controller;

import com.taotao.reggie.entity.ShoppingCart;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * create by 刘鸿涛
 * 2022/9/29 11:25
 */
@SuppressWarnings({"all"})
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartController shoppingCartController;

}

编写Controoler方法
package com.taotao.reggie.controller;

import com.taotao.reggie.common.R;
import com.taotao.reggie.entity.ShoppingCart;
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;

/**
 * create by 刘鸿涛
 * 2022/9/29 11:25
 */
@SuppressWarnings({"all"})
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartController shoppingCartController;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据:{}",shoppingCart);

        return null;
    }
}


验证页面

在这里插入图片描述

继续分析

查看数据库发现“number”属性,此属性代表dish_id相同的物品的数量,所谓点了“几份”
在这里插入图片描述

原子类

老师因为这个“购物车添加物品时,相同数量+1功能,扩展了一个原子类”,此类不受线程影响,对处理此功能更加方便。
课程中使用了number属性,如果此物品id被添加过,那么就让number + 1,否则此物品第一次添加默认为1,但是实际开发并不这样用
在这里插入图片描述
——乐观锁——在修改数据前会判断此数据有没有被修改过

完善ShoppingCartController.java方法

package com.taotao.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.taotao.reggie.common.BaseContext;
import com.taotao.reggie.common.R;
import com.taotao.reggie.entity.ShoppingCart;
import com.taotao.reggie.service.ShoppingCartService;
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;

import java.time.LocalDateTime;

/**
 * create by 刘鸿涛
 * 2022/9/29 11:25
 */
@SuppressWarnings({"all"})
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据:{}",shoppingCart);

        //设置用户id,指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //查询当前菜品或者套餐是否在购物车中
        Long dishId = shoppingCart.getDishId();


        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //where ShoppingCart.userId = currentId
        //eq方法目的是动态查询
        queryWrapper.eq(ShoppingCart::getUserId,currentId);

        if(dishId != null){
            //添加到购物车的是菜品
            //where art.getUserId = currentId
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else{
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        //SQL:select * from shopping_cart where user_id = ? and dish_id/setmeal_id = ?
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);

        if(cartServiceOne != null){
            //如果已经存在,就在原来的数量基础上加一
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number + 1);
            shoppingCartService.updateById(cartServiceOne);
        }else{
            //如果不存在,则添加到购物车,数量默认就是一
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }
        return R.success(cartServiceOne);
    }
}

功能测试

尝试添加“菜品”数据,数据库更新成功

在这里插入图片描述

在这里插入图片描述

尝试添加"套餐数据"

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鬼鬼骑士

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值