Ejemplo n.º 1
0
        public async Task <IActionResult> AddToCart(Model.Input.CartAdd model)
        {
            var locationRepo = (Repository.ILocation) this._services.GetService(typeof(Repository.ILocation));
            var userRepo     = (Repository.IUser) this._services.GetService(typeof(Repository.IUser));
            var orderRepo    = (Repository.IOrder) this._services.GetService(typeof(Repository.IOrder));
            var productRepo  = (Repository.IProduct) this._services.GetService(typeof(Repository.IProduct));

            var userId = Guid.Parse(HttpContext.User.FindFirst(claim => claim.Type == Auth.Claim.UserId).Value);

            var user = await userRepo.GetUserById(userId);

            var location = await userRepo.GetDefaultLocation(user);

            var product = await productRepo.GetProductById(model.ItemId);

            var currentOrder = await userRepo.GetOpenOrder(user, location);

            var addStatus = await orderRepo.AddLineItem(userId, currentOrder, product, model.ItemQuantity);

            switch (addStatus)
            {
            case Repository.AddLineItemResult.Ok:
            {
                var okModel = new Model.View.CartAddOk();
                okModel.ItemId       = model.ItemId;
                okModel.ItemQuantity = model.ItemQuantity;

                return(RedirectToAction("CartAddOk", "Cart", okModel));
            }

            case Repository.AddLineItemResult.ExceedsStock:
            {
                this._logger.LogWarning($"An attempt was made to update the cart with values exceeding store stock. User id: '{userId}'.");
                this.SetFlashError("Unable to add the item to your order: The amount requested exceeds the amount available in stock.");
                return(Redirect($"/ItemDetail/View/{model.ItemId}"));
            }

            case Repository.AddLineItemResult.OrderMissing:
            {
                this._logger.LogWarning($"An attempt was made to update the cart when the order is missing. User id: '{userId}'.");
                this.SetFlashError("There was a problem adding this item to your cart. Please try again.");
                return(Redirect($"/ItemDetail/View/{model.ItemId}"));
            }

            case Repository.AddLineItemResult.ProductMissing:
            {
                this._logger.LogWarning($"An attempt was made to update the cart with a non-existing item. User id: '{userId}'.");
                this.SetFlashError("There was a problem adding this item to your cart. Please try again.");
                return(Redirect($"/ItemDetail/View/{model.ItemId}"));
            }

            default:
            {
                this.SetFlashError("There was a problem adding this item to your cart. Please try again.");
                return(Redirect($"/ItemDetail/View/{model.ItemId}"));
            }
            }
        }
Ejemplo n.º 2
0
 public IActionResult CartAddOk(Model.View.CartAddOk model)
 {
     return(View("CartAddOk", model));
 }