//[HttpPost] //[ValidateAntiForgeryToken] public ActionResult NewCourse(int id) { ViewBag.Title = "New Course"; if (Helpers.Helpers.IsStaff(User)) { return(RedirectToAction("Index", "Staff")); } Student oStudent = db.Students.SingleOrDefault(s => s.StudentId == id); if (oStudent == null)//Error. Student DNE. { throw new ArgumentException("NewCourse(int id) - Student DNE."); } Parent oParent = db.Parents.Include(p => p.Children).SingleOrDefault(p => p.ParentId == SessionSingleton.Current.ParentId && p.Children.Any(s => s.StudentId == oStudent.StudentId)); if (oParent == null) //Error. This Student/Child doesn't belong to this Parent. { throw new ArgumentException("NewCourse(int id) - Parent DNE."); } ResponseCreateCourseViewModel oRVM = new ResponseCreateCourseViewModel() { CourseVM = new CourseViewModel() { StudentId = id }, TeacherVM = new TeacherViewModel() }; return(View(oRVM)); }
public ActionResult EditCourse(int StudentId, int CourseId) { ViewBag.Title = "Edit Course"; if (Helpers.Helpers.IsStaff(User)) { return(RedirectToAction("Index", "Staff")); } Course oCourse = db.Courses.Include(c => c.Teacher).SingleOrDefault(c => c.CourseId == CourseId); if (oCourse == null)//Error. Course DNE. { throw new ArgumentException("EditCourse(int StudentId, int CourseId) - Course DNE."); } Teacher oTeacher = oCourse.Teacher; Student oStudent = db.Students.SingleOrDefault(s => s.StudentId == StudentId && s.Courses.Any(c => c.CourseId == oCourse.CourseId)); if (oStudent == null)//Error. Student DNE OR doesn't take this Course. { throw new ArgumentException("EditCourse(int StudentId, int CourseId) - Student DNE."); } Parent oParent = db.Parents.Include(p => p.Children).SingleOrDefault(p => p.ParentId == SessionSingleton.Current.ParentId && p.Children.Any(s => s.StudentId == oStudent.StudentId)); if (oParent == null) //Error. This Student/Child doesn't belong to this Parent. { throw new ArgumentException("EditCourse(int StudentId, int CourseId) - Parent DNE."); } ResponseCreateCourseViewModel oRVM = new ResponseCreateCourseViewModel() { CourseVM = new CourseViewModel() { CourseId = oCourse.CourseId, Name = oCourse.Name, TimeStart = oCourse.TimeStart, TimeEnd = oCourse.TimeEnd, ClassType = oCourse.ClassType, SchoolName = oCourse.SchoolName, StudentId = StudentId }, TeacherVM = new TeacherViewModel() { TeacherId = oTeacher.TeacherId, Email = oTeacher.Email, FirstName = oTeacher.FirstName, LastName = oTeacher.LastName, Gender = oTeacher.Gender, Phone = oTeacher.Phone, PreferredMethodOfContact = oTeacher.PreferredMethodOfContact } }; return(View(oRVM)); }
public async Task <ActionResult> AlterCourse(ResponseCreateCourseViewModel oRVM) { TempData["UserMessage"] = new MessageVM() { IsSuccessful = false, Title = "Error!", Message = "Something went wrong." }; if (Helpers.Helpers.IsStaff(User)) { return(RedirectToAction("Index", "Staff")); } if (!ModelState.IsValid) { return(View(oRVM)); } CourseViewModel oCourseVM = oRVM.CourseVM; Course oCourse = db.Courses.Include(c => c.Teacher).SingleOrDefault(c => c.CourseId == oCourseVM.CourseId); if (oCourse == null)//Error. Course DNE. { throw new ArgumentException("AlterCourse(ResponseCreateCourseViewModel oRVM) - Course DNE."); } TeacherViewModel oTeacherVM = oRVM.TeacherVM; Teacher oTeacher = oCourse.Teacher; Student oStudent = db.Students.SingleOrDefault(s => s.StudentId == oCourseVM.StudentId && s.Courses.Any(c => c.CourseId == oCourse.CourseId)); if (oStudent == null)//Error. Student DNE OR doesn't take this Course. { throw new ArgumentException("AlterCourse(ResponseCreateCourseViewModel oRVM) - Student DNE."); } Parent oParent = db.Parents.Include(p => p.Children).SingleOrDefault(p => p.ParentId == SessionSingleton.Current.ParentId && p.Children.Any(s => s.StudentId == oStudent.StudentId)); if (oParent == null) //Error. This Student/Child doesn't belong to this Parent. { throw new ArgumentException("AlterCourse(ResponseCreateCourseViewModel oRVM) - Parent DNE."); } oCourse.Name = oCourseVM.Name; oCourse.TimeStart = oCourseVM.TimeStart; oCourse.TimeEnd = oCourseVM.TimeEnd; oCourse.ClassType = oCourseVM.ClassType; oCourse.SchoolName = oCourseVM.SchoolName; oTeacher.Email = oTeacherVM.Email; oTeacher.FirstName = oTeacherVM.FirstName; oTeacher.LastName = oTeacherVM.LastName; oTeacher.Gender = oTeacherVM.Gender; oTeacher.Phone = oTeacherVM.Phone; oTeacher.PreferredMethodOfContact = oTeacherVM.PreferredMethodOfContact; try { db.SaveChanges(); } catch (Exception e) { return(View(oRVM)); } TempData["UserMessage"] = new MessageVM() { IsSuccessful = true, Title = "Success!", Message = "Your child's course has been successfully updated." }; return(RedirectToAction("Index", new { StudentId = oCourseVM.StudentId, CourseId = oCourse.CourseId })); }
public async Task <ActionResult> CreateCourse(ResponseCreateCourseViewModel oRVM) { TempData["UserMessage"] = new MessageVM() { IsSuccessful = false, Title = "Error!", Message = "Something went wrong." }; if (Helpers.Helpers.IsStaff(User)) { return(RedirectToAction("Index", "Staff")); } if (!ModelState.IsValid) { return(View(oRVM)); } CourseViewModel oCourseVM = oRVM.CourseVM; TeacherViewModel oTeacherVM = oRVM.TeacherVM; Student oStudent = db.Students.Include(s => s.Courses).Include(s => s.Teachers).SingleOrDefault(p => p.StudentId == oCourseVM.StudentId); if (oStudent == null)//Error. Student DNE. { throw new ArgumentException("CreateCourse(ResponseCreateCourseViewModel oRVM) - Student DNE."); } Parent oParent = db.Parents.Include(p => p.Children).SingleOrDefault(p => p.ParentId == SessionSingleton.Current.ParentId && p.Children.Any(s => s.StudentId == oStudent.StudentId)); if (oParent == null) //Error. This Student/Child doesn't belong to this Parent. { throw new ArgumentException("CreateCourse(ResponseCreateCourseViewModel oRVM) - Parent DNE."); } Teacher oTeacher = new Teacher() { Email = oTeacherVM.Email, FirstName = oTeacherVM.FirstName, LastName = oTeacherVM.LastName, Gender = oTeacherVM.Gender, Phone = oTeacherVM.Phone, PreferredMethodOfContact = oTeacherVM.PreferredMethodOfContact }; Course oCourse = new Course() { Name = oCourseVM.Name, TimeStart = oCourseVM.TimeStart, TimeEnd = oCourseVM.TimeEnd, ClassType = oCourseVM.ClassType, SchoolName = oCourseVM.SchoolName, Teacher = oTeacher }; oStudent.Courses.Add(oCourse); //Add Course to Student oStudent.Teachers.Add(oTeacher); //Add Teacher to Student try { if (db.SaveChanges() <= 0) { return(View(oRVM)); } } catch (Exception e) { return(View(oRVM)); } TempData["UserMessage"] = new MessageVM() { IsSuccessful = true, Title = "Success!", Message = "Your child's course has been successfully added." }; return(RedirectToAction("Index", new { StudentId = oStudent.StudentId, CourseId = oCourse.CourseId })); }