예제 #1
0
        public HttpResponseMessage Get(string country)
        {
            if (string.IsNullOrEmpty(country) || string.IsNullOrWhiteSpace(country))
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, new BaseApiResponse
                {
                    Code = InternalApiStatusCode.FailedRequestValidation,
                    Message = "country is invalid"
                }));
            }


            var countryResposne = new CountryCityResponse();

            try
            {
                var result = _weatherService.GetCities(country);

                if (!result.IsSuccess)
                {
                    return(Request.CreateResponse(HttpStatusCode.InternalServerError, new BaseApiResponse
                    {
                        Code = InternalApiStatusCode.Error,
                        Message = "Failed to fetch cities"
                    }));
                }

                countryResposne.Cities  = result.Cities;
                countryResposne.Code    = InternalApiStatusCode.Success;
                countryResposne.Message = "list of cities retrieved";


                return(Request.CreateResponse(HttpStatusCode.OK, countryResposne));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
            }
        }
예제 #2
0
        public HttpResponseMessage Get([FromUri] WeatherRequest request)
        {
            if (string.IsNullOrEmpty(request.Country) || string.IsNullOrWhiteSpace(request.Country))
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, new BaseApiResponse
                {
                    Code = InternalApiStatusCode.FailedRequestValidation,
                    Message = "country is invalid"
                }));
            }

            if (string.IsNullOrEmpty(request.City) || string.IsNullOrWhiteSpace(request.City))
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, new BaseApiResponse
                {
                    Code = InternalApiStatusCode.FailedRequestValidation,
                    Message = "city is invalid"
                }));
            }

            var countryCityResposne = new CountryCityResponse();
            var countryCityResult   = _weatherService.GetCities(request.Country);

            if (!countryCityResult.IsSuccess)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, new BaseApiResponse
                {
                    Code = InternalApiStatusCode.Error,
                    Message = "Failed to fetch cities"
                }));
            }

            if (!countryCityResult.Cities.Any(c => c.Equals(request.City)))
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, new BaseApiResponse
                {
                    Code = InternalApiStatusCode.Error,
                    Message = "City doesn't below to this country"
                }));
            }


            var weatherResposne = new WeatherResponse();

            try
            {
                var result = _weatherService.GetCityWeather(request.City);

                if (!result.IsSuccess)
                {
                    return(Request.CreateResponse(HttpStatusCode.InternalServerError, new BaseApiResponse
                    {
                        Code = InternalApiStatusCode.Error,
                        Message = "Failed to fetch cities"
                    }));
                }

                weatherResposne.Weather = result.CityWeather;
                weatherResposne.Code    = InternalApiStatusCode.Success;
                weatherResposne.Message = "City weather condition";


                return(Request.CreateResponse(HttpStatusCode.OK, weatherResposne));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
            }
        }