public ActionResult editLocation(int locationID, int schoolID, string description, Location location, string callback, HttpPostedFileWrapper image) { ajaxReturnData data = new ajaxReturnData(); try { using (ApplicationDbContext DB = new ApplicationDbContext()) { location.id = locationID; DB.Locations.Attach(location); DB.Entry(location).State = EntityState.Unchanged; DB.Entry(location).Property(l => l.name).IsModified = true; //DB.Entry(location).Property(l => l.city).IsModified = true; location.addContent(DB, "description", description); if (image != null && image.ContentLength > 0) { string path = "/content/images/uploads/locations/" + locationID; //bool existsfirst = System.IO.Directory.Exists(Server.MapPath(path)); bool exists = System.IO.Directory.Exists(Server.MapPath(path)); if (!exists) { Directory.CreateDirectory(Server.MapPath(path)); } path = path + "/locationImage" + Path.GetExtension(image.FileName); image.SaveAs(Server.MapPath(path)); location.image = path; DB.Entry(location).Property(l => l.image).IsModified = true; } DB.SaveChanges(); School school = new School(); school.id = schoolID; //school.schoolLocation = location; school.locationID = location.id; school.features = new SchoolFeatures(); DB.Schools.Attach(school); DB.Entry(school).State = EntityState.Unchanged; DB.Entry(school).Property(s => s.locationID).IsModified = true; DB.SaveChanges(); } if (string.IsNullOrEmpty(callback)) { data.statusCode = (int)statusCodes.success; } else { data.statusCode = (int)statusCodes.successRun; data.callback = callback; } data.message = "location updated"; return Json(data); } catch (Exception ex) { data.statusCode = (int)statusCodes.fail; data.message = "Failed to update location; " + ex.Message; return Json(data); } }
public ActionResult deleteLocation(int locationID, string callback) { ajaxReturnData data = new ajaxReturnData(); using (ApplicationDbContext DB = new ApplicationDbContext()) { try { List<School> schools = DB.Schools.Where(s => s.locationID == locationID).ToList<School>(); foreach (School school in schools) { school.locationID = null; DB.SaveChanges(); } List<Content> contents = DB.Content.Where(c => c.locationID == locationID).ToList<Content>(); foreach (Content content in contents) { content.locationID = null; DB.SaveChanges(); } Location doomedLocation = new Location(); doomedLocation.id = locationID; DB.Locations.Attach(doomedLocation); DB.Locations.Remove(doomedLocation); DB.SaveChanges(); if (string.IsNullOrEmpty(callback)) { data.statusCode = (int)statusCodes.success; } else { data.statusCode = (int)statusCodes.successRun; data.callback = callback; } data.message = "location removed"; return Json(data); } catch (Exception ex) { data.statusCode = (int)statusCodes.fail; data.message = "Error: " + ex.Message; return Json(data); } } }
public ActionResult addLocation(Location location, string callback, string description, string city, HttpPostedFileWrapper image) { ajaxReturnData data = new ajaxReturnData(); try { using (ApplicationDbContext DB = new ApplicationDbContext()) { DB.Locations.Add(location); DB.SaveChanges(); //add name field //Content content = new Content(); //content.name = "name"; //content.locationID = location.id; //DB.Content.Add(content); //DB.SaveChanges(); //ContentBody cb = new ContentBody(); //cb.contentID = content.contentID; //cb.code = "en"; //cb.body = name; //cb.lastModifiedByID = User.Identity.GetUserId(); //cb.lastModified = DateTime.Now; //DB.ContentBody.Add(cb); //DB.SaveChanges(); //add description field Content content = new Content(); content.name = "description"; content.locationID = location.id; DB.Content.Add(content); DB.SaveChanges(); ContentBody cb = new ContentBody(); cb.contentID = content.contentID; cb.code = "en"; cb.body = description; cb.lastModifiedByID = User.Identity.GetUserId(); cb.lastModified = DateTime.Now; DB.ContentBody.Add(cb); DB.SaveChanges(); ////add city field //content = new Content(); //content.name = "city"; //content.locationID = location.id; //DB.Content.Add(content); //DB.SaveChanges(); //cb = new ContentBody(); //cb.contentID = content.contentID; //cb.code = "en"; //cb.body = city; //cb.lastModifiedByID = User.Identity.GetUserId(); //cb.lastModified = DateTime.Now; //DB.ContentBody.Add(cb); //DB.SaveChanges(); if (image != null && image.ContentLength > 0) { string path = "/content/images/uploads/locations/" + location.id; bool exists = Directory.Exists(Server.MapPath(path)); if (!exists) { Directory.CreateDirectory(Server.MapPath(path)); } path = path + "/locationImage" + Path.GetExtension(image.FileName); image.SaveAs(Server.MapPath(path)); location.image = path; DB.Entry(location).Property(l => l.image).IsModified = true; } //int contentID; //contentID = DB.Content.Where(c => c.locationID == location.id && c.name == "name").FirstOrDefault().contentID; //ContentBody cb = DB.ContentBody.Where(c => c.contentID == contentID && c.code == "en").FirstOrDefault(); //cb.body = name; DB.SaveChanges(); } if (string.IsNullOrEmpty(callback)) { data.statusCode = (int)statusCodes.success; } else { data.statusCode = (int)statusCodes.successRun; data.callback = callback; } data.message = "location added"; return Json(data); } catch (Exception ex) { data.statusCode = (int)statusCodes.fail; data.message = "Failed to add location; " + ex.Message; return Json(data); } }