public async Task <ActionResult <IpDetailDTO> > GetIpDetail(string ip) { try { // ensure that I will only search for IPs and I will not have random strings in my Database if (!System.Text.RegularExpressions.Regex.IsMatch(ip, @"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$")) { return(NotFound()); } // first check the cache var ipDetailsFromCache = IpRequestHandlerService.CheckCacheForIp(ip); if (ipDetailsFromCache != null) { var ipdDTO = Mapper.Map <IpDetailDTO>(ipDetailsFromCache); return(ipdDTO); } // then check the db. If it is in there then update cache and return var ipDetailsFromDb = await IpDetailsRepository.GetIpDetailAsync(ip); if (ipDetailsFromDb != null) { await IpRequestHandlerService.UpdateCacheAsync(ipDetailsFromDb); var ipdDTO = Mapper.Map <IpDetailDTO>(ipDetailsFromDb); return(ipdDTO); } // lastly get the IP from library. If its found then update both db and cache var ipDetailsFromLibrary = await IpRequestHandlerService.CheckLibraryForIPAsync(ip); if (ipDetailsFromLibrary != null) { await IpDetailsRepository.AddIpDetailsAsync(ipDetailsFromLibrary); await IpRequestHandlerService.UpdateCacheAsync(ipDetailsFromLibrary); var ipdDTO = Mapper.Map <IpDetailDTO>(ipDetailsFromLibrary); return(ipdDTO); } return(NotFound()); } catch (Exception) { return(StatusCode(Microsoft.AspNetCore.Http.StatusCodes.Status500InternalServerError)); } }
public async Task <ActionResult <IEnumerable <IpDetailDTO> > > GetIpdetails() { List <IpDetailDTO> ipList = new List <IpDetailDTO>(); var ips = await IpDetailsRepository.GetIpDetailsAsync(); List <IpDetail> ipd = ips.ToList(); foreach (var ip in ipd) { ipList.Add(Mapper.Map <IpDetailDTO>(ip)); } return(ipList); }