public async Task <RestaurantSearchResultsDto> GetRestaurantsByGeoLocation(string latitude, string longitude, int page, int pageSize) { var restaurantSearchResultsDto = new RestaurantSearchResultsDto(); try { var cleanLatitude = CleanString(latitude); var cleanLongitude = CleanString(longitude); var cacheKey = $"{cleanLatitude}_{cleanLongitude}"; _cache.TryGetValue(cacheKey, out RestaurantsSearchResults restaurantsSearchResults); if (restaurantsSearchResults == null) { _logger.LogDebug($"Search for GeoLocation {cacheKey} not found in cache, fetching from API"); var response = await _restaurantsApiClient.GetRestaurantsByLatLong(cleanLatitude, cleanLongitude); var hasNoResults = !response.IsSuccess || response.RestaurantsSearchResults?.Restaurants == null || !response.RestaurantsSearchResults.Restaurants.Any(); if (hasNoResults) { restaurantSearchResultsDto.ErrorMessage = response.StatusCode == HttpStatusCode.BadRequest ? "Invalid GeoLocation" : "No resturants found for this geolocation"; _logger.LogDebug($"Search for GeoLocation {cacheKey}, {restaurantSearchResultsDto.ErrorMessage}"); } else { restaurantsSearchResults = response.RestaurantsSearchResults; _cache.Set(cacheKey, restaurantsSearchResults, TimeSpan.FromMinutes(_cacheSettings.ExpiryInMinutes)); // Adding the postcode to cache so that we get the same results for the postcode search too. _cache.Set(CleanString(restaurantsSearchResults.MetaData.Postcode), restaurantsSearchResults, TimeSpan.FromMinutes(_cacheSettings.ExpiryInMinutes)); _logger.LogDebug($"Results cached for GeoLocation {cacheKey} and postcode {CleanString(restaurantsSearchResults.MetaData.Postcode)}"); } } restaurantSearchResultsDto = BuildRestaurantSearchResultsDto(page, pageSize, restaurantsSearchResults, restaurantSearchResultsDto); } catch (Exception ex) { _logger.LogError(ServiceError, ex, GeoLocationErrorMessage); restaurantSearchResultsDto.ErrorMessage = GeoLocationErrorMessage; } return(restaurantSearchResultsDto); }
public async Task <RestaurantSearchResultsDto> GetRestaurantsByPostCode(string postcode, int page, int pageSize) { var restaurantSearchResultsDto = new RestaurantSearchResultsDto(); try { var cleanPostCode = CleanString(postcode); _cache.TryGetValue(cleanPostCode, out RestaurantsSearchResults restaurantsSearchResults); if (restaurantsSearchResults == null) { _logger.LogDebug($"Search for postcode {cleanPostCode} not found in cache, fetching from API"); var response = await _restaurantsApiClient.GetRestaurantsByPostCode(cleanPostCode); var hasNoResults = !response.IsSuccess || response.RestaurantsSearchResults?.Restaurants == null || !response.RestaurantsSearchResults.Restaurants.Any(); if (hasNoResults) { _logger.LogInformation($"Search for postcode {cleanPostCode} found no results"); restaurantSearchResultsDto.ErrorMessage = $"No resturants found for postcode: {postcode}"; } else { restaurantsSearchResults = response.RestaurantsSearchResults; _cache.Set(cleanPostCode, restaurantsSearchResults, TimeSpan.FromMinutes(_cacheSettings.ExpiryInMinutes)); _logger.LogDebug($"Results cached for postcode {cleanPostCode}"); } } restaurantSearchResultsDto = BuildRestaurantSearchResultsDto(page, pageSize, restaurantsSearchResults, restaurantSearchResultsDto); } catch (Exception ex) { var errorMessage = $"Error occured while getting resturants by postcode: {postcode}"; _logger.LogError(ServiceError, ex, errorMessage); restaurantSearchResultsDto.ErrorMessage = errorMessage; } return(restaurantSearchResultsDto); }
public StaticPagedList <RestaurantViewModel> Resolve(RestaurantSearchResultsDto source, RestaurantsSearchViewModel destination, StaticPagedList <RestaurantViewModel> destMember, ResolutionContext context) { if (source.Restaurants == null) { return(default);
private RestaurantSearchResultsDto BuildRestaurantSearchResultsDto(int page, int pageSize, RestaurantsSearchResults restaurantsSearchResults, RestaurantSearchResultsDto restaurantSearchResultsDto) { var restaurants = restaurantsSearchResults?.Restaurants; if (restaurants != null && restaurants.Any()) { var resultRestaurants = restaurantsSearchResults.Restaurants .Skip((page - 1) * pageSize).Take(pageSize).ToList(); restaurantSearchResultsDto.Restaurants = new StaticPagedList <RestaurantDto>( _mapper.Map <List <RestaurantDto> >(resultRestaurants), page, pageSize, restaurantsSearchResults.MetaData.ResultCount); restaurantSearchResultsDto.PostCode = restaurantsSearchResults.MetaData.Postcode; restaurantSearchResultsDto.Latitude = restaurantsSearchResults.MetaData.Latitude.ToString(); restaurantSearchResultsDto.Longitude = restaurantsSearchResults.MetaData.Longitude.ToString(); } return(restaurantSearchResultsDto); }
private RestaurantsSearchViewModel BuildViewModel(RestaurantSearchResultsDto restaurantSearchResultsDto) { var restaurantsSearchViewModel = _mapper.Map <RestaurantsSearchViewModel>(restaurantSearchResultsDto); return(restaurantsSearchViewModel); }