Skip to content

Commit

Permalink
feature: 购物车服务添加商品流程新增最大数量限制 (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
magestacks committed Apr 20, 2023
1 parent eec0761 commit f69669c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,21 @@
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.opengoofy.congomall.biz.cart.domain.aggregate.CartItem;
import org.opengoofy.congomall.biz.cart.domain.common.SelectFlagEnum;
import org.opengoofy.congomall.biz.cart.domain.repository.CartItemRepository;
import org.opengoofy.congomall.biz.cart.infrastructure.dao.mapper.CartItemMapper;
import org.opengoofy.congomall.biz.cart.infrastructure.dao.entity.CartItemDO;
import org.opengoofy.congomall.biz.cart.infrastructure.dao.mapper.CartItemMapper;
import org.opengoofy.congomall.mybatisplus.springboot.starter.PageUtil;
import org.opengoofy.congomall.springboot.starter.common.toolkit.BeanUtil;
import org.opengoofy.congomall.springboot.starter.convention.exception.ServiceException;
import org.opengoofy.congomall.springboot.starter.convention.page.PageRequest;
import org.opengoofy.congomall.springboot.starter.convention.page.PageResponse;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import java.util.List;
Expand All @@ -52,14 +53,17 @@
* @公众号 马丁玩编程,关注回复:资料,领取后端技术专家成长手册
*/
@Repository
@AllArgsConstructor
@RequiredArgsConstructor
public class CartItemRepositoryImpl implements CartItemRepository {

private final CartItemMapper cartItemMapper;
private final RedissonClient redissonClient;

private static final String ADD_CART_LOCK_PREFIX = "add-cart-lock_";

@Value("${congomall.cart.max-product:500}")
public Integer maxProductNum;

@Override
public PageResponse<CartItem> pageQueryCartItem(String userId, PageRequest pageRequest) {
LambdaQueryWrapper<CartItemDO> queryWrapper = Wrappers.lambdaQuery(CartItemDO.class)
Expand Down Expand Up @@ -93,6 +97,10 @@ public void addCartItem(CartItem cartItem) {
if (!tryLock) {
throw new ServiceException("添加购物车失败");
}
int countUserCartItem = countUserCartItem(String.valueOf(cartItem.getCustomerUserId()));
if (countUserCartItem >= maxProductNum) {
throw new ServiceException(String.format("购物车最多添加%d件商品", maxProductNum));
}
try {
LambdaQueryWrapper<CartItemDO> queryWrapper = Wrappers.lambdaQuery(CartItemDO.class)
.eq(CartItemDO::getProductId, cartItem.getProductId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ congomall:
description: Cart Service
title: Cart Service
version: 1.0.0
cart:
max-product: 500

mybatis-plus:
global-config:
Expand Down

0 comments on commit f69669c

Please sign in to comment.