public async Task <IActionResult> AddShoppingCartItem(
            [FromBody] AddShoppingCartItemDto addShoppingCartItemDto)
        {
            var currentUserId = _httpContextAccessor
                                .HttpContext
                                .User
                                .FindFirst(ClaimTypes.NameIdentifier)
                                .Value;

            var shoppingCart = await _touristRouteRepository
                               .GetShoppingCartByUserIdAsync(currentUserId);

            var touristRoute = await _touristRouteRepository
                               .GetTouristRouteAsync(addShoppingCartItemDto.TouristRouteId);

            if (touristRoute == null)
            {
                return(NotFound($"旅游路线{addShoppingCartItemDto.TouristRouteId}不存在"));
            }

            var lineItem = new LineItem()
            {
                TouristRouteId  = addShoppingCartItemDto.TouristRouteId,
                ShoppingCartId  = shoppingCart.Id,
                OriginalPrice   = touristRoute.OriginalPrice,
                DiscountPresent = touristRoute.DiscountPresent
            };

            await _touristRouteRepository.AddShoppingCartItemAsync(lineItem);

            await _touristRouteRepository.SaveAsync();


            return(Ok(_mapper.Map <ShoppingCartDto>(shoppingCart)));
        }
예제 #2
0
        public async Task <IActionResult> AddShoppingCartItem(
            [FromBody] ShoppingCartForCreationDto shoppingCartForCreation
            )
        {
            // 1 获得当前用户
            var userId = httpContextAccessor
                         .HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;

            // 2 使用userid获得购物车
            var shoppingCart = await touristRouteRepository
                               .GetShoppingCartByUserIdAsync(userId);

            // 3 合建lineItem
            var touristRoute = await touristRouteRepository
                               .GetTouristRouteAsync(shoppingCartForCreation.TouristRouteId);

            if (touristRoute == null)
            {
                return(NotFound("旅游路线不存在"));
            }

            var lineItem = new LineItem()
            {
                TouristRouteId  = shoppingCartForCreation.TouristRouteId,
                ShoppingCardId  = shoppingCart.Id,
                OriginalPrice   = touristRoute.OriginalPrice,
                DiscountPresent = touristRoute.DiscountPresent
            };

            // 4 添加LineItem,并保存数据
            await touristRouteRepository.AddShoppingCartItemAsync(lineItem);

            await touristRouteRepository.SaveAsync();

            return(Ok(mapper.Map <ShoppingCartDto>(shoppingCart)));
        }