在 foodie-dev-api 模块中完善 ShopcartController 类,添加 del 方法
package com.imooc.controller;
import com.imooc.pojo.bo.ShopcartBO;
import com.imooc.utils.IMOOCJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author 92578
* @since 1.0
*/
@Api(value = "购物车接口 controller", tags = {"购物车接口相关的 api"})
@RequestMapping("shopcart")
@RestController
public class ShopcartController {
@ApiOperation(value = "添加商品到购物车", notes = "添加商品到购物车", httpMethod = "POST")
@PostMapping("/add")
public IMOOCJSONResult add(@RequestParam String userId,
@RequestBody ShopcartBO shopcartBO,
HttpServletRequest request,
HttpServletResponse response) {
if (StringUtils.isBlank(userId)) {
return IMOOCJSONResult.errorMsg("");
}
System.out.println(shopcartBO);
// TODO 前端用户在登录的情况下,添加商品到购物车,会同时在后端同步购物车到 redis 缓存
return IMOOCJSONResult.ok();
}
@ApiOperation(value = "从购物车中删除商品", notes = "从购物车中删除商品", httpMethod = "POST")
@PostMapping("/del")
public IMOOCJSONResult del(@RequestParam String userId,
@RequestBody String itemSpecId,
HttpServletRequest request,
HttpServletResponse response) {
if (StringUtils.isBlank(userId)) {
return IMOOCJSONResult.errorMsg("参数不能为空");
}
// TODO 用户在页面删除购物车中的商品数据,如果此时用户已经登录,则x需要同步删除后端购物车中的商品
return IMOOCJSONResult.ok();
}
}
启动项目,访问 http://localhost:8080/foodie-shop/index.html
登录账号“imooc”,密码“123123”,随便添加几样商品到购物车,进入购物车页面,点击删除,商品删除成功
同时选中两样商品,删除其中一项观察结果,发现结算金额经过重新计算