Esempio n. 1
0
        public async Task GetByIPAsync_ShouldReturnIPDetails_WhenIPDetailsExists()
        {
            // Arrange
            string ip        = "10.10.10.10";
            string city      = "Naxxar";
            string continent = "Europe";
            string country   = "Malta";
            double longitude = 1.123;
            double latitude  = 1.123;
            int    id        = 1;
            List <IPDetailsEntity> iPDetailsEntities = new();

            iPDetailsEntities.Add(new IPDetailsEntity()
            {
                IP        = ip,
                City      = city,
                Continent = continent,
                Country   = country,
                Latitude  = latitude,
                Longitude = longitude,
                Id        = id
            });
            _ipManagerRepoMock.Setup(x => x.FindByIPAsync(ip)).ReturnsAsync(iPDetailsEntities);

            // Act
            var details = await _sut.GetByIPAsync(ip);

            // Assert
            Assert.AreEqual(details.IP, ip);
            Assert.AreEqual(details.City, city);
            Assert.AreEqual(details.Continent, continent);
            Assert.AreEqual(details.Country, country);
            Assert.AreEqual(details.Longitude, longitude);
            Assert.AreEqual(details.Latitude, latitude);
        }
 public async Task <ActionResult <IPDetails> > GetDetailsFromDB(string ip)
 {
     try
     {
         IPManagerService managerService = new IPManagerService(repository);
         return(await managerService.GetByIPAsync(ip));
     } catch (DuplicateIPAddressException exception)
     {
         return(StatusCode(StatusCodes.Status500InternalServerError, exception.Message));
     } catch (Exception exception)
     {
         return(StatusCode(StatusCodes.Status500InternalServerError,
                           $"Error whilst getting IP details from database. {exception.Message}"));
     }
 }
        public async Task <ActionResult <IPDetails> > Details(string ip)
        {
            try
            {
                // Attempt to retrieve from cache
                if (!memoryCache.TryGetValue(ip, out IPDetails details))
                {
                    // Attempt to retrieve from database
                    IPManagerService managerService = new IPManagerService(repository);
                    details = await managerService.GetByIPAsync(ip);

                    if (details == null)
                    {
                        // Get from IPLibrary
                        IPInfoProviderService providerService = new IPInfoProviderService(configuration);
                        details = providerService.GetDetails(ip);

                        // Save in our Database
                        details = await managerService.SaveAsync(details);
                    }

                    // Save in cache for one minute
                    var cacheEntryOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(1));
                    memoryCache.Set(ip, details, cacheEntryOptions);
                }

                return(details);
            }
            catch (IPServiceNotAvailableException ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError,
                                  ex.Message));
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError,
                                  "Error getting IP details"));
            }
        }