Esempio n. 1
0
        public async Task <ActionResult <HotelsSearchResponse> > Search(string cityCode, DateTime checkInDate, DateTime checkOutDate, int pageSize, int pageOffset, CancellationToken cancellationToken)
        {
            try
            {
                HotelsSearchUserRequest hotelsSearchRequest = new HotelsSearchUserRequest(cityCode, checkInDate, checkOutDate, pageSize, pageOffset);
                ValidateAndSanitazeHotelsSearchRequest(hotelsSearchRequest);

                HotelsSearchResponse response;

                bool isCacheHit = _cache.TryGetValue(hotelsSearchRequest.ToCacheKey(), out response);

                if (!isCacheHit)
                {
                    _logger.LogInformation($"No cache hit. CityCode: {hotelsSearchRequest.CityCode}, " +
                                           $"CheckIn: {hotelsSearchRequest.CheckInDate}, CheckOut: { hotelsSearchRequest.CheckOutDate}, pageSize: {hotelsSearchRequest.PageSize}, pageOffset: {hotelsSearchRequest.PageOffset}");

                    response = await _hotelsSearchService.SearchHotels(hotelsSearchRequest, cancellationToken);

                    var cacheEntryOptions = new MemoryCacheEntryOptions()
                                            .SetSize(1)
                                            .SetSlidingExpiration(TimeSpan.FromMinutes(2))
                                            // Remove from cache after this time, regardless of sliding expiration
                                            .SetAbsoluteExpiration(TimeSpan.FromMinutes(10));

                    _cache.Set(hotelsSearchRequest.ToCacheKey(), response, cacheEntryOptions);
                }
                else
                {
                    _logger.LogInformation($"Cache hit for request. CityCode: {hotelsSearchRequest.CityCode}, " +
                                           $"CheckIn: {hotelsSearchRequest.CheckInDate}, CheckOut: { hotelsSearchRequest.CheckOutDate}, pageSize: {hotelsSearchRequest.PageSize}, pageOffset: {hotelsSearchRequest.PageOffset}");
                }


                return(Ok(response));
            }
            catch (ArgumentException argEx)
            {
                _logger.LogWarning(argEx, argEx.Message);
                return(StatusCode((int)HttpStatusCode.BadRequest, new { message = argEx.Message }));
            }
            catch (HttpRequestException reqEx)
            {
                _logger.LogError(reqEx, "Cannot retrieve Amadeus Hotels information from Amadeus API.");
                return(StatusCode((int)HttpStatusCode.BadGateway, new { message = "Cannot retrieve Amadeus Hotels information from Amadeus API. Reason: " + reqEx.Message }));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Internal error.");
                return(StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal error." }));
            }
        }