add set meal or dish into shopping cart
This commit is contained in:
@@ -4,6 +4,7 @@ package com.sky.controller.user;
|
|||||||
import com.sky.entity.Setmeal;
|
import com.sky.entity.Setmeal;
|
||||||
import com.sky.result.Result;
|
import com.sky.result.Result;
|
||||||
import com.sky.service.SetmealService;
|
import com.sky.service.SetmealService;
|
||||||
|
import com.sky.service.ShoppingCartService;
|
||||||
import com.sky.vo.DishItemVO;
|
import com.sky.vo.DishItemVO;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/user/shoppingCart")
|
||||||
|
@Slf4j
|
||||||
|
@Api(tags = "shoppingCart-controller")
|
||||||
|
public class ShoppingCartController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ShoppingCartService shoppingCartService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an item into the shopping cart.
|
||||||
|
*
|
||||||
|
* @param shoppingCartDTO The data transfer object encapsulating the details of the item to be added.
|
||||||
|
* Includes 'dishId' for individual dishes, 'setmealId' for set meals, and 'dishFlavor' to specify dish flavors if applicable.
|
||||||
|
* @return A {@link Result} indicating the outcome of the operation, with a success code and no specific data content.
|
||||||
|
*/
|
||||||
|
@PostMapping("/add")
|
||||||
|
@ApiOperation("add into shopping cart")
|
||||||
|
public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO){
|
||||||
|
log.info("add into shopping cart, details: {}",shoppingCartDTO);
|
||||||
|
shoppingCartService.addIntoShoppingCart(shoppingCartDTO);
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package com.sky.mapper;
|
||||||
|
|
||||||
|
import com.sky.entity.ShoppingCart;
|
||||||
|
import org.apache.ibatis.annotations.Insert;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Update;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface ShoppingCartMapper {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a list of shopping carts based on a given ShoppingCart instance.
|
||||||
|
* This method is typically used to fetch carts matching specific criteria
|
||||||
|
* encapsulated within the provided ShoppingCart object.
|
||||||
|
*
|
||||||
|
* @param shoppingCart An instance of ShoppingCart containing criteria
|
||||||
|
* for the search. The fields within this object may be
|
||||||
|
* used to filter or customize the retrieval of shopping carts.
|
||||||
|
* @return A list of ShoppingCart instances that match the criteria specified
|
||||||
|
* in the input ShoppingCart object. If no carts match, an empty list is returned.
|
||||||
|
*/
|
||||||
|
List<ShoppingCart> list(ShoppingCart shoppingCart);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* update the item number by item's id
|
||||||
|
* @param shoppingCart
|
||||||
|
*/
|
||||||
|
@Update("update sky_take_out.shopping_cart set number = #{number} where id = #{id}")
|
||||||
|
void updateNumberById(ShoppingCart shoppingCart);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* insert a new item into the shopping cart
|
||||||
|
* @param shoppingCart
|
||||||
|
*/
|
||||||
|
@Insert("insert into sky_take_out.shopping_cart(name, image, user_id, dish_id, setmeal_id, dish_flavor, amount, create_time) " +
|
||||||
|
"values (#{name},#{image},#{userId},#{dishId},#{setmealId},#{dishFlavor},#{amount},#{createTime})")
|
||||||
|
void insert(ShoppingCart shoppingCart);
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.sky.service;
|
||||||
|
|
||||||
|
import com.sky.dto.ShoppingCartDTO;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public interface ShoppingCartService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a setmeal or dish to the shopping cart.
|
||||||
|
*
|
||||||
|
* @param shoppingCartDTO The data transfer object containing details of the item to be added to the shopping cart.
|
||||||
|
* It includes dishId, setmealId, and dishFlavor necessary for identifying and customizing the item.
|
||||||
|
*/
|
||||||
|
void addIntoShoppingCart(ShoppingCartDTO shoppingCartDTO);
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class ShoppingCartServiceImpl implements ShoppingCartService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ShoppingCartMapper shoppingCartMapper;
|
||||||
|
@Autowired
|
||||||
|
private DishMapper dishMapper;
|
||||||
|
@Autowired
|
||||||
|
private SetmealMapper setmealMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an item to the shopping cart.
|
||||||
|
*
|
||||||
|
* @param shoppingCartDTO The data transfer object containing details of the item to be added.
|
||||||
|
* It includes the dish ID, setmeal ID, and dish flavor if applicable.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void addIntoShoppingCart(ShoppingCartDTO shoppingCartDTO) {
|
||||||
|
//check the item if exists in the shopping cart
|
||||||
|
//fix properties
|
||||||
|
ShoppingCart shoppingCart = new ShoppingCart();
|
||||||
|
BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
|
||||||
|
shoppingCart.setUserId(BaseContext.getCurrentId());
|
||||||
|
|
||||||
|
List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
|
||||||
|
|
||||||
|
if (list != null && !list.isEmpty()){
|
||||||
|
//if exists
|
||||||
|
ShoppingCart cart = list.get(0);
|
||||||
|
cart.setNumber(cart.getNumber() +1);
|
||||||
|
shoppingCartMapper.updateNumberById(cart);
|
||||||
|
}else {
|
||||||
|
//if not,fix the rest properties: name, amount, image
|
||||||
|
//check the item to be added is of dish or set meal
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
24
sky-server/src/main/resources/mapper/ShoppingCartMapper.xml
Normal file
24
sky-server/src/main/resources/mapper/ShoppingCartMapper.xml
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
<?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 sky_take_out.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>
|
||||||
Reference in New Issue
Block a user