// DELETE api/values/192.168.1.1 public IHttpActionResult Delete(string ipAddress) { GeolocationDomainResult geolocationDomain = _geolocationLogic.Value.DelteValue(ipAddress); if (!geolocationDomain.Succesful) { if (geolocationDomain.ErrorCode == 404) { var resp = new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent(geolocationDomain.ErrorMsg) }; throw new HttpResponseException(resp); } else { var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(geolocationDomain.ErrorMsg) }; throw new HttpResponseException(resp); } } return(Json(geolocationDomain)); }
/// <summary> /// Remove geolocation information from database /// </summary> /// <param name="ipAddress">Ip address or URL</param> /// <returns>status of operation with deleted geolocation data</returns> public GeolocationDomainResult DelteValue(string ipAddress) { GeolocationDomainResult geolocationDomainResult = new GeolocationDomainResult(); geolocationDomainResult.Succesful = true; using (var db = _geolocationsRepositories.GetGeolocationEntities()) { try { geolocations g = new geolocations(); g = db.geolocations.Where(p => p.ip == ipAddress).FirstOrDefault(); if (g != null) { geolocationDomainResult.Geolocation = new GeolocationDomain(); _mapper.Map(g, geolocationDomainResult.Geolocation); db.geolocations.Remove(g); db.SaveChanges(); } else { geolocationDomainResult.ErrorCode = 404; geolocationDomainResult.ErrorMsg = "The given ip address doesn't exist."; geolocationDomainResult.Succesful = false; } } catch (Exception ex) { geolocationDomainResult.ErrorCode = 0; geolocationDomainResult.ErrorMsg = "Internal DataBase Error."; geolocationDomainResult.Succesful = false; Logger.Log.Error(ex); } } return(geolocationDomainResult); }
// PUT api/values/192.168.1.1 public IHttpActionResult Put([FromBody] GeolocationDomain data) { GeolocationDomainResult geolocationDomain = _geolocationLogic.Value.PutValue(data); if (!geolocationDomain.Succesful) { if (geolocationDomain.ErrorCode == 404) { var resp = new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent(geolocationDomain.ErrorMsg) }; throw new HttpResponseException(resp); } else { var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(geolocationDomain.ErrorMsg) }; throw new HttpResponseException(resp); } } return(Json(geolocationDomain)); }
/// <summary> /// Collect geolocation information /// </summary> /// <param name="ipAddress">Ip Address or Url</param> /// <returns></returns> public IHttpActionResult Get(string ipAddress) { GeolocationDomainResult geolocationDomain = _geolocationLogic.Value.GetValue(ipAddress); if (!geolocationDomain.Succesful) { var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(geolocationDomain.ErrorMsg) }; throw new HttpResponseException(resp); } return(Json(geolocationDomain)); }
// POST api/values public IHttpActionResult Post([FromBody] GeolocationDomain data) { GeolocationDomainResult geolocationDomain = _geolocationLogic.Value.AddValue(data); if (!geolocationDomain.Succesful) { if (geolocationDomain.ErrorCode == 302) { return(RedirectToRoute("GeolocationApiGet", new { controller = "Geolocation", ipAddress = data.Ip })); } else { var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(geolocationDomain.ErrorMsg) }; throw new HttpResponseException(resp); } } return(Json(geolocationDomain)); }
/// <summary> /// Add geolocation data to database /// </summary> /// <param name="data">geolocation data</param> /// <returns>status of operation with added geolocation data</returns> public GeolocationDomainResult AddValue(GeolocationDomain data) { GeolocationDomainResult geolocationDomainResult = new GeolocationDomainResult(); geolocationDomainResult.Succesful = true; using (var db = _geolocationsRepositories.GetGeolocationEntities()) { try { geolocations g = new geolocations(); g = db.geolocations.Where(p => p.ip == data.Ip).FirstOrDefault(); if (g == null) { g = new geolocations(); _mapper.Map(data, g); db.geolocations.Add(g); db.SaveChanges(); geolocationDomainResult.Geolocation = new GeolocationDomain(); _mapper.Map(g, geolocationDomainResult.Geolocation); } else { geolocationDomainResult.ErrorCode = 302; geolocationDomainResult.ErrorMsg = "The given ip address exist."; geolocationDomainResult.Succesful = false; } } catch (Exception ex) { geolocationDomainResult.ErrorCode = 0; geolocationDomainResult.ErrorMsg = "Internal DataBase Error."; geolocationDomainResult.Succesful = false; Logger.Log.Error(ex); } } return(geolocationDomainResult); }
/// <summary> /// Method allow collect information about geolocation. /// If @ipAddress exist in database the collection is get from there. /// If @ipAddress not exist in database remote API is called and resoult is stored in database. /// </summary> /// <param name="ipAddress">Ip addres or URL</param> /// <returns>status of operation with collected geolocation data</returns> public GeolocationDomainResult GetValue(string ipAddress) { GeolocationDomainResult geolocationDomainResult = new GeolocationDomainResult(); geolocationDomainResult.Succesful = true; #region ReadFromDataBase using (var db = _geolocationsRepositories.GetGeolocationEntities()) { try { geolocations g = new geolocations(); g = db.geolocations.Where(p => p.ip == ipAddress).FirstOrDefault(); if (g != null) { geolocationDomainResult.Geolocation = new GeolocationDomain(); _mapper.Map(g, geolocationDomainResult.Geolocation); return(geolocationDomainResult); } } catch (Exception ex) { Logger.Log.Error(ex); } } #endregion #region CallRemoteApi List <string> ipAddresses = new List <string> { ipAddress }; var response = _ipstackApi.Get(ipAddresses); geolocationDomainResult.Succesful = response.ipstackErrorData.success; if (geolocationDomainResult.Succesful) { geolocationDomainResult.Geolocation = new GeolocationDomain(); _mapper.Map(response.ipstackData, geolocationDomainResult.Geolocation); using (var db = _geolocationsRepositories.GetGeolocationEntities()) { ///Geolocation api alweys return ip not url ///in database store ip/url from input geolocations g = new geolocations(); _mapper.Map(geolocationDomainResult.Geolocation, g); g.ip = ipAddress; try { db.geolocations.Add(g); db.SaveChanges(); } catch (Exception ex) { Logger.Log.Error(ex); } } } else { geolocationDomainResult.ErrorMsg = "Remote api IpStack returend error: " + response.ipstackErrorData.error.info; Logger.Log.Error(geolocationDomainResult.ErrorMsg); } #endregion return(geolocationDomainResult); }