Example #1
0
        /// <summary>
        /// Method used for calculating the offer that will be served to the client
        /// </summary>
        /// <param name="offer">The body payload containing the chosen CityId and the chosen services</param>
        /// <returns>The presentation model containing the calculated offer details</returns>
        public async Task <OfferDto> CalculateOfferAsync(NewOfferDto offer)
        {
            if (offer == null)
            {
                throw new ArgumentNullException("IOffer model is null.");
            }

            if (!offer.Alternatives.Any())
            {
                throw new Exception("Services not set.");
            }

            try
            {
                var city = await _citiesService.GetCityByIdAsync(offer.CityId);

                var response = new OfferDto()
                {
                    City       = city.Name,
                    OfferItems = await SetOfferItemsAsync(offer.Alternatives, offer.MainServiceAmount),
                    Currency   = city.Currency
                };

                return(response);
            }
            catch (Exception)
            {
                throw;
            }
        }
Example #2
0
        public async Task <IActionResult> CalculateOffer([FromBody] NewOfferDto offer)
        {
            if (offer == null || offer.Alternatives == null)
            {
                return(BadRequest($"Invalid model."));
            }

            try
            {
                var response = await _offerService.CalculateOfferAsync(offer);

                return(Ok(response));
            }
            catch (Exception e)
            {
                return(StatusCode(500, e));
            }
        }