在 foodie-dev-api 模块中完善 ShopcartController 类,添加 del 方法

    1. package com.imooc.controller;
    2. import com.imooc.pojo.bo.ShopcartBO;
    3. import com.imooc.utils.IMOOCJSONResult;
    4. import io.swagger.annotations.Api;
    5. import io.swagger.annotations.ApiOperation;
    6. import org.apache.commons.lang3.StringUtils;
    7. import org.springframework.web.bind.annotation.*;
    8. import javax.servlet.http.HttpServletRequest;
    9. import javax.servlet.http.HttpServletResponse;
    10. /**
    11. * @author 92578
    12. * @since 1.0
    13. */
    14. @Api(value = "购物车接口 controller", tags = {"购物车接口相关的 api"})
    15. @RequestMapping("shopcart")
    16. @RestController
    17. public class ShopcartController {
    18. @ApiOperation(value = "添加商品到购物车", notes = "添加商品到购物车", httpMethod = "POST")
    19. @PostMapping("/add")
    20. public IMOOCJSONResult add(@RequestParam String userId,
    21. @RequestBody ShopcartBO shopcartBO,
    22. HttpServletRequest request,
    23. HttpServletResponse response) {
    24. if (StringUtils.isBlank(userId)) {
    25. return IMOOCJSONResult.errorMsg("");
    26. }
    27. System.out.println(shopcartBO);
    28. // TODO 前端用户在登录的情况下,添加商品到购物车,会同时在后端同步购物车到 redis 缓存
    29. return IMOOCJSONResult.ok();
    30. }
    31. @ApiOperation(value = "从购物车中删除商品", notes = "从购物车中删除商品", httpMethod = "POST")
    32. @PostMapping("/del")
    33. public IMOOCJSONResult del(@RequestParam String userId,
    34. @RequestBody String itemSpecId,
    35. HttpServletRequest request,
    36. HttpServletResponse response) {
    37. if (StringUtils.isBlank(userId)) {
    38. return IMOOCJSONResult.errorMsg("参数不能为空");
    39. }
    40. // TODO 用户在页面删除购物车中的商品数据,如果此时用户已经登录,则x需要同步删除后端购物车中的商品
    41. return IMOOCJSONResult.ok();
    42. }
    43. }

    启动项目,访问 http://localhost:8080/foodie-shop/index.html
    登录账号“imooc”,密码“123123”,随便添加几样商品到购物车,进入购物车页面,点击删除,商品删除成功
    image.png
    image.png
    同时选中两样商品,删除其中一项观察结果,发现结算金额经过重新计算
    image.png
    image.png