Пример #1
0
        public IHttpActionResult RemoveItemFromCart(RemoveItemFromCartRequest request)
        {
            if (request == null)
            {
                return(this.Ok(false));
            }

            return(this.Ok(this.cartService.DeleteItemFromCart(request.UserId, request.ProductId)));
        }
Пример #2
0
        public ActionResult <RemoveItemFromCartResponse> RemoveItemFromCart(long cartId, long cartItemId)
        {
            var removeItemFromCartRequest = new RemoveItemFromCartRequest {
                CartId = cartId, CartItemId = cartItemId
            };
            var removeItemFromCartResponse = _cartService.RemoveItemFromCart(removeItemFromCartRequest);

            return(removeItemFromCartResponse);
        }
Пример #3
0
        public RemoveItemFromCartResponse RemoveItemFromCart(RemoveItemFromCartRequest removeItemFromCartRequest)
        {
            RemoveItemFromCartResponse response = new RemoveItemFromCartResponse();
            var cartItem = _cartItemRepository.FindCartItemById(removeItemFromCartRequest.CartItmeId);

            _cartItemRepository.DeleteCartItem(cartItem);

            response.CartItemId = cartItem.Id;
            return(response);
        }
Пример #4
0
        public async Task RemoveFromCart(RemoveItemFromCartRequest request, ClaimsPrincipal user)
        {
            var userId = _userService.GetUserId(user, request.Email);
            var cart   = GetUserCartItem(userId, request.ItemId);

            await using var transaction = await _context.Database.BeginTransactionAsync();

            try
            {
                if (cart != null)
                {
                    var item = _context.Items.FirstOrDefault(x => x.ItemId == request.ItemId);
                    if (item == null)
                    {
                        throw new Exception("Lack of item");
                    }
                    item.Quantity += request.Quantity;
                    _context.Items.Update(item);
                    await _context.SaveChangesAsync().ConfigureAwait(false);

                    cart.Quantity -= request.Quantity;
                    if (cart.Quantity <= 0)
                    {
                        _context.Carts.Remove(cart);
                    }
                    else
                    {
                        _context.Carts.Update(cart);
                    }
                    await _context.SaveChangesAsync();
                }

                await transaction.CommitAsync();
            }
            catch (Exception e)
            {
                await transaction.RollbackAsync();

                throw;
            }
        }
Пример #5
0
        public async Task <IActionResult> Remove([FromBody] RemoveItemFromCartRequest request)
        {
            try
            {
                var validator = new CartValidator <RemoveItemFromCartRequest>();
                var result    = await validator.ValidateAsync(request);

                if (!result.IsValid)
                {
                    throw new ValidationException("Invalid data");
                }

                await _cartService.RemoveFromCart(request, User);

                return(Ok());
            }
            catch (Exception)
            {
                return(BadRequest());
            }
        }
 public void Delete([FromBody] RemoveItemFromCartRequest request)
 {
 }