Ejemplo n.º 1
0
        public async Task <ActionResult <IEnumerable <PlaceProvider> > > GetPrice(
            [FromQuery] string slaLevel,
            [FromQuery] DateTimeOffset slaFrom,
            [FromQuery] DateTimeOffset slaUntil,
            [FromQuery] int registrations,
            [FromQuery] string currency,
            [FromQuery] string country            = "",
            [FromQuery] bool includeVAT           = false,
            [FromQuery] bool includeSLA           = true,
            [FromQuery] bool includeRegistrations = true
            )
        {
            try
            {
                if (!slaLevel.ValidateSLA())
                {
                    throw new Exception("Invalid SLA Level");
                }
                if (!currency.ValidateCurrency())
                {
                    throw new Exception("Invalid currency");
                }
                if (registrations < 0)
                {
                    throw new Exception("Invalid registrations");
                }

                if (includeSLA && includeRegistrations)
                {
                    var price = placeProviderRepository.GetPriceWithoutVAT(slaLevel, registrations, currency, slaFrom, slaUntil);
                    if (includeVAT)
                    {
                        var multiplier = placeProviderRepository.GetVATMultiplier(country);
                        price = decimal.Round(price * multiplier, 2);
                    }
                    return(Ok(price));
                }
                if (includeSLA)
                {
                    var price = placeProviderRepository.GetPriceWithoutVATSLA(slaLevel, currency, slaFrom, slaUntil);
                    if (includeVAT)
                    {
                        var multiplier = placeProviderRepository.GetVATMultiplier(country);
                        price = decimal.Round(price * multiplier, 2);
                    }
                    return(Ok(price));
                }
                if (includeRegistrations)
                {
                    var price = placeProviderRepository.GetPriceWithoutVATRegistrations(registrations, currency);
                    if (includeVAT)
                    {
                        var multiplier = placeProviderRepository.GetVATMultiplier(country);
                        price = decimal.Round(price * multiplier, 2);
                    }
                    return(Ok(price));
                }
                throw new Exception("Unknown input");
            }
            catch (ArgumentException exc)
            {
                logger.LogError(exc.Message);
                return(BadRequest(new ProblemDetails()
                {
                    Detail = exc.Message
                }));
            }
            catch (Exception exc)
            {
                logger.LogError(exc, exc.Message);
                return(BadRequest(new ProblemDetails()
                {
                    Detail = exc.Message
                }));
            }
        }