public void TestSaveStudentTutorNote() { using (SenecaContext db = new SenecaContext()) { Debug.WriteLine(db.Database.Connection.ConnectionString); db.Database.Log = Console.Write; Student student = (from s in db.Students where s.FirstName == "Jayden" select s).Single(); ApplicationUser user = (from u in db.Users where u.UserName == "p" select u).Single(); TutorNote note = db.TutorNotes.Create(); note.Date = DateTime.Now; note.SessionNote = "xxx"; note.Student = student; note.ApplicationUser = user; db.TutorNotes.Add(note); db.SaveChanges(); TutorNote foundNote = (from n in db.TutorNotes where n.SessionNote == "xxx" select n).SingleOrDefault(); Assert.IsNotNull(foundNote); Assert.AreEqual(student, foundNote.Student); Assert.AreEqual(user, foundNote.ApplicationUser); Assert.IsTrue(student.TutorNotes.Contains(foundNote)); } }
// GET: TutorNotes/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } TutorNote tutorNote = _db.TutorNotes.Find(id); if (tutorNote == null) { return(HttpNotFound()); } var viewModel = new AddEditTutorNoteViewModel(); List <SelectListItem> userList = new List <SelectListItem>(); foreach (ApplicationUser user in _db.Users) { if (user.UserName == tutorNote.ApplicationUser.UserName) { userList.Add(new SelectListItem { Text = user.FirstName + @" " + user.LastName, Value = user.Id, Selected = true }); } else { userList.Add(new SelectListItem { Text = user.FirstName + @" " + user.LastName, Value = user.Id, Selected = false }); } } List <SelectListItem> studentList = new List <SelectListItem>(); foreach (Student student in _db.Students) { if (student.Id == tutorNote.Student.Id) { studentList.Add(new SelectListItem { Text = student.FirstName, Value = student.Id.ToString(), Selected = true }); } else { studentList.Add(new SelectListItem { Text = student.FirstName, Value = student.Id.ToString(), Selected = false }); } } viewModel.Id = tutorNote.Id; viewModel.Date = tutorNote.Date; //viewModel.User = tutorNote.ApplicationUser; viewModel.Users = userList; viewModel.Students = studentList; return(View(viewModel)); }
// GET: TutorNotes/Delete/5 public ActionResult Delete(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } TutorNote tutorNote = _db.TutorNotes.Find(id); if (tutorNote == null) { return(HttpNotFound()); } return(View(tutorNote)); }
public ActionResult SaveTutorNote(int studentId, DateTime date, string sessionNote) { if (sessionNote.Length != 0) { var userId = User.Identity.GetUserId(); var tutorNote = new TutorNote { Date = date.AddHours(5), SessionNote = sessionNote, ApplicationUser = (from u in _db.Users where u.Id == userId select u).Single(), Student = (from s in _db.Students where s.Id == studentId select s).Single() }; _db.TutorNotes.Add(tutorNote); _db.SaveChanges(); String json = JsonConvert.SerializeObject(tutorNote, Formatting.Indented); return(Content(json, "application/json")); } else { return(Content(null, "application/json")); } }
public ActionResult EditTutorSessionNote(int?id, string sessionNote) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } TutorNote tutorNote = _db.TutorNotes.Find(id); if (tutorNote == null) { return(HttpNotFound()); } //var studentId = tutorNote.Student.Id; // keep this in case we delete the note if (sessionNote.Length == 0) { // delete this note _db.TutorNotes.Remove(tutorNote); _db.SaveChanges(); // create a new empty note, because Ajax Error: not hit. var note = new TutorNote { Id = 0 }; String json = JsonConvert.SerializeObject(note, Formatting.Indented); return(Content(json, "application/json")); } else { tutorNote.SessionNote = sessionNote; _db.SaveChanges(); String json = JsonConvert.SerializeObject(tutorNote, Formatting.Indented); return(Content(json, "application/json")); } }