当前位置: 首页 > news >正文

苍穹外卖学习笔记(十六)

文章目录

  • 二. 购物车功能
    • 添加购物车
      • ShoppingCartController
      • ShoppingCartService
      • ShoppingCartServiceImpl
      • ShoppingCartMapper
      • ShoppingCartMapper
      • application
    • 查看购物车
      • ShoppingCartController
      • ShoppingCartService
      • ShoppingCartServiceImpl
    • 清空购物车
      • ShoppingCartController
      • ShoppingCartService
      • ShoppingCartServiceImpl
    • 删除购物车中一个商品
      • ShoppingCartController
      • ShoppingCartService
      • ShoppingCartServiceImpl

二. 购物车功能

添加购物车

ShoppingCartController

package com.sky.controller.user;import com.sky.dto.ShoppingCartDTO;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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;/*** @author Jie.* @description: TODO* @date 2024/10/7* @version: 1.0*/
@RestController
@RequestMapping("/user/shoppingCart")
@Api(tags = "购物车")
@Slf4j
public class ShoppingCartController {@Autowiredprivate ShoppingCartService shoppingCartService;/*** 添加购物车*/@PostMapping("add")@ApiOperation(value = "添加购物车")public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO) {log.info("添加购物车:{}", shoppingCartDTO);shoppingCartService.addShoppingCart(shoppingCartDTO);return Result.success();}
}

ShoppingCartService

package com.sky.service;import com.sky.dto.ShoppingCartDTO;/*** @author Jie.* @description: TODO* @date 2024/10/7* @version: 1.0*/
public interface ShoppingCartService {/*** 添加购物车*/void addShoppingCart(ShoppingCartDTO shoppingCartDTO);
}

ShoppingCartServiceImpl

package com.sky.service.impl;import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;
import java.util.List;/*** @author Jie.* @description: TODO* @date 2024/10/7* @version: 1.0*/
@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {@Autowiredprivate ShoppingCartMapper shoppingCartMapper;@Autowiredprivate DishMapper dishMapper;@Autowiredprivate SetmealMapper setmealMapper;/*** 添加购物车*/@Overridepublic void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {//判断加入购物车的商品是否已经存在ShoppingCart shoppingCart = new ShoppingCart();BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);Long currentId = BaseContext.getCurrentId();shoppingCart.setUserId(currentId);//查询购物车列表List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);//如果存在,修改商品数量if (list != null && !list.isEmpty()) {ShoppingCart cart = list.get(0);cart.setNumber(cart.getNumber() + 1);//数量加1shoppingCartMapper.updateById(cart);}//如果不存在,添加商品else {//判断是菜品还是套餐Long dishId = shoppingCartDTO.getDishId();if (dishId != null) {//菜品Dish dish = dishMapper.selectById(dishId);shoppingCart.setName(dish.getName());shoppingCart.setImage(dish.getImage());shoppingCart.setAmount(dish.getPrice());} else {//套餐Long setmealId = shoppingCartDTO.getSetmealId();Setmeal setmeal = setmealMapper.selectById(setmealId);shoppingCart.setName(setmeal.getName());shoppingCart.setImage(setmeal.getImage());shoppingCart.setAmount(setmeal.getPrice());}shoppingCart.setNumber(1);shoppingCart.setCreateTime(LocalDateTime.now());shoppingCartMapper.insert(shoppingCart);}}
}

ShoppingCartMapper

package com.sky.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sky.entity.ShoppingCart;
import org.apache.ibatis.annotations.Mapper;import java.util.List;/*** @author Jie.* @description: TODO* @date 2024/10/7* @version: 1.0*/
@Mapper
public interface ShoppingCartMapper extends BaseMapper<ShoppingCart> {/*** 查询购物车列表*/List<ShoppingCart> list(ShoppingCart shoppingCart);
}

ShoppingCartMapper

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sky.mapper.ShoppingCartMapper"><select id="list" resultType="com.sky.entity.ShoppingCart">SELECT * FROM shopping_cart<where><if test="userId != null">AND user_id = #{userId}</if><if test="setmealId != null">AND setmeal_id = #{setmealId}</if><if test="dishId != null">AND dish_id = #{dishId}</if><if test="dishFlavor != null">AND dish_flavor = #{dishFlavor}</if></where></select>
</mapper>

application

server:port: 8080spring:profiles:active: devmain:allow-circular-references: truedatasource:druid:driver-class-name: ${sky.datasource.driver-class-name}url: jdbc:mysql://${sky.datasource.host}:${sky.datasource.port}/${sky.datasource.database}?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=trueusername: ${sky.datasource.username}password: ${sky.datasource.password}redis:host: ${sky.redis.host}post: ${sky.redis.post}database: ${sky.redis.database}# mybatis-plus配置
mybatis-plus:configuration:# 驼峰命名map-underscore-to-camel-case: true# 日志log-impl: org.apache.ibatis.logging.stdout.StdOutImplglobal-config:db-config:id-type: automapper-locations: classpath:mapper/*.xml# 日志配置
logging:level:com:sky:mapper: debugservice: infocontroller: infosky:jwt:# 设置jwt签名加密时使用的秘钥admin-secret-key: itcast# 设置jwt过期时间admin-ttl: 720000000# 设置前端传递过来的令牌名称admin-token-name: token# 设置jwt签名加密时使用的秘钥user-secret-key: itheima# 设置jwt过期时间user-ttl: 720000000# 设置前端传递过来的令牌名称user-token-name: authenticationalioss:endpoint: ${sky.alioss.endpoint}access-key-id: ${sky.alioss.access-key-id}access-key-secret: ${sky.alioss.access-key-secret}bucket-name: ${sky.alioss.bucket-name}wechat:appid: ${sky.wechat.appid}secret: ${sky.wechat.secret}

查看购物车

ShoppingCartController

 /*** 查看购物车*/@GetMapping("list")@ApiOperation(value = "查看购物车")public Result<List<ShoppingCart>> list() {List<ShoppingCart> list = shoppingCartService.showShoppingCart();return Result.success(list);}

ShoppingCartService

 /*** 查看购物车*/List<ShoppingCart> showShoppingCart();

ShoppingCartServiceImpl

  /*** 查看购物车*/@Overridepublic List<ShoppingCart> showShoppingCart() {Long currentId = BaseContext.getCurrentId();ShoppingCart shoppingCart = ShoppingCart.builder().userId(currentId).build();List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);return list;}

清空购物车

ShoppingCartController

    /*** 清空购物车*/@DeleteMapping("clean")@ApiOperation(value = "清空购物车")public Result clean() {shoppingCartService.cleanShoppingCart();return Result.success();}

ShoppingCartService

 /*** 清空购物车*/void cleanShoppingCart();

ShoppingCartServiceImpl

   /*** 清空购物车*/@Overridepublic void cleanShoppingCart() {Long currentId = BaseContext.getCurrentId();LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();wrapper.eq(ShoppingCart::getUserId, currentId);shoppingCartMapper.delete(wrapper);}

删除购物车中一个商品

ShoppingCartController

    /*** 删除购物车中一个商品*/@PostMapping("/sub")public Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO) {log.info("删除购物车中一个商品:{}", shoppingCartDTO);shoppingCartService.subShoppingCart(shoppingCartDTO);return Result.success();}

ShoppingCartService

 /*** 删除购物车商品*/void subShoppingCart(ShoppingCartDTO shoppingCartDTO);

ShoppingCartServiceImpl

   /*** 删除购物车商品*/@Overridepublic void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {ShoppingCart shoppingCart = new ShoppingCart();BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);Long currentId = BaseContext.getCurrentId();shoppingCart.setUserId(currentId);List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);if (list != null && !list.isEmpty()) {ShoppingCart cart = list.get(0);if (cart.getNumber() > 1) {cart.setNumber(cart.getNumber() - 1);shoppingCartMapper.updateById(cart);} else {shoppingCartMapper.deleteById(cart.getId());}}}

http://www.mrgr.cn/news/44603.html

相关文章:

  • getattr()内置函数
  • 【操作系统考研】2进程管理(1)
  • 看诊新助手,语音转文字技术,让病历记录更贴心
  • c++和c语言的区别
  • OpenHarmony(鸿蒙南向开发)——标准系统方案之瑞芯微RK3568移植案例(上)
  • 国标GBT28181详解:第三方呼叫控制的实时视音频点播流程详解(国标GB/T28181-2022 )
  • MySQL多表查询:标量子查询
  • 强化-微分
  • 面向对象编程【JavaScript】
  • 基于PHP的校园二手书交易管理系统
  • 计算机毕业设计 基于Python的豆果美食推荐系统的设计与实现 Python+Django+Vue 前后端分离 附源码 讲解 文档
  • 每日英语听力 Day13
  • 备战大数据比赛:个人经验与实战技巧分享
  • LeetCode题练习与总结:移动零--283
  • 二维数组的旋转与翻转(C++)(上(这只是简单讲解))
  • 开源项目带来的思考
  • 修改 MySQL 数据库中的唯一键
  • Oracle登录报错-ORA-01017: invalid username/password;logon denied
  • 推荐一款强大的书签管理工具,让你的网址不在落灰
  • 汉诺塔问题