示例#1
0
        private static CartCustomerDTO MapCartCustomerProtoResponseToDTO(CartCustomerResponse cartResponse)
        {
            var cartCustomerDTO = new CartCustomerDTO
            {
                TotalValue   = (decimal)cartResponse.Totalvalue,
                Discount     = (decimal)cartResponse.Discount,
                VoucherUsage = cartResponse.Voucherusage
            };

            if (cartResponse.Voucher != null)
            {
                cartCustomerDTO.Voucher = new VoucherDTO
                {
                    Code = cartResponse.Voucher.Code,
                    DiscountPercentage = (decimal?)cartResponse.Voucher.Discountpercentage,
                    DiscountValue      = (decimal?)cartResponse.Voucher.Discountvalue,
                    DiscountType       = cartResponse.Voucher.Discounttype
                };
            }

            foreach (var item in cartResponse.Items)
            {
                cartCustomerDTO.Items.Add(new ItemCartDTO
                {
                    Name      = item.Name,
                    Image     = item.Image,
                    ProductId = Guid.Parse(item.Productid),
                    Quantity  = item.Quantity,
                    Value     = (decimal)item.Value
                });
            }

            return(cartCustomerDTO);
        }
        private void PopulateDataOrder(CartCustomerDTO cartCustomer, AddressDTO address, OrderDTO order)
        {
            order.VoucherCode  = cartCustomer.Voucher?.Code;
            order.VoucherUsage = cartCustomer.VoucherUsage;
            order.TotalValue   = cartCustomer.TotalValue;
            order.Discount     = cartCustomer.Discount;
            order.OrderItems   = cartCustomer.Items;

            order.Address = address;
        }
        private async Task <bool> ValidateCartItems(CartCustomerDTO cart, IEnumerable <ProductCatalogDTO> products)
        {
            if (cart.Items.Count != products.Count())
            {
                var unavailableItems = cart.Items.Select(c => c.ProductId).Except(products.Select(p => p.Id)).ToList();

                foreach (var itemId in unavailableItems)
                {
                    var itemCart = cart.Items.FirstOrDefault(c => c.ProductId == itemId);
                    AddErrorInProcess($"The item {itemCart.Name} is no longer available in the catalog, please remove it from the cart to proceed with the purchase");
                }

                return(false);
            }

            foreach (var itemCart in cart.Items)
            {
                var productCatalog = products.FirstOrDefault(p => p.Id == itemCart.ProductId);

                if (productCatalog.Value != itemCart.Value)
                {
                    var msgErro = $"The product {itemCart.Name} has changed its value (from:" +
                                  $"{string.Format (CultureInfo.GetCultureInfo ("en-US"), "{0:C}", itemCart.Value)} to: " +
                                  $"{string.Format (CultureInfo.GetCultureInfo ("en-US"), "{0:C}", productCatalog.Value)}) since it was added to the cart.";

                    AddErrorInProcess(msgErro);

                    var responseRemover = await _cartService.RemoveProductCart(itemCart.ProductId);

                    if (ResponseHasErrors(responseRemover))
                    {
                        AddErrorInProcess($"It was not possible to automatically remove the product {itemCart.Name} from your cart, _" + " remove and add again if you still want to buy this item");
                        return(false);
                    }

                    itemCart.Value = productCatalog.Value;
                    var responseAdd = await _cartService.AddProductCart(itemCart);

                    if (ResponseHasErrors(responseAdd))
                    {
                        AddErrorInProcess($"It was not possible to automatically update the product {itemCart.Name} in your cart, _" + "add again if you still want to buy this item");
                        return(false);
                    }

                    ClearErrorsProcess();
                    AddErrorInProcess(msgErro + " We updated the value in your cart, check the order and if you prefer, remove the product");
                    return(false);
                }
            }

            return(true);
        }