private static void CreateEditMarkingPeriod(string op, int?markingPeriodId, string period, bool fullYear, int schoolYearId, DateTime start, DateTime end) { try { using (SmacEntities context = new SmacEntities()) { if (op.Equals("ADD")) { if ((from a in context.MarkingPeriods where a.Period == period && a.FullYear == fullYear && a.SchoolYearId == schoolYearId select a).FirstOrDefault() != null) { throw new Exception("Marking period was not created. Marking period already exists for this school year."); } MarkingPeriod MarkingPeriod = new MarkingPeriod() { SchoolYear = (from a in context.SchoolYears where a.SchoolYearId == schoolYearId select a).FirstOrDefault(), Period = fullYear ? null : period, FullYear = fullYear, StartDate = start, EndDate = end }; context.MarkingPeriods.Add(MarkingPeriod); context.SaveChanges(); } else if (op.Equals("EDIT")) { var mPeriod = (from a in context.MarkingPeriods where a.MarkingPeriodId == markingPeriodId.Value select a).FirstOrDefault(); if (mPeriod == null) { throw new Exception("Marking period was not updated. Marking period not found."); } if (GetMarkingPeriod(period, fullYear, schoolYearId, markingPeriodId.Value, context) != null) { throw new Exception("Marking period was not updated. New marking period values already exist."); } mPeriod.Period = fullYear ? null : period; mPeriod.FullYear = fullYear; mPeriod.StartDate = start; mPeriod.EndDate = end; mPeriod.SchoolYear = (from a in context.SchoolYears where a.SchoolYearId == schoolYearId select a).FirstOrDefault(); context.Entry(mPeriod).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } } } catch (Exception ex) { throw ex; } }
private static void CreateEditSchoolYear(string op, int schoolId, string year, DateTime start, DateTime end, int?schoolYearId) { try { using (SmacEntities context = new SmacEntities()) { if (op.Equals("ADD")) { if ((from a in context.SchoolYears where a.Year == year select a).FirstOrDefault() != null) { throw new Exception("School year was not created. School year already exists for this school."); } else { SchoolYear schoolyear = new SchoolYear() { School = (from a in context.Schools where a.SchoolId == schoolId select a).FirstOrDefault(), Year = year, StartDate = start, EndDate = end }; context.SchoolYears.Add(schoolyear); context.SaveChanges(); } } else if (op.Equals("EDIT")) { if ((from a in context.SchoolYears where a.SchoolYearId == schoolYearId.Value select a).FirstOrDefault() == null) { throw new Exception("Subject was not updated. Subject name not found."); } else if ((from a in context.SchoolYears where a.SchoolYearId != schoolYearId.Value && a.SchoolId == schoolId && a.Year == year select a).FirstOrDefault() != null) { throw new Exception("Subject was not updated. New subject name already exists in database."); } else { var schYear = (from a in context.SchoolYears where a.SchoolYearId == schoolYearId.Value select a).FirstOrDefault(); schYear.Year = year; schYear.StartDate = start; schYear.EndDate = end; context.Entry(schYear).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } } } } catch (Exception ex) { throw ex; } }
private static void CreateEditClass(string op, int schoolId, int subjectId, int?classId, string description, string className) { try { using (SmacEntities context = new SmacEntities()) { if (op.Equals("ADD")) { if ((from a in context.Classes where a.ClassName == className && a.SchoolId == schoolId && a.SubjectId == subjectId select a).FirstOrDefault() != null) { throw new Exception("Class was not created. Class name already exists for this school/subject."); } else { Class Class = new Class() { ClassName = className, Subject = (from a in context.Subjects where a.SubjectId == subjectId select a).FirstOrDefault(), Description = string.IsNullOrWhiteSpace(description) ? null : description }; context.Classes.Add(Class); context.SaveChanges(); } } else if (op.Equals("EDIT")) { var mClass = (from a in context.Classes where a.ClassId == classId.Value select a).FirstOrDefault(); if (mClass == null) { throw new Exception("Class was not updated. Class name not found."); } if ((from a in context.Classes where a.ClassName == className && a.SubjectId == mClass.SubjectId && a.ClassId != classId.Value select a).FirstOrDefault() != null) { throw new Exception("Class was not updated. Class name already exists for subject/school."); } mClass.ClassName = className; mClass.Subject = (from a in context.Subjects where a.SubjectId == subjectId select a).FirstOrDefault(); mClass.Description = string.IsNullOrWhiteSpace(description) ? null : description; context.Entry(mClass).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } } } catch (Exception ex) { throw ex; } }
private static void CreateEditClub(string op, int?schoolId, string clubName, string description, int?clubId) { try { using (SmacEntities context = new SmacEntities()) { if (op.Equals("ADD")) { if ((from a in context.Clubs where a.SchoolId == schoolId.Value && a.ClubName == clubName select a).FirstOrDefault() != null) { throw new Exception("Club was not created. Club name already exists for this school."); } else { Club Club = new Club() { School = (from a in context.Schools where a.SchoolId == schoolId select a).FirstOrDefault(), Description = !string.IsNullOrWhiteSpace(description) ? description : null, ClubName = clubName }; context.Clubs.Add(Club); context.SaveChanges(); } } else if (op.Equals("EDIT")) { if ((from a in context.Clubs where a.ClubId == clubId.Value select a).FirstOrDefault() == null) { throw new Exception("Club was not updated. Club not found."); } else if ((from a in context.Clubs where a.ClubId != clubId.Value && a.SchoolId == schoolId.Value && a.ClubName == clubName select a).FirstOrDefault() != null) { throw new Exception("Club was not updated. New club name already exists in database."); } else { var Club = (from a in context.Clubs where a.ClubId == clubId select a).FirstOrDefault(); Club.ClubName = clubName; Club.Description = !string.IsNullOrWhiteSpace(description) ? description : null; context.Entry(Club).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } } } } catch (Exception ex) { throw ex; } }
private static void CreateEditSubject(string op, int?schoolId, string subjName, int?subjectId) { try { using (SmacEntities context = new SmacEntities()) { if (op.Equals("ADD")) { if ((from a in context.Subjects where a.SchoolId == schoolId.Value && a.SubjectName == subjName select a).FirstOrDefault() != null) { throw new Exception("Subject was not created. Subject name already exists for this school."); } else { Subject subject = new Subject() { School = (from a in context.Schools where a.SchoolId == schoolId.Value select a).FirstOrDefault(), SubjectName = subjName }; context.Subjects.Add(subject); context.SaveChanges(); } } else if (op.Equals("EDIT")) { if ((from a in context.Subjects where a.SubjectId == subjectId.Value select a).FirstOrDefault() == null) { throw new Exception("Subject was not updated. Subject name not found."); } else if ((from a in context.Subjects where a.SchoolId == schoolId && a.SubjectName == subjName && a.SubjectId != subjectId.Value select a).FirstOrDefault() != null) { throw new Exception("Subject was not updated. New subject name already exists in database."); } else { var subject = (from a in context.Subjects where a.SubjectId == subjectId select a).FirstOrDefault(); subject.SubjectName = subjName; context.Entry(subject).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } } } } catch (Exception ex) { throw ex; } }
private static void CreateEditSection(string op, int schoolId, int subjectId, int classId, int?sectionId, string sectionName, string description) { try { using (SmacEntities context = new SmacEntities()) { if (op.Equals("ADD")) { if ((from a in context.Sections where a.SchoolId == schoolId && a.SubjectId == subjectId && a.ClassId == classId && a.SectionName == sectionName select a).FirstOrDefault() != null) { throw new Exception("Section was not created. Section name already exists for this school/subject/class."); } else { Section section = new Section() { Class = (from a in context.Classes where a.ClassId == classId select a).FirstOrDefault(), SectionName = sectionName, Description = description }; context.Sections.Add(section); context.SaveChanges(); } } else if (op.Equals("EDIT")) { var mSection = (from a in context.Sections where a.SectionId == sectionId.Value select a).FirstOrDefault(); if (mSection == null) { throw new Exception("Section was not updated. Section name not found."); } if ((from a in context.Sections where a.SchoolId == schoolId && a.SubjectId == subjectId && a.ClassId == classId && a.SectionName == sectionName && a.SectionId != sectionId.Value select a).FirstOrDefault() != null) { throw new Exception("Section was not created as it already exists for this school/subject/class."); } mSection.SectionName = sectionName; mSection.Description = description; context.Entry(mSection).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } } } catch (Exception ex) { throw ex; } }
public static void PromoteAdmin(string Id) { try { using (SmacEntities context = new SmacEntities()) { if (UserEntity.GetUser(Id) == null) { throw new Exception("User not set as Admin. User ID doesn't exist."); } else { if (IsAdmin(Id)) { throw new Exception("User not set as Admin. User already set as Admin."); } else { Admin admin = new Admin(); admin.UserId = Id; admin.User = UserEntity.GetUser(Id); context.Admins.Add(admin); context.SaveChanges(); } } } } catch (Exception ex) { throw ex; } }
public static void RevokeAdmin(string Id) { try { using (SmacEntities context = new SmacEntities()) { if (UserEntity.GetUser(Id) == null) { throw new Exception("User not removed as Admin. User doesn't exist."); } else if (!IsAdmin(Id)) { throw new Exception("User not removed as Admin. User not an Admin."); } else { Admin admin = new Admin(); admin.UserId = Id; admin.User = UserEntity.GetUser(Id); context.Admins.Add(admin); context.SaveChanges(); } } } catch (Exception ex) { throw ex; } }
public static void CreateTimeSlot(int schoolId, TimeSpan start, TimeSpan end) { try { using (SmacEntities context = new SmacEntities()) { if ((from a in context.TimeSlots where a.StartTime == start && a.EndTime == end && a.SchoolId == schoolId select a).FirstOrDefault() != null) { throw new Exception("Time slot already exists. Time slot not created."); } TimeSlot ts = new TimeSlot() { School = (from a in context.Schools where a.SchoolId == schoolId select a).FirstOrDefault(), StartTime = start, EndTime = end }; context.TimeSlots.Add(ts); context.SaveChanges(); } } catch (Exception ex) { throw ex; } }
public static void UpdateTimeSlot(int schoolId, TimeSpan start, TimeSpan end, int id) { try { using (SmacEntities context = new SmacEntities()) { if ((from a in context.TimeSlots where a.TimeSlotId == id select a) == null) { throw new Exception("Time slot could not be found"); } if ((from a in context.TimeSlots where a.StartTime == start && a.EndTime == end && a.SchoolId == schoolId && a.TimeSlotId != id select a).FirstOrDefault() != null) { throw new Exception("Time slot already exists. Time slot not updated."); } TimeSlot ts = (from a in context.TimeSlots where a.TimeSlotId == id select a).FirstOrDefault(); ts.StartTime = start; ts.EndTime = end; context.Entry(ts).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } } catch (Exception ex) { throw ex; } }
public static void DeleteUser(string Id) { try { using (SmacEntities context = new SmacEntities()) { if ((from a in context.Users where a.UserId == Id select a).FirstOrDefault() == null) { throw new Exception("User was not deleted. User ID not found."); } else { var user = (from a in context.Users where a.UserId == Id select a).FirstOrDefault(); context.Users.Remove(user); context.SaveChanges(); } } } catch (Exception ex) { throw ex; } }
public static void AddEndDate(string Id, DateTime endDate) { try { using (SmacEntities context = new SmacEntities()) { if (GetUser(Id) == null) { throw new Exception("User was not edited. User ID not found."); } else { var user = GetUser(Id); user.EndDate = endDate; context.Entry(user).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } } } catch (Exception ex) { throw ex; } }
public static void CreateGrade(string val, int schoolId) { try { using (SmacEntities context = new SmacEntities()) { if ((from a in context.Grades where a.SchoolId == schoolId && a.GradeValue == val select a).FirstOrDefault() != null) { throw new Exception("Grade not created as it already exists."); } Grade grade = new Grade() { GradeValue = val, School = (from a in context.Schools where a.SchoolId == schoolId select a).FirstOrDefault() }; context.Grades.Add(grade); context.SaveChanges(); } } catch (Exception ex) { throw ex; } }
public static void CreateSchedule(int sectionId, int tsId, string day, int periodId) { try { using (SmacEntities context = new SmacEntities()) { if ((from a in context.SectionSchedules where a.SectionId == sectionId && a.TimeSlotId == tsId && a.DayValue == day && a.MarkingPeriodId == periodId select a).FirstOrDefault() != null) { throw new Exception("Schedule already exists for this section."); } SectionSchedule sch = new SectionSchedule() { Section = (from a in context.Sections where a.SectionId == sectionId select a).FirstOrDefault(), Day = (from a in context.Days where a.DayValue == day select a).FirstOrDefault(), TimeSlot = (from a in context.TimeSlots where a.TimeSlotId == tsId select a).FirstOrDefault(), MarkingPeriod = (from a in context.MarkingPeriods where a.MarkingPeriodId == periodId select a).FirstOrDefault() }; context.SectionSchedules.Add(sch); context.SaveChanges(); } } catch (Exception ex) { throw ex; } }
public static void SendPrivateMessage(string toUserId, string fromUserId, string content) { try { using (SmacEntities context = new SmacEntities()) { PrivateMessage pm = new PrivateMessage() { Content = content, DateSent = DateTime.Now, DateRead = null, UserSentFrom = UserEntity.GetUser(fromUserId, context), UserSentTo = UserEntity.GetUser(toUserId, context), ToUser = toUserId, FromUser = fromUserId }; context.PrivateMessages.Add(pm); context.SaveChanges(); } } catch (Exception ex) { throw ex; } }
public static void UpdateUserCred(string id, string username, string password) { try { using (SmacEntities context = new SmacEntities()) { var cred = GetUserCred(id); if (cred == null) { throw new Exception("User Credentials could not be found."); } if (DoesUsernameExist(username, id)) { throw new Exception("Username already exists. Please select another."); } cred.UserName = username; if (password != null) { cred.Password = GetSHA256Hash(password); } context.Entry(cred).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } } catch (Exception ex) { throw ex; } }
public static void CreateUserCred(string id, string username, string password) { try { using (SmacEntities context = new SmacEntities()) { if (DoesUsernameExist(username, id)) { throw new Exception("Username already exists. Please select another."); } UserCredential cred = new UserCredential() { Password = password, UserName = GetSHA256Hash(password), User = UserEntity.GetUser(id) }; context.UserCredentials.Add(cred); context.SaveChanges(); } } catch (Exception ex) { throw ex; } }
public static void MarkAllConvosAsRead(string userId, string otherUserId) { try { using (SmacEntities context = new SmacEntities()) { var msgs = (from a in context.PrivateMessages where (a.FromUser == userId && a.ToUser == otherUserId) || (a.FromUser == otherUserId && a.ToUser == userId) select a).ToList(); foreach (var pm in msgs) { if (pm.DateRead == null && pm.FromUser != userId) { pm.DateRead = DateTime.Now; context.Entry(pm).State = EntityState.Modified; } } context.SaveChanges(); } } catch (Exception ex) { throw ex; } }
public static void CreateEnrollment(string studentId, int periodId, int sectionId) { try { using (SmacEntities context = new SmacEntities()) { var curEnroll = (from a in context.Enrollments where a.UserId == studentId && a.MarkingPeriodId == periodId && a.SectionId == sectionId select a).FirstOrDefault(); if (curEnroll != null) { throw new Exception("Enrollment already exists for " + curEnroll.Student.User.FirstName + " " + curEnroll.Student.User.LastName); } var enroll = new Enrollment() { Student = (from a in context.Students where a.UserId == studentId select a).FirstOrDefault(), MarkingPeriod = (from a in context.MarkingPeriods where a.MarkingPeriodId == periodId select a).FirstOrDefault(), Section = (from a in context.Sections where a.SectionId == sectionId select a).FirstOrDefault() }; context.Enrollments.Add(enroll); context.SaveChanges(); } } catch (Exception ex) { throw ex; } }
public static void SetGrade(string studentId, int sectionId, int markingPeriodId, string grade) { try { using (SmacEntities context = new SmacEntities()) { var enroll = (from a in context.Enrollments where a.UserId == studentId && a.SectionId == sectionId && a.MarkingPeriodId == markingPeriodId select a).FirstOrDefault(); if (enroll != null) { enroll.GradeValue = grade; context.Entry(enroll).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } else { throw new Exception("Enrollment not found. Grade not set."); } } } catch (Exception ex) { throw ex; } }
public static void PromoteStudent(string uId) { try { using (SmacEntities context = new SmacEntities()) { if (UserEntity.GetUser(uId) == null) { throw new Exception("User not set as Student. User ID doesn't exist."); } else { if (IsStudent(uId)) { throw new Exception("User not set as Student. User already set as Student."); } else { Student Student = new Student(); Student.UserId = uId; Student.User = UserEntity.GetUser(uId); context.Students.Add(Student); context.SaveChanges(); } } } } catch (Exception ex) { throw ex; } }
public static void RemoveGrade(string studentId, int schoolId, int subjectId, int classId, int sectionId, int markingPeriodId) { try { using (SmacEntities context = new SmacEntities()) { var enroll = GetEnrollment(studentId, schoolId, subjectId, classId, sectionId, markingPeriodId); if (enroll != null) { enroll.GradeValue = null; context.Entry(enroll).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } else { throw new Exception("Enrollment not found. Grade not set."); } } } catch (Exception ex) { throw ex; } }
public static KhanShare CreateKhanShare(string title, string url, string apiId) { try { using (SmacEntities context = new SmacEntities()) { var ks = GetKhanShare(title, url, apiId); if (ks != null) { KhanShare newKs = new KhanShare() { ApiId = apiId, Url = url, Title = title }; context.KhanShares.Add(newKs); context.SaveChanges(); } return(GetKhanShare(title, url, apiId)); } } catch (Exception ex) { throw ex; } }
public static void RevokeStudent(string uId) { try { using (SmacEntities context = new SmacEntities()) { if (UserEntity.GetUser(uId) == null) { throw new Exception("User not removed as Student. User doesn't exist."); } else if (!IsStudent(uId)) { throw new Exception("User not removed as Student. User not an Student."); } else { Student Student = new Student(); Student.UserId = uId; Student.User = UserEntity.GetUser(uId); context.Students.Add(Student); context.SaveChanges(); } } } catch (Exception ex) { throw ex; } }
public static void CreatePost(string userId, string title, int sectionId, string content, int?repliedTo) { try { using (SmacEntities context = new SmacEntities()) { var thread = new Thread() { Section = (from a in context.Sections where a.SectionId == sectionId select a).FirstOrDefault(), RepliedTo = repliedTo, DateTimePosted = DateTime.Now, Content = content, ThreadTitle = title, User = (from a in context.Users where a.UserId == userId select a).FirstOrDefault() }; context.Threads.Add(thread); context.SaveChanges(); } } catch (Exception ex) { throw ex; } }
public static void PromoteTeacher(string uId) { try { using (SmacEntities context = new SmacEntities()) { if (UserEntity.GetUser(uId) == null) { throw new Exception("User not set as Teacher. User ID doesn't exist."); } else { if (IsTeacher(uId)) { throw new Exception("User not set as Teacher. User already set as Teacher."); } else { Teacher Teacher = new Teacher(); Teacher.UserId = uId; Teacher.User = UserEntity.GetUser(uId); context.Teachers.Add(Teacher); context.SaveChanges(); } } } } catch (Exception ex) { throw ex; } }
public static void RevokeTeacher(string uId) { try { using (SmacEntities context = new SmacEntities()) { if (UserEntity.GetUser(uId) == null) { throw new Exception("User not removed as Teacher. User doesn't exist."); } else if (!IsTeacher(uId)) { throw new Exception("User not removed as Teacher. User is not a Teacher."); } else { Teacher Teacher = new Teacher(); Teacher.UserId = uId; Teacher.User = UserEntity.GetUser(uId); context.Teachers.Add(Teacher); context.SaveChanges(); } } } catch (Exception ex) { throw ex; } }
private static void CreateEditSchool(string op, string schName, string addr, string city, string state, string zip, string phone, int?schoolId) { try { using (SmacEntities context = new SmacEntities()) { if (op.Equals("ADD")) { School school = new School() { SchoolName = schName, StreetAddress = addr, City = city, State = state, ZipCode = zip, PhoneNumber = phone }; context.Schools.Add(school); context.SaveChanges(); } else if (op.Equals("EDIT")) { var school = GetSchool(schoolId.Value); school.SchoolName = schName; school.StreetAddress = addr; school.City = city; school.State = state; school.ZipCode = zip; school.PhoneNumber = phone; context.Entry(school).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } } } catch (Exception ex) { throw ex; } }
public static void CreateGender(string gType) { using (SmacEntities context = new SmacEntities()) { Gender gender = new Gender() { GenderType = gType }; context.Genders.Add(gender); context.SaveChanges(); } }
public static void UpdateGender(string gType, string newValue) { using (SmacEntities context = new SmacEntities()) { Gender gender = (from a in context.Genders where a.GenderType == gType select a).FirstOrDefault(); if (gender != null) { gender.GenderType = newValue; context.Entry(gender).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } } }