//***** 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); }
//***** Insert TA ***** public static string Insert(TeachersAssistant TA_DB) { string errors = String.Empty; using (TAhubContext db = new TAhubContext()) { try { //Check for duplicates here if (db.TAs.FirstOrDefault(x => x.Email == TA_DB.Email) == null) { db.TAs.Add(TA_DB); db.SaveChanges(); } else { errors += "Your email address has already been registered to another account. Do not try to fool us -_-\n"; } } catch (Exception ex) { errors += "Unable to Process Request.\n"; } } return(errors); }
public static List <TeachersAssistant> Generate_TAs(int Count) { List <TeachersAssistant> TAs = new List <TeachersAssistant>(); for (int i = 0; i < Count; i++) { TeachersAssistant TA = new TeachersAssistant(); TA.Password = "******"; TA.StudentId = 11507800 + i; TA.GPA = $"3.{i%10}{(i+7)%10}"; int majorIndex = i % TAhub.Models.EnumValues.Majors.Count; TA.Major = TAhub.Models.EnumValues.Majors[majorIndex]; TA.HasExperience = true; TA.Credits = 25 + (i % 50); bool isMale = i % 2 == 0; if (isMale) { TA.FirstName = FirstNames_Male[i % FirstNames_Male.Count]; TA.Gender = "Male"; } else { TA.FirstName = FirstNames_Female[i % FirstNames_Female.Count]; TA.Gender = "Female"; } TA.LastName = LastNames[i % LastNames.Count]; TA.Email = $"{TA.FirstName}.{TA.LastName}@wsu.edu"; TA.Messages = Generate_Messages(); TA.Preference1 = "Cpts 121"; TA.Preference2 = "Psych 230"; TA.Preference3 = "Cpts 322"; TAs.Add(TA); } return(TAs); }
//***** TA DB ***** public static TeachersAssistant TA_ModelToDB(Models.TAModel TA_Model) { TeachersAssistant TA_DB = new TeachersAssistant(); using (TAhubContext db = new TAhubContext()) { try { TA_DB.StudentId = Int32.Parse(TA_Model.StudentId); TA_DB.FirstName = TA_Model.FirstName; TA_DB.LastName = TA_Model.LastName; TA_DB.Email = TA_Model.Login.Username; TA_DB.Password = TA_Model.Login.Password; TA_DB.Credits = Int32.Parse(TA_Model.Credits); TA_DB.GPA = TA_Model.GPA; TA_DB.HasExperience = Models.EnumValues.Bools[(int)TA_Model.HasExperience]; TA_DB.Lab = TA_Model.Lab; TA_DB.Gender = Models.EnumValues.Genders[(int)TA_Model.Gender]; TA_DB.Major = Models.EnumValues.Majors[(int)TA_Model.Major]; TA_DB.WeeklySchedule = TA_Model.WeeklySchedule; int count = TA_Model.CoursePreferences != null ? TA_Model.CoursePreferences.Count : 0; for (int i = 0; i < count; i++) { switch (i) { case 0: TA_DB.Preference1 = TA_Model.CoursePreferences[i]; break; case 1: TA_DB.Preference2 = TA_Model.CoursePreferences[i]; break; case 2: TA_DB.Preference3 = TA_Model.CoursePreferences[i]; break; default: break; } } TA_DB.Course = TA_Model.CourseAssignment; TA_DB.Messages = TA_Model.Messages; } catch (Exception) { TA_DB = null; } } return(TA_DB); }
//***** 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); }