protected async Task PlaceOrder() { ValidateInfo(); if (Messages.Any()) { StateHasChanged(); return; } var pseudoCart = JsonConvert.SerializeObject(ECart.ToPseudo()); var resultObj = await MegaService.Fetcher.Fetch("/api/Cart/CheckOut/", new { PseudoCart = pseudoCart, Name, Address, Phone, AlternateName, AlternateAddress, AlternatePhone, SendToDifferentAddress, PaymentMethod = (int)PaymentMethod, Note }); var checkoutResult = resultObj["Result"].ToObject <CheckOutResult>(); if (checkoutResult.Succeeded) { var orderId = checkoutResult.OrderId; MegaService.UriHelper.NavigateTo($"/order/{orderId}"); MegaService.LocalStorage["CART"] = null; } else { Messages = checkoutResult.Messages; StateHasChanged(); } }
private async Task <ECart> FromPseudo(PseudoCart pseudoCart) { var cart = new ECart(); foreach (var item in pseudoCart.Cart) { var product = await _iProductRepository.FindProductByIdForCart(item.Id); if (product != null && product.Status == (int)Consts.Status.Public) { var cartItem = new ECartItem { Product = product, Quantity = item.Quantity, Guid = item.Guid }; item.Choices.ForEach(choice => { var extra = cartItem.Product.Extras.FirstOrDefault(ext => ext.Id == choice.Extra); var option = extra?.Options.FirstOrDefault(opt => opt.Id == choice.Option.GetValueOrDefault(-1)); if (extra != null && option != null) { cartItem.Choices.Add(extra, option); } }); cart.Contents.Add(cartItem); } } cart.Coupons = cart.Contents.SelectMany(item => item.Product.Coupons) .Where(c => pseudoCart.Coupons.Contains(c.Id)).Distinct().ToList(); return(cart); }
private async Task <CheckOutResult> ValidateCart(PseudoCart cart) { ICollection <string> messages = new List <string>(); ECart realCart = new ECart(); foreach (var item in cart.Cart.ToList()) { bool valid = true; var upToDateItem = await _productRepository.FindProductByIdForCartNoFreshen(item.Id); ExtraDictionary choices = new ExtraDictionary(); if (upToDateItem.Status != (int)Consts.Status.Public) { messages.Add($"{upToDateItem.Name} is no longer on sale."); valid = false; } else if (upToDateItem.Quantity < item.Quantity) { messages.Add($"{upToDateItem.Name} does not have enough item on sale."); valid = false; } else { foreach (var choice in item.Choices) { bool validChoice = true; var upToDateExtra = upToDateItem.Extras.FirstOrDefault(ext => ext.Id == choice.Extra && !ext.Deleted); Option upToDateOption = null; if (upToDateExtra == null) { messages.Add($"An extra feature of product {upToDateItem.Name} does not exist."); validChoice = false; } else { if (!upToDateExtra.Optional && choice.Option == null) { messages.Add($"All mandatory options were not fulfilled with product {upToDateItem.Name}."); validChoice = false; } else { upToDateOption = upToDateExtra.Options.FirstOrDefault(opt => opt.Id == choice.Option && !opt.Deleted); if (upToDateOption == null) { messages.Add($"An option of product {upToDateItem.Name} does not exist."); validChoice = false; } } } if (validChoice) { choices.Add(upToDateExtra, upToDateOption); } } } if (valid) { realCart.Contents.Add(new ECartItem { Product = upToDateItem, Quantity = item.Quantity, Choices = choices }); } } foreach (var coupon in cart.Coupons) { var realCoupon = realCart.AvailableCoupons.FirstOrDefault(c => c.Id == coupon && !c.Deleted && c.IsValid); if (realCoupon != null) { realCart.Coupons.Add(realCoupon); } else { messages.Add($"Coupon id ${coupon} is no longer valid and has been removed."); } } return(new CheckOutResult { Messages = messages, ECart = realCart, Succeeded = realCart.Contents.Any() }); }