Beispiel #1
0
        public static bool UpdateStoreCount(Model.ShoppingCart entity)
        {
            var sql        = string.Empty;
            var parameters = new List <SqlParameter>();

            if (entity.Type == 1)
            {
                sql = @"UPDATE ShoppingCart SET StoreCount=(StoreCount+@StoreCount) WHERE HuoDongID=@HuoDongID AND ConsumerID=@ConsumerID";
                parameters.Add(new SqlParameter()
                {
                    ParameterName = "@HuoDongID", Value = entity.HuoDongID
                });
            }
            else if (entity.Type == 2)
            {
                sql = @"UPDATE ShoppingCart SET StoreCount=@StoreCount WHERE ID=@ID AND ConsumerID=@ConsumerID";
                parameters.Add(new SqlParameter()
                {
                    ParameterName = "@ID", Value = entity.ID
                });
            }
            parameters.Add(new SqlParameter()
            {
                ParameterName = "@StoreCount", Value = entity.StoreCount
            });
            parameters.Add(new SqlParameter()
            {
                ParameterName = "@ConsumerID", Value = entity.ConsumerID
            });
            int i = SqlHelper.ExecuteNonQuery(sql, parameters.ToArray());

            return(i > 0 ? true : false);
        }
Beispiel #2
0
        public static bool Add(Model.ShoppingCart entity)
        {
            var sql        = @"
                        INSERT INTO [ShoppingCart]
                               (
                                [ConsumerID]
                                ,[HuoDongID]
                                ,[StoreCount]
                               )
                         VALUES
                               (
                                @ConsumerID
                                ,@HuoDongID
                                ,@StoreCount
                               )
                    ";
            var parameters = new List <SqlParameter>();

            parameters.Add(new SqlParameter()
            {
                ParameterName = "@ConsumerID", Value = entity.ConsumerID
            });
            parameters.Add(new SqlParameter()
            {
                ParameterName = "@HuoDongID", Value = entity.HuoDongID
            });
            parameters.Add(new SqlParameter()
            {
                ParameterName = "@StoreCount", Value = entity.StoreCount
            });
            int i = SqlHelper.ExecuteNonQuery(sql, parameters.ToArray());

            return(i > 0 ? true : false);
        }
Beispiel #3
0
        public async Task <CommandResponse> ExecuteAsync(AddToCartCommand command, CommandResponse previousResult)
        {
            Model.ShoppingCart cart = await _repository.GetActualOrDefaultAsync(command.AuthenticatedUserId);

            StoreProduct product = (await _dispatcher.DispatchAsync(new GetStoreProductQuery {
                ProductId = command.ProductId
            })).Result;

            if (product == null)
            {
                _logger.LogWarning("Product {0} can not be added to cart for user {1} as it does not exist", command.ProductId, command.AuthenticatedUserId);
                return(CommandResponse.WithError($"Product {command.ProductId} does not exist"));
            }
            List <ShoppingCartItem> cartItems = new List <ShoppingCartItem>(cart.Items);

            cartItems.Add(new ShoppingCartItem
            {
                Product  = product,
                Quantity = command.Quantity
            });
            cart.Items = cartItems;
            await _repository.UpdateAsync(cart);

            _logger.LogInformation("Updated basket for user {0}", command.AuthenticatedUserId);
            return(CommandResponse.Ok());
        }
Beispiel #4
0
        public async Task AddToBasketAsync(Guid userId, Guid productId, int quantity)
        {
            _logger.LogInformation("Updating basket for user {0}", userId);
            try
            {
                Model.ShoppingCart cart = await GetBasket(userId);

                StoreProduct product = await _productService.GetAsync(productId);

                if (product == null)
                {
                    _logger.LogWarning("Product {0} can not be added to cart for user {1} as it does not exist", productId, userId);
                    throw new ShoppingCartException($"Product {productId} does not exist");
                }
                List <ShoppingCartItem> cartItems = new List <ShoppingCartItem>(cart.Items);
                cartItems.Add(new ShoppingCartItem
                {
                    Product  = product,
                    Quantity = quantity
                });
                cart.Items = cartItems;
                await _repository.UpdateAsync(cart);

                _logger.LogInformation("Updated basket for user {0}", userId);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Unable to add product {0} to basket for user {1}", productId, userId);
                throw new ShoppingCartException("Unable to add to basket");
            }
        }
 public Task <Model.ShoppingCart> GetActualOrDefaultAsync(Guid userId)
 {
     if (!_baskets.TryGetValue(userId, out Model.ShoppingCart cart))
     {
         cart = new Model.ShoppingCart
         {
             UserId = userId
         };
     }
     return(Task.FromResult(cart));
 }
Beispiel #6
0
        private async Task <Model.ShoppingCart> GetBasket(Guid userId)
        {
            Model.ShoppingCart cart = await _repository.GetAsync(userId);

            if (cart == null)
            {
                cart = new Model.ShoppingCart
                {
                    UserId = userId
                };
            }
            return(cart);
        }
Beispiel #7
0
        public async Task <Model.ShoppingCart> GetAsync(Guid userId)
        {
            _logger.LogInformation("Getting basket for user {0}", userId);
            try
            {
                Model.ShoppingCart cart = await GetBasket(userId);

                _logger.LogInformation("Retrieved cart for user {0} with {1} items", userId, cart.Items.Count);
                return(cart);
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Unable to get basket for user {0}", userId);
                return(null);
            }
        }
Beispiel #8
0
        public static bool DeleteByHuoDongID(Model.ShoppingCart entity)
        {
            var sql        = @"DELETE FROM ShoppingCart WHERE HuoDongID=@HuoDongID AND ConsumerID=@ConsumerID";
            var parameters = new List <SqlParameter>();

            parameters.Add(new SqlParameter()
            {
                ParameterName = "@HuoDongID", Value = entity.HuoDongID
            });
            parameters.Add(new SqlParameter()
            {
                ParameterName = "@ConsumerID", Value = entity.ConsumerID
            });
            int i = SqlHelper.ExecuteNonQuery(sql, parameters.ToArray());

            return(i > 0 ? true : false);
        }
Beispiel #9
0
        // GET: /<controller>/
        public ViewResult Index()
        {
            empty.Model.ShoppingCart cart = new Model.ShoppingCart {
                Products = empty.Model.Product.GetProducts()
            };
            empty.Model.Product[] productArray =
            {
                new Model.Product {
                    Name = "Kayak", Price = 23134
                },
                new Model.Product {
                    Name = "LifeJacket", Price = 2124
                },
            };
            decimal cartTotal  = cart.TotalPrices();
            decimal arrayTotal = productArray.TotalPrices();

            return(View("Index", new String[] {
                $"cart total:{cartTotal:C2}",
                $"Array Total:{cartTotal:C2}"
            }));
        }
 public Task UpdateAsync(Model.ShoppingCart cart)
 {
     _baskets[cart.UserId] = cart;
     return(Task.FromResult(0));
 }
Beispiel #11
0
 public static bool Add(Model.ShoppingCart entity)
 {
     return(DAL.ShoppingCartDal.Add(entity));
 }
Beispiel #12
0
 public static bool UpdateStoreCount(Model.ShoppingCart entity)
 {
     return(DAL.ShoppingCartDal.UpdateStoreCount(entity));
 }
Beispiel #13
0
 public static bool DeleteByHuoDongID(Model.ShoppingCart entity)
 {
     return(DAL.ShoppingCartDal.DeleteByHuoDongID(entity));
 }
Beispiel #14
0
 public static bool Delete(Model.ShoppingCart entity)
 {
     return(DAL.ShoppingCartDal.Delete(entity));
 }