public async Task SaveAsync_ShouldAdd_WhenNotExists() { // Arrange string ip = "30.30.30.30"; string city = "Naxxar"; string continent = "Europe"; string country = "Malta"; double longitude = 1.123; double latitude = 1.123; IPDetails inputIPDetails = new IPDetails() { IP = ip, City = city, Continent = continent, Country = country, Latitude = latitude, Longitude = longitude, }; IPDetailsEntity inputIPDetailsEntity = IPDetailsMapper.ToEntity(inputIPDetails); // Expected List <IPDetailsEntity> expectedIPDetails = new(); _ipManagerRepoMock.Setup(x => x.FindByIPAsync(ip)).ReturnsAsync(expectedIPDetails); _ipManagerRepoMock.Setup(x => x.AddAsync(inputIPDetailsEntity)).ReturnsAsync(inputIPDetailsEntity); // Act var details = await _sut.SaveAsync(inputIPDetails); // Assert Assert.AreEqual(inputIPDetails, details); }
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")); } }