增加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";
}
});
};