예제 #1
0
        public async Task <IActionResult> CalculateInsuranceForOrderAsync([FromBody] IEnumerable <OrderEntryDto> input)
        {
            // Map.
            var order        = _mapper.Map <IEnumerable <OrderEntry> >(input);
            var productTypes = await _productTypeRepository.ListAllAsync();

            try
            {
                // Act.
                InsuredOrder insuredOrder = new()
                {
                    Entries = order.Select(entry =>
                    {
                        ProductType productType = productTypes.First(type => type.Id == entry.Product.ProductTypeId);

                        return(new InsuredOrderEntry()
                        {
                            InsuredProduct = new()
                            {
                                ProductId = entry.Product.Id,
                                ProductTypeName = productType.Name,
                                ProductTypeHasInsurance = productType.CanBeInsured,
                                ProductTypeSurcharge = productType.Surcharge,
                                SalesPrice = entry.Product.SalesPrice,
                            },
                            Quantity = entry.Quantity,
                        });
                    })
                };

                // Map.
                var output = _mapper.Map <InsuredOrderDto>(insuredOrder);

                // Return.
                return(Ok(output));
            }