public async Task <ActionResult <List <IPDetails> > > GetDetailsFromDB()
 {
     try
     {
         IPManagerService managerService = new IPManagerService(repository);
         return(await managerService.ListAsync());
     }
     catch (Exception)
     {
         return(StatusCode(StatusCodes.Status500InternalServerError,
                           "Error whilst getting IP details from database"));
     }
 }
        public async Task <ActionResult <Guid> > BatchUpdateWithProgressReport(IPDetails[] detailsArray)
        {
            Guid guid = Guid.NewGuid();
            Progress <ProgressReportModel> progress = new Progress <ProgressReportModel>();

            progress.ProgressChanged += ReportProgress;

            IPManagerService managerService = new IPManagerService(repository);

            // This should run in separate thread
            _ = Task.Run(() => managerService.UpdateBulk(guid, detailsArray, progress));

            return(guid);
        }
 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"));
            }
        }
示例#5
0
 public IPManagerServiceTests()
 {
     _sut = new IPManagerService(_ipManagerRepoMock.Object);
 }