springboot整合项目-商城项目展示购物车商品数量增加or减少功能

增加or减少购物车商品数量

持久层

1.sql规划

1.增加之前先判断是否存在这个数据

select * from t_cart where cid = #{cid}

增加购物车数量,就是update操作

update  t_cart set num=#{num},modified_time =? modified_user =? where cid = #{cid}
2.接口与抽象方法
/**
     * 根据cid查找此条数据是否存在
     * @param cid
     * @return
     */
   Cart findByCid(Integer cid);

业务层

1.规划异常

1.更新时异常 updateException
2.找不到此条数据 CartNotFoundException

2.接口与方法的设计
/**
     * 增加购物车里面的商品数量
     * @param cid
     * @param uid
     * @param username
     * @return
     */
  Integer updateNumByCid(Integer cid,Integer uid,String username);

  /**
   * 减少购物车里面的商品数量
   * @param cid
   * @param uid
   * @param username
   * @return
   */
  Integer reduceNum(Integer cid,Integer uid,String username);
/**
     * 增加购物车里的数量
     * @param cid
     * @param uid
     * @param username
     * @return
     */
    @Override
    public Integer updateNumByCid(Integer cid, Integer uid, String username) {
        //先查找此数据是否存在
        Cart result = cartMapper.findByCid(cid);
        if (result == null) {
            throw new CartNotFoundException("此商品找不到的异常");
        }
        if (!result.getUid().equals(uid)){
            throw new AccessDeniedException("访问非法");
        }

        int num = result.getNum()+1;
        Integer integer = cartMapper.updateNumerByCid(cid, num, username, new Date());
        if (integer !=1){
            throw new UpdateException("更新时产生了位置的异常");
        }
        return num;
    }

    /**
     * 减少购物车的数量
     * @param cid
     * @param uid
     * @param username
     * @return
     */
    @Override
    public Integer reduceNum(Integer cid, Integer uid, String username) {
        //先查找此数据是否存在
        Cart result = cartMapper.findByCid(cid);
        if (result == null) {
            throw new CartNotFoundException("此商品找不到的异常");
        }
        if (!result.getUid().equals(uid)){
            throw new AccessDeniedException("访问非法");
        }

        int num = result.getNum()-1;
        Integer integer = cartMapper.updateNumerByCid(cid, num, username, new Date());
        if (integer !=1){
            throw new UpdateException("更新时产生了位置的异常");
        }
        return num;
    }
测试

控制层

1.异常处理
else if (e instanceof CartNotFoundException) {
            result.setState(4007);
        }
2.请求设计

1.增加
/{cid}/num/add
/post
/HttpSession session Integer cid
/JsonResult
2.减少
/{cid}/num/reduce
/post
/HttpSession session Integer cid
/JsonResult

3.方法以及实现的逻辑
/**
     * 增加购物车某个商品的数量
     * @param cid
     * @param session
     * @return
     */
    @RequestMapping("{cid}/num/add")
    public JsonResult<Integer> addNum(@PathVariable("cid") Integer cid, HttpSession session) {
        // 从Session中获取uid和username
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        // 调用业务对象执行增加数量
        Integer data = cartService.updateNumByCid(cid, uid, username);
        // 返回成功
        return new JsonResult<Integer>(OK, data);
    }

    /**
     * 减少某个商品的数量
     * @param cid
     * @param session
     * @return
     */
    @RequestMapping("{cid}/num/reduce")
    public JsonResult<Integer> reduceNum(@PathVariable("cid") Integer cid, HttpSession session) {
        // 从Session中获取uid和username
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        // 调用业务对象执行增加数量
        Integer data = cartService.reduceNum(cid,uid,username);
        // 返回成功
        return new JsonResult<Integer>(OK, data);
    }

前端页面

1.
function reduceNum(cid) {
				$.ajax({
					url: "/carts/" + cid + "/num/reduce",
					type: "POST",
					dataType: "JSON",
					success: function(json) {
						if (json.state == 200) {
							// showCartList();
							$("#num-" - cid).val(json.data);
							let price = $("#price-" - cid).html();
							let totalPrice = price * json.data;
							$("#total-price-" - cid).html(totalPrice);
							showCartList();
						} else {
							alert("增加商品数量失败!" + json.message);
						}
					},
					error: function(xhr) {
						alert("您的登录信息已经过期,请重新登录!HTTP响应码:" + xhr.status);
						location.href = "login.html";
					}
				});
			};
			function addNum(cid) {
				$.ajax({
					url: "/carts/" + cid + "/num/add",
					type: "POST",
					dataType: "JSON",
					success: function(json) {
						if (json.state == 200) {
							// showCartList();
							$("#num-" + cid).val(json.data);
							let price = $("#price-" + cid).html();
							let totalPrice = price * json.data;
							$("#total-price-" +cid).html(totalPrice);
						} else {
							alert("增加商品数量失败!" + json.message);
						}
					},
					error: function(xhr) {
						alert("您的登录信息已经过期,请重新登录!HTTP响应码:" + xhr.status);
						location.href = "login.html";
					}
				});
			};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值