예제 #1
0
        public async Task <BaseDtoResponse <AddToCartDto> > Add(AddToCartRequest request)
        {
            try
            {
                Product product = await _productRepository.GetById(request.ProductId);

                if (product != null)
                {
                    CartItem item = _mapper.Map <AddToCartRequest, CartItem>(request);
                    item.Product = product;
                    CartItem carItem = await _cartItemRepository.Add(item);

                    if (carItem == null)
                    {
                        return(new BaseDtoResponse <AddToCartDto>("Error occured while creating the CartItem, try again"));
                    }
                    AddToCartDto response = _mapper.Map <CartItem, AddToCartDto>(carItem);
                    return(new BaseDtoResponse <AddToCartDto>(response));
                }
                else
                {
                    return(new BaseDtoResponse <AddToCartDto>("Unable to find product in the Catalog, please try again"));
                }
                //IList<CartItem> items = await _cartItemRepository.List(new ItemsInCartForUserSpecification(request.IdentityId));
                //if (items != null)
                //{
                //    CartItem sameProduct = items.Cast<CartItem>().SingleOrDefault(e => e.ProductId == request.ProductId);

                //}
            }
            catch (Exception ex)
            {
                return(new BaseDtoResponse <AddToCartDto>(ex.Message));
            }
        }
예제 #2
0
        public async Task <int> Handle(AddCartItemsCommand request, CancellationToken cancellationToken)
        {
            if (request.Id.HasValue)
            {
                await _cartService.ValidateCartId(request.Id.Value);
            }

            var products = request
                           .Products
                           .Select(x => new CartItemEntry(x.ProductId, x.Quantity))
                           .ToArray();

            return(await _cartItemRepository.Add(request.Id, products));
        }
        public PartialViewResult OrderGrid(Cart cart)
        {
            var model = new CartViewModel
            {
                Customer = null,
                Cart     = null
            };

            // Get existing items.
            var cartList = _cartItemRepository
                           .GetCartItems(cart.CartId);

            // Add the Items NOT in the list.
            var newItems = cart.Items
                           .Where(item => !cartList.Select(p => p.ProductId).Contains(item.ProductId))
                           .ToList();

            cartList.AddRange(newItems);

            // Save the new added items.
            foreach (var item in newItems)
            {
                _cartItemRepository.Add(new CartItem
                {
                    ProductId = item.ProductId,
                    Quantity  = 1,
                    CartId    = 1
                });
            }
            _cartItemRepository.SaveChanges();

            // Use the new cartList to load the products and defaults
            model.CartItems = cartList
                              .Select(e =>
            {
                e.Product  = _productRepository.First(product => product.ProductId == e.ProductId);
                e.Quantity = e.Quantity == 0 ? 1 : e.Quantity;
                return(e);
            })
                              .ToList();

            return(PartialView("_childGrid", model));
        }
예제 #4
0
 public async Task Save(string connectionString, int id, string cartId, string cartCode, int version, CartItem[] cartItems,
                        CartItemDetail shoppingCartItemDetail)
 {
     await _cartItemRepository.Add(connectionString, id, cartId, cartCode, version, cartItems, shoppingCartItemDetail, _cartItemDetailRepository.Add, _cartRepository.Change);
 }
예제 #5
0
 public int Create(CartItem entity)
 {
     return(_cartItemRepository.Add(entity));
 }