//***** 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); }
//***** 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); }
//***** Insert Course ***** public static string Insert(Course Course_DB) { string errors = String.Empty; using (TAhubContext db = new TAhubContext()) { try { //Check for duplicates here Course course = db.Courses.FirstOrDefault(x => x.SLN == Course_DB.SLN); if (course == null) { db.Courses.Add(Course_DB); db.SaveChanges(); } else { errors += $"Course '{Course_DB.SLN}' already exists in our system\n"; } } catch (Exception ex) { errors += "Unable to Process Request.\n"; } } return(errors); }
//***** Insert Professor ***** public static string Insert(Professor Prof_DB) { string errors = String.Empty; using (TAhubContext db = new TAhubContext()) { try { //Check for duplicates here if (db.Professors.FirstOrDefault(x => x.Email == Prof_DB.Email) == null) { db.Professors.Add(Prof_DB); db.SaveChanges(); } else { errors += "Your email address has already been registered to another account. Do not try to fool us -_-\n"; } } catch (Exception) { errors += "Unable to Process Request.\n"; } } return(errors); }
//*****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); }
//***** Insert Schedule ***** public static string Insert(Schedule Schedule_DB) { string errors = String.Empty; using (TAhubContext db = new TAhubContext()) { try { db.Schedules.Add(Schedule_DB); db.SaveChanges(); } catch (Exception) { errors += "Unable to Process Request.\n"; } } return(errors); }
//***** 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); }
//***** 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); }
//*****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); }