Exemplo n.º 1
0
        /// <summary>
        /// calculate two airport measure. Default return type value is mile. ReturnValueType property is optional.
        /// you can set as km or mile or send null.
        /// </summary>
        /// <param name="rqModel"></param>
        /// <returns></returns>
        public async Task <Result> CalculateDistanceAsync(CalculateRequestModel rqModel)
        {
            try
            {
                CalculatedResultModel calcResultModel = new CalculatedResultModel();
                //validate airportcodes
                if (string.IsNullOrWhiteSpace(rqModel.FirstAirportCode) || string.IsNullOrWhiteSpace(rqModel.SecondAirportCode))
                {
                    return(new FailureResult("First and second airportcodes are mandatory fields."));
                }

                var firstAirport = await GetAirportInfoAsync(rqModel.FirstAirportCode);

                var secondAirport = await GetAirportInfoAsync(rqModel.SecondAirportCode);

                calcResultModel.Distance          = GetDistance(firstAirport, secondAirport, rqModel.ReturnValueType?.ToLower());
                calcResultModel.FirstAirportName  = firstAirport.Name;
                calcResultModel.SecondAirportName = secondAirport.Name;
                calcResultModel.DistanceType      = rqModel.ReturnValueType?.ToLower() == "km" ? "km" : "mile";

                return(new SuccessResult(calcResultModel, "Success"));
            }
            catch (Exception ex)
            {
                return(new FailureResult(ex.Message));
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> RegisterAsync([FromBody] CalculateRequestModel rqModel)
        {
            Result result = await _distanceService.CalculateDistanceAsync(rqModel);

            if (!result.Success)
            {
                return(BadRequest(new { message = result.Message }));
            }
            return(Ok(result));
        }