public ActionResult NewTeacher(ListTeacherViewModel model) { // Agrega un nuevo profesor al sistema try { if (ModelState.IsValid) // Checkea los DataAnnotations de ListTeacherViewModel, o sea los [Required] { // Si es un modelo válido, agregar el nuevo profe a la base using (Models.DBContainer db = new Models.DBContainer()) { Teacher teacher = new Teacher(); teacher.first_name = model.first_name; teacher.last_name = model.last_name; teacher.active = model.active; db.Teachers.Add(teacher); db.SaveChanges(); } return(RedirectToAction("ListTeachers", "Admin")); } return(View(model)); } catch (Exception exc) { throw new Exception(exc.Message); } }
public ActionResult EditTeacher(ListTeacherViewModel model) { // Edita al profe y lo persiste en la base de datos try { if (ModelState.IsValid) { using (Models.DBContainer db = new Models.DBContainer()) { Teacher teacher = db.Teachers.Find(model.id_teacher); if (teacher == null) { TempData["Alert"] = "An error occurred, try again later"; return(RedirectToAction("ListTeachers")); } teacher.first_name = model.first_name; teacher.last_name = model.last_name; teacher.active = model.active; db.Entry(teacher).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } return(RedirectToAction("ListTeachers", "Admin")); } return(View(model)); } catch (Exception exc) { throw new Exception(exc.Message); } }
public ActionResult EditSubject(ListSubjectViewModelAdmin model) { // Modifica la materia y la persiste en la base de datos try { if (ModelState.IsValid) { using (Models.DBContainer db = new Models.DBContainer()) { Subject subject = db.Subjects.Find(model.id_subject); if (subject == null) { TempData["Alert"] = "An error occurred, try again later"; return(RedirectToAction("ListSubjects")); } subject.name = model.name; subject.desc = model.description; subject.capacity = model.capacity; subject.time_from = model.time_from; subject.time_to = model.time_to; subject.id_teacher = model.id_teacher; if (subject.time_to <= subject.time_from) { // Horario no válido ViewData["Error"] = "Ending time must be strictly greater than starting time"; PopulateDropDownList(); return(View(model)); } db.Entry(subject).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } return(RedirectToAction("ListSubjects", "Admin")); } // Si el modelo no es válido, tengo que volver a la misma pantalla, con // lo cual hay que rellenar de nuevo el DropDownList PopulateDropDownList(); return(View(model)); } catch (Exception exc) { throw new Exception(exc.Message); } }
public ActionResult NewSubject(ListSubjectViewModelAdmin model) { // Crea una nueva materia y la guarda en el sistema try { if (ModelState.IsValid) { using (Models.DBContainer db = new Models.DBContainer()) { Subject subject = new Subject(); subject.name = model.name; subject.desc = model.description; subject.capacity = model.capacity; subject.time_from = model.time_from; subject.time_to = model.time_to; subject.id_teacher = model.id_teacher; if (subject.time_to <= subject.time_from) { // Horario no válido ViewData["Error"] = "Ending time must be strictly greater than starting time"; PopulateDropDownList(); return(View(model)); } db.Subjects.Add(subject); db.SaveChanges(); } return(RedirectToAction("ListSubjects", "Admin")); } // Si el modelo no es válido, tengo que volver a la misma pantalla, con // lo cual hay que rellenar de nuevo el DropDownList PopulateDropDownList(); return(View(model)); } catch (Exception exc) { throw new Exception(exc.Message); } }
public ActionResult DeleteSubject(int id) { // Elimina la materia cuyo id_subject es id try { using (Models.DBContainer db = new Models.DBContainer()) { Subject subject = db.Subjects.Find(id); if (subject == null) { TempData["Alert"] = "An error occurred, try again later"; return(RedirectToAction("ListSubjects")); } db.Subjects.Remove(subject); db.SaveChanges(); } return(RedirectToAction("ListSubjects", "Admin")); } catch (Exception exc) { throw new Exception(exc.Message); } }
public ActionResult DeleteTeacher(int id) { // Elimina al profesor del sistema try { using (Models.DBContainer db = new Models.DBContainer()) { Teacher teacher = db.Teachers.Find(id); if (teacher == null) { TempData["Alert"] = "An error occurred, try again later"; return(RedirectToAction("ListTeachers")); } db.Teachers.Remove(teacher); db.SaveChanges(); } return(RedirectToAction("ListTeachers", "Admin")); } catch (Exception exc) { throw new Exception(exc.Message); } }
public ActionResult Enroll(int id_subject) { // Recibe por post el id de la materia y trata de inscribirte a la misma try { using (Models.DBContainer db = new Models.DBContainer()) { int id_student = ((Student)Session["User"]).id_student; // Esto es lo mismo de un metodo anterior que esta arriba... hacer alguna funcion para no repetir el codigo // Veo si el alumno ya está inscripto en la materia revisando los registros // de la tabla Subjects_Students var course = (from sub_stu in db.Subjects_Students where sub_stu.id_student == id_student && sub_stu.id_subject == id_subject select sub_stu).FirstOrDefault(); if (course != null) { // El estudiante ya está inscripto, mandar a la lista de materias con un mensaje ViewData["EnrollMessage"] = "You're already enrolled in this subject"; ViewData["TypeMessage"] = "alert-warning"; return(View()); } // Obtengo la lista de los horarios de todas las materias en las que el // estudiante se inscribió var lst_schedules = (from subject in db.Subjects join sub_stu in db.Subjects_Students on subject.id_subject equals sub_stu.id_subject where sub_stu.id_student == id_student select new Schedule { time_from = subject.time_from, time_to = subject.time_to }).ToList(); // Obtengo el horario de la materia a la cual se desea inscribir var subject_schedule = (from subject in db.Subjects where subject.id_subject == id_subject select new Schedule { time_from = subject.time_from, time_to = subject.time_to }).FirstOrDefault(); if (subject_schedule == null) { ViewData["EnrollMessage"] = "An error occurred, try again later"; ViewData["TypeMessage"] = "alert-danger"; return(View()); } // Recorro la lista de horarios de las inscripciones del estudiante // buscando si hay algun solapamiento de horarios foreach (var schedule in lst_schedules) { if (StartsInMiddle(schedule, subject_schedule) || StartsInMiddle(subject_schedule, schedule)) { ViewData["EnrollMessage"] = "This course overlaps with any other subject that you already enrolled. You can't enroll in this course"; ViewData["TypeMessage"] = "alert-info"; return(View()); } } // Si llegó hasta acá significa que no hay horarios solapados... // Inscribirlo en la materia Subject_Student new_subject_student = new Subject_Student(); new_subject_student.id_student = ((Student)Session["User"]).id_student; new_subject_student.id_subject = id_subject; db.Subjects_Students.Add(new_subject_student); db.SaveChanges(); } ViewData["EnrollMessage"] = "The enrollment has been successful"; ViewData["TypeMessage"] = "alert-success"; return(View()); } catch { ViewData["EnrollMessage"] = "An error occurred, try again later"; ViewData["TypeMessage"] = "alert-danger"; return(View()); } }