delete item from shopping cart

This commit is contained in:
2024-12-19 14:33:02 +08:00
parent cf18e8bd82
commit 96c6308c52
5 changed files with 69 additions and 0 deletions

View File

@@ -48,6 +48,10 @@ public class ShoppingCartController {
return Result.success(list);
}
/**
* clean shopping cart
* @return
*/
@DeleteMapping("/clean")
@ApiOperation("clean the shopping cart")
public Result clean(){
@@ -55,4 +59,16 @@ public class ShoppingCartController {
shoppingCartService.cleanShoppingCart();
return Result.success();
}
/**
* subtract an item from the shopping cart
* @param shoppingCartDTO
* @return
*/
@PostMapping("/sub")
@ApiOperation("subtract an item from the shopping cart")
public Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO){
shoppingCartService.subtractFromShoppingCart(shoppingCartDTO);
return Result.success();
}
}

View File

@@ -45,4 +45,10 @@ public interface ShoppingCartMapper {
*/
@Delete("delete from sky_take_out.shopping_cart where user_id = #{userId}")
void clean(Long userId);
/**
* delete the item that number is 0 from the shopping cart
* @param shoppingCart
*/
void delete(ShoppingCart shoppingCart);
}

View File

@@ -27,4 +27,10 @@ public interface ShoppingCartService {
* clean shopping cart
*/
void cleanShoppingCart();
/**
* subtract an item from the shopping cart
* @param shoppingCartDTO
*/
void subtractFromShoppingCart(ShoppingCartDTO shoppingCartDTO);
}

View File

@@ -94,4 +94,28 @@ public class ShoppingCartServiceImpl implements ShoppingCartService {
Long userId = BaseContext.getCurrentId();
shoppingCartMapper.clean(userId);
}
/**
* subtract an item from the shopping cart
* @param shoppingCartDTO
*/
@Override
public void subtractFromShoppingCart(ShoppingCartDTO shoppingCartDTO) {
//construct an object that contains the basic properties : userId, set meal/dish id, dish flavor
ShoppingCart shoppingCart = new ShoppingCart();
BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
shoppingCart.setUserId(BaseContext.getCurrentId());
List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
if (!list.isEmpty()) {
shoppingCart = list.get(0);
//check the number of shopping cart
if (shoppingCart.getNumber() > 1) {
shoppingCart.setNumber(shoppingCart.getNumber() - 1);
shoppingCartMapper.updateNumberById(shoppingCart);
} else {
shoppingCartMapper.delete(shoppingCart);
}
}
}
}

View File

@@ -3,6 +3,23 @@
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sky.mapper.ShoppingCartMapper">
<delete id="delete">
delete 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>
</delete>
<select id="list" resultType="com.sky.entity.ShoppingCart">
select * from sky_take_out.shopping_cart