public ActionResult Create(Note note) { if (Session["email"] == null) { return(RedirectToAction("Login", "User")); } else { using (NoteAppEntities ne = new NoteAppEntities()) { note.email = Session["email"].ToString(); if (note.ImageFile != null) { String fileName = Path.GetFileNameWithoutExtension(note.ImageFile.FileName); String fileExt = Path.GetExtension(note.ImageFile.FileName); fileName = fileName + DateTime.Now.ToString("ddmmyyyyhhmmss") + fileExt; note.image = "~/Images/" + fileName; fileName = Path.Combine(Server.MapPath("~/Images"), fileName); note.ImageFile.SaveAs(fileName); } ne.Notes.Add(note); ne.SaveChanges(); } return(RedirectToAction("Dashboard")); } }
public ActionResult Edit(int id, Note note) { using (NoteAppEntities ne = new NoteAppEntities()) { if (Session["email"] == null) { return(RedirectToAction("Login", "User")); } else { if (note.image != null) { String fileName = Path.GetFileNameWithoutExtension(note.ImageFile.FileName); String fileExt = Path.GetExtension(note.ImageFile.FileName); fileName = fileName + DateTime.Now.ToString("ddmmyyyyhhmmss") + fileExt; note.image = "~/Images/" + fileName; fileName = Path.Combine(Server.MapPath("~/Images"), fileName); note.ImageFile.SaveAs(fileName); } ne.Notes.Attach(note); ne.Entry(note).Property(x => x.title).IsModified = true; ne.Entry(note).Property(x => x.content).IsModified = true; note.email = Session["email"].ToString(); ne.SaveChanges(); return(RedirectToAction("Dashboard", "Note")); } } }
//Toggle Sharing public ActionResult ToggleSharing(int id) { using (NoteAppEntities ne = new NoteAppEntities()) { Note note = ne.Notes.Find(id); if (note.shared == true) { note.shared = false; ne.SaveChanges(); } else { note.shared = true; ne.SaveChanges(); } return(RedirectToAction("ViewNote", "Note", new { id = id })); } }
//Delete public ActionResult Delete(int id) { using (NoteAppEntities ne = new NoteAppEntities()) { if (Session["email"] == null) { return(RedirectToAction("Login", "User")); } else { Note note = ne.Notes.Find(id); ne.Notes.Remove(note); ne.SaveChanges(); if (note.image != null) { String imagePath = Request.MapPath(note.image); System.IO.File.Delete(imagePath); } return(RedirectToAction("Dashboard")); } } }
public ActionResult Register(User user) { if (ModelState.IsValid) { using (NoteAppEntities ne = new NoteAppEntities()) { var auth = ne.Users.Where(x => x.email == user.email).Count(); if (auth == 1) { ViewBag.Message = "An account is already associated with this email. Please try again."; return(View("Register", user)); } else { ne.Users.Add(user); ne.SaveChanges(); ModelState.Clear(); user = null; ViewBag.Message = "Account created!"; } } } return(View(user)); }