//***** Delete Professor ***** public static bool Delete_Prof(int Id) { bool isSuccessful = true; using (TAhubContext db = new TAhubContext()) { try { Professor deleteProf = db.Professors.FirstOrDefault(x => x.Id == Id); db.Entry(deleteProf).Collection(s => s.Courses).Load(); // loads Professor's Courses db.Entry(deleteProf).Collection(p => p.Courses).Query().Include(r => r.TAs).Load(); //Loads course's TAs //Removes Professor's courses from database db.Courses.RemoveRange(deleteProf.Courses); db.SaveChanges(); //Removes Professor from database db.Entry(deleteProf).State = System.Data.Entity.EntityState.Deleted; db.SaveChanges(); } catch (Exception ex) { isSuccessful = false; } } return(isSuccessful); }
//***** Delete TA by Id***** public static bool Delete_TA(int Id) { bool isSuccessful = true; using (TAhubContext db = new TAhubContext()) { try { TeachersAssistant deleteTA = db.TAs.FirstOrDefault(x => x.Id == Id); db.Entry(deleteTA).Collection(s => s.Messages).Load(); // loads TA's Messsages //Removes TA's Messages from database db.Messages.RemoveRange(deleteTA.Messages); db.SaveChanges(); //Removes TA's WeeklySchedule from database db.Schedules.RemoveRange(deleteTA.WeeklySchedule); db.SaveChanges(); //Removes TA from database db.Entry(deleteTA).State = System.Data.Entity.EntityState.Deleted; db.SaveChanges(); } catch (Exception ex) { isSuccessful = false; } } return(isSuccessful); }
//*****Update Professor Account Info ***** public static Professor Update(Professor Prof_DB) { Professor Prof_Original = null; using (TAhubContext db = new TAhubContext()) { try { Prof_Original = db.Professors.FirstOrDefault(x => x.Id == Prof_DB.Id); db.Entry(Prof_Original).Collection(p => p.Courses).Load(); //Loads Professor's Course db.Entry(Prof_Original).Collection(p => p.Courses).Query().Include(r => r.TAs).Load(); //Loads courses TAs if (Prof_Original != null) { //First Name if (!String.IsNullOrWhiteSpace(Prof_DB.FirstName) && !String.Equals(Prof_Original.FirstName, Prof_DB.FirstName)) { Prof_Original.FirstName = Prof_DB.FirstName; } //Last Name if (!String.IsNullOrWhiteSpace(Prof_DB.LastName) && !String.Equals(Prof_Original.LastName, Prof_DB.LastName)) { Prof_Original.LastName = Prof_DB.LastName; } //Email if (!String.IsNullOrWhiteSpace(Prof_DB.Email) && !String.Equals(Prof_Original.Email, Prof_DB.Email)) { Prof_Original.Email = Prof_DB.Email; } //Password if (!String.IsNullOrWhiteSpace(Prof_DB.Password) && !String.Equals(Prof_Original.Password, Prof_DB.Password)) { Prof_Original.Password = Prof_DB.Password; } db.Entry(Prof_Original).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } else { Prof_Original = null; } } catch (Exception ex) { Prof_Original = null; } } return(Prof_Original); }
//***** Check For Prof Account ***** public static Professor Prof_HasAccount(string email, string password) { Professor professor; using (TAhubContext db = new TAhubContext()) { try { professor = db.Professors.FirstOrDefault(x => (x.Email == email && x.Password == password)); db.Entry(professor).Collection(p => p.Courses).Load(); //Loads Professor's Course db.Entry(professor).Collection(p => p.Courses).Query().Include(r => r.TAs).Load(); //Loads courses TAs } catch (Exception) { professor = null; } } return(professor); }
//***** Get Course From DB ***** public static Course Course_Get(int SLN) { Course course; using (TAhubContext db = new TAhubContext()) { try { course = db.Courses.FirstOrDefault(x => x.SLN == SLN); db.Entry(course).Reference(s => s.Professor).Load(); // loads Course's Professor db.Entry(course).Collection(s => s.TAs).Load(); // loads Course's TAs } catch (Exception) { course = null; } } return(course); }
//***** Get Prof From DB ***** public static Professor Prof_Get(int Id) { Professor professor; using (TAhubContext db = new TAhubContext()) { try { professor = db.Professors.FirstOrDefault(x => x.Id == Id); db.Entry(professor).Collection(p => p.Courses).Load(); //Loads Professor's Courses db.Entry(professor).Collection(p => p.Courses).Query().Include(r => r.TAs).Load(); //Loads courses TAs } catch (Exception) { professor = null; } } return(professor); }
//***** Get TA From DB ***** public static TeachersAssistant TA_Get(int Id) { TeachersAssistant TA; using (TAhubContext db = new TAhubContext()) { try { TA = db.TAs.FirstOrDefault(x => x.Id == Id); db.Entry(TA).Reference(s => s.Course).Load(); // loads Course db.Entry(TA).Collection(s => s.Messages).Load(); // loads Message collection db.Entry(TA).Collection(s => s.WeeklySchedule).Load(); // loads Schedule collection } catch (Exception) { TA = null; } } return(TA); }
//***** Delete Course ***** public static bool Delete_Course(int Id) { bool isSuccessful = true; using (TAhubContext db = new TAhubContext()) { try { Course deleteCourse = db.Courses.FirstOrDefault(x => x.Id == Id); db.Entry(deleteCourse).Collection(s => s.TAs).Load(); // loads Courses TA's db.Entry(deleteCourse).State = System.Data.Entity.EntityState.Deleted; db.SaveChanges(); } catch (Exception ex) { isSuccessful = false; } } return(isSuccessful); }
//***** Check For TA Account ***** public static TeachersAssistant TA_HasAccount(string email, string password) { TeachersAssistant TA; using (TAhubContext db = new TAhubContext()) { try { TA = db.TAs.FirstOrDefault(x => (x.Email == email && x.Password == password)); db.Entry(TA).Reference(s => s.Course).Load(); // loads Course db.Entry(TA).Collection(s => s.Messages).Load(); // loads Message collection db.Entry(TA).Collection(s => s.WeeklySchedule).Load(); // loads Schedule collection } catch (Exception) { TA = null; } } return(TA); }
//***** Delete Message ***** public static bool Delete_Schedule(int Id) { bool isSuccessful = true; using (TAhubContext db = new TAhubContext()) { try { Schedule deleteSchedule = db.Schedules.FirstOrDefault(x => x.Id == Id); db.Entry(deleteSchedule).State = System.Data.Entity.EntityState.Deleted; db.SaveChanges(); } catch (Exception) { isSuccessful = false; } } return(isSuccessful); }
//***** Update a TAs Course Information ***** public static string TA_UpdateCourse(TeachersAssistant TA_DB, Nullable <int> CourseId) { string errors = ""; using (TAhubContext db = new TAhubContext()) { try { TA_DB.Lab = null; TA_DB.CourseId = CourseId; db.Entry(TA_DB).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } catch (Exception ex) { errors += "Unable to Process Request.\n"; } } return(errors); }
//*****Update TA Account Info ***** public static TeachersAssistant Update(TeachersAssistant TA_DB) { TeachersAssistant TA_Original = null; using (TAhubContext db = new TAhubContext()) { try { TA_Original = db.TAs.FirstOrDefault(x => x.Id == TA_DB.Id); db.Entry(TA_Original).Reference(s => s.Course).Load(); // loads Course db.Entry(TA_Original).Collection(s => s.Messages).Load(); // loads Message collection db.Entry(TA_Original).Collection(s => s.WeeklySchedule).Load(); // loads Schedule collection if (TA_Original != null) { //First Name if (!String.IsNullOrWhiteSpace(TA_DB.FirstName) && !String.Equals(TA_Original.FirstName, TA_DB.FirstName)) { TA_Original.FirstName = TA_DB.FirstName; } //Last Name if (!String.IsNullOrWhiteSpace(TA_DB.LastName) && !String.Equals(TA_Original.LastName, TA_DB.LastName)) { TA_Original.LastName = TA_DB.LastName; } //Email if (!String.IsNullOrWhiteSpace(TA_DB.Email) && !String.Equals(TA_Original.Email, TA_DB.Email)) { TA_Original.Email = TA_DB.Email; } //Password if (!String.IsNullOrWhiteSpace(TA_DB.Password) && !String.Equals(TA_Original.Password, TA_DB.Password)) { TA_Original.Password = TA_DB.Password; } //Gender if (!String.IsNullOrWhiteSpace(TA_DB.Gender) && !String.Equals(TA_Original.Gender, TA_DB.Gender)) { TA_Original.Gender = TA_DB.Gender; } //Major if (!String.IsNullOrWhiteSpace(TA_DB.Major) && !String.Equals(TA_Original.Major, TA_DB.Major)) { TA_Original.Major = TA_DB.Major; } //Lab if (TA_Original.Lab != TA_DB.Lab) { TA_Original.Lab = TA_DB.Lab; } //GPA if (!String.IsNullOrWhiteSpace(TA_DB.GPA) && !String.Equals(TA_Original.GPA, TA_DB.GPA)) { TA_Original.GPA = TA_DB.GPA; } //Credits if (TA_Original.Credits != TA_DB.Credits && TA_DB.Credits >= 0) { TA_Original.Credits = TA_DB.Credits; } db.Entry(TA_Original).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } else { TA_Original = null; } } catch (Exception ex) { TA_Original = null; } } return(TA_Original); }