Exemplo n.º 1
0
 public async Task PlaceOrder(CustomerModel customerModel, string supplierName, ProductModel[] productModels)
 {
     await _orderService.PlaceOrderAsync(
         Mapper.Map <CustomerDto>(customerModel),
         supplierName,
         Mapper.Map <ProductDto[]>(productModels));
 }
Exemplo n.º 2
0
        private async void submitOrderBtn_Click(object sender, EventArgs e)
        {
            var selectedProductBoxesAndQuantitiesHash = _productsQuantityHash
                                                        .Where(x => x.Key.SelectedValue != null)
                                                        .ToDictionary(x => x.Key, x => x.Value);

            if (!selectedProductBoxesAndQuantitiesHash.Any())
            {
                MessageForm.ShowWarning("No products selected.");
                return;
            }

            var selectedSupplierDto = suppliersComboBox.SelectedValue as SupplierDto;

            var products = new Dictionary <int, ProductDto>();

            foreach (var item in selectedProductBoxesAndQuantitiesHash)
            {
                int quantity;
                var parsed = int.TryParse(item.Value.Text, out quantity);
                if (!parsed || quantity < 1)
                {
                    MessageForm.ShowError("Quantity must be a whole positive number.");
                    return;
                }

                var currentProduct = item.Key.SelectedValue as ProductDto;
                if (!products.ContainsKey(currentProduct.Id))
                {
                    currentProduct.Quantity = quantity;
                    products.Add(currentProduct.Id, currentProduct);
                }
                else
                {
                    products[currentProduct.Id].Quantity += quantity;
                }
            }

            var totalPrice   = products.Sum(x => x.Value.Price * x.Value.Quantity);
            var confirmation = PromptMessage.ConfirmationMessage($"Are you sure you want to order? Your total price is: ${totalPrice}");

            if (!confirmation)
            {
                return;
            }

            await _orderService.PlaceOrderAsync(Constants.Customer, selectedSupplierDto.Name, products.Values.ToArray());

            MessageForm.ShowSuccess("Order request sent.");
            ClearProductsQuantityAndPrice();
        }