public async Task <IHttpActionResult> CreateGeoInfo([FromBody] string ip) { if (!ModelState.IsValid) { return(JsonResult.BadRequest("IP address has to be a string")); } if (!ValidateIPv4(ip)) { return(JsonResult.BadRequest("Provided IP address is in a wrong format!")); } try { var exists = await GeoInfoExists(ip); if (exists) { return(JsonResult.BadRequest("IP address already exists in the DB!")); } GeoInfo geoInfo = new GeoInfo(); geoInfo = await FillWithDetails(ip); db.GeoInfos.Add(geoInfo); await db.SaveChangesAsync(); return(JsonResult.Success(Mapper.Map <GeoInfo, GeoInfoDto>(geoInfo))); } catch (Exception e) { return(JsonResult.Error(string.Format("Could not create GeoInfo with ip:{0} due to error: {1}", ip, e.Message))); } }
public async Task <IHttpActionResult> GetGeoInfos() { logger.Info("Fetching all GeoInfos from database"); try { var geoInfosList = await db.GeoInfos.ProjectTo <GeoInfoDto>().ToListAsync(); logger.Info(string.Format("Fetched {0} GeoInfos from database", geoInfosList.Count())); return(JsonResult.Success(geoInfosList)); } catch (Exception e) { logger.Error(e.Message); logger.Error(e.StackTrace); return(JsonResult.Error(string.Format("Could not fetch GeoInfos due to error: {0}", e.Message))); } }
private dynamic EnableEmailNotification(dynamic parameters) { if (!Request.Form.email.HasValue) { return(Response.AsJson(JsonResult.Error("Please provide an email address."), HttpStatusCode.BadRequest)); } string email = Request.Form.email; string slug = parameters.slug; var result = _Api.CreateServicehook(slug, String.Format("http://appharbify.com/Sites/{0}/NotifyByEmail?email={1}", slug, Uri.EscapeDataString(email))); if (result.Status != CreateStatus.Created) { return(Response.AsJson(JsonResult.Error("Unable to add service hook."), HttpStatusCode.BadRequest)); } return(Response.AsJson(JsonResult.OK())); }
public async Task <IHttpActionResult> DeleteGeoInfo(int id) { try { GeoInfo geoInfo = await db.GeoInfos.FindAsync(id); if (geoInfo == null) { return(JsonResult.NotFound(string.Format("GeoInfo with id:{0} could not be found!", id))); } db.GeoInfos.Remove(geoInfo); await db.SaveChangesAsync(); return(JsonResult.Success(string.Format("GeoInfo with id:{0} was removed successfully!", id))); } catch (Exception e) { return(JsonResult.Error(string.Format("Error: could not delete GeoInfo with id:{0} due to error :{1}", id, e.Message))); } }
public async Task <IHttpActionResult> GetGeoInfo(int id) { logger.Info(string.Format("Trying to find GeoInfo with id:{0}", id)); try { GeoInfo geoInfo = await db.GeoInfos.Include(gi => gi.Location.Languages).Where(gi => gi.Id == id).SingleOrDefaultAsync(); if (geoInfo == null) { logger.Info(string.Format("GeoInfo with id:{0} could not be found!", id)); return(JsonResult.NotFound(string.Format("GeoInfo with id:{0} could not be found!", id))); } var geoInfoDto = Mapper.Map <GeoInfo, GeoInfoDto>(geoInfo); logger.Info(string.Format("Returning GeoInfo with id:{0}", id)); return(JsonResult.Success(geoInfoDto)); } catch (Exception e) { logger.Error(e.Message); logger.Error(e.StackTrace); return(JsonResult.Error(string.Format("Could not fetch GeoInfo with id:{0} due to error: {1}", id, e.Message))); } }