public static void UpdateUserInfo(UserInfo userInfo) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { User userToUpdate = (User)context.Set <User>().Find(userInfo.UserID); userToUpdate.UserID = userInfo.UserID; userToUpdate.LastName = userInfo.LastName; userToUpdate.FirstName = userInfo.FirstName; userToUpdate.Patronymic = userInfo.Patronymic; if (userToUpdate.Password == userInfo.Password) //password supplied is crypted already { //do nothing - passwords already the same } else { userToUpdate.Password = Crypter.Blowfish.Crypt(userInfo.Password); } if (!Util.IsValidEmail(userInfo.Email)) { throw new ArgumentException("Email string is not a valid email!"); } userToUpdate.Email = userInfo.Email; userToUpdate.Phone = userInfo.Phone; userToUpdate.DateOfBirth = userInfo.DoB; context.SaveChanges(); } }
public static void UpdateStudent(StudentInfo si) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { Student s = context.Set <Student>().Find(si.StudentID); if (s == null) { throw new ArgumentNullException("StudentID to update cannot be null!"); } if (!Util.IsValidEmail(si.Email)) { throw new ArgumentException("Email string is not a valid email!"); } UsersDAL.UpdateUser(si); s.DateOfJoin = si.DateOfJoin; //find grade ID var gID = (from grade in context.Grades where (grade.GradeNo + grade.Section).Equals(si.GradeName) select grade.GradeID); if (gID.Count() > 0) { s.GradeID = gID.First(); } else { throw new ArgumentException("Invalid grade name!"); } context.SaveChanges(); } }
/// <summary> /// Changes user password /// </summary> /// <param name="UserID">UserID</param> /// <param name="oldPass">old plain text password</param> /// <param name="newPass">new plain text password</param> public static void ChangeUserPassword(int UserID, string oldPass, string newPass) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { if (newPass.Length > 0) { User userToUpdate = (User)context.Set <User>().Find(UserID); //if (userToUpdate.Password.Equals(oldPass)) if (Crypter.CheckPassword(oldPass, userToUpdate.Password)) { //userToUpdate.Password = newPass; userToUpdate.Password = Crypter.Blowfish.Crypt(newPass); context.SaveChanges(); } else { throw new ArgumentException("Old password is incorrect!"); } } else { throw new ArgumentOutOfRangeException("New password is empty!"); } } }
public static void UpdateGroup(int groupID, int teacherID, string subject, string gradeSection, string description) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { //get gradeID using gradeSection int gradeID = (from grade in context.Grades where grade.GradeNo + grade.Section == gradeSection select grade.GradeID).FirstOrDefault(); if (gradeID == 0) { throw new ArgumentOutOfRangeException("Grade name is invalid!"); } //get subjectID int subjID = (from subj in context.Subjects where subj.Title == subject select subj.SubjectID).FirstOrDefault(); if (subjID == 0) { throw new ArgumentOutOfRangeException("Subject title is invalid!"); } Group groupToUpdate = context.Set <Group>().Find(groupID); groupToUpdate.GroupID = groupID; groupToUpdate.GradeID = gradeID; groupToUpdate.SubjectID = subjID; groupToUpdate.TeacherID = teacherID; groupToUpdate.Description = description; context.SaveChanges(); } }
/// <summary> /// Updates a user /// </summary> /// <param name="u">New passwords supplied should be plain text!</param> public static void UpdateUser(UserInfo u) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { User userToUpdate = (User)context.Set <User>().Find(u.UserID); if (userToUpdate == null) { throw new ArgumentException("UserID is incorrect!"); } userToUpdate.FirstName = u.FirstName; userToUpdate.LastName = u.LastName; userToUpdate.Patronymic = u.Patronymic; userToUpdate.DateOfBirth = u.DoB; if (!Util.IsValidEmail(u.Email)) { throw new ArgumentException("Email string is not a valid email!"); } userToUpdate.Email = u.Email; if (userToUpdate.Password != u.Password) //new password supplied => crypt it! { userToUpdate.Password = Crypter.Blowfish.Crypt(u.Password); } userToUpdate.Phone = u.Phone; context.SaveChanges(); } }
/// <summary> /// Adds new teacher to the database. /// </summary> /// <param name="t"></param> /// <returns>ID of newly added teacher.</returns> public static int AddNewTeacher(TeacherInfo t) { int userID; if (!Util.IsValidEmail(t.Email)) { throw new ArgumentException("Email string is not a valid email!"); } using (SchoolJournalEntities context = new SchoolJournalEntities()) { using (var dbContextTransaction = context.Database.BeginTransaction()) { userID = UsersDAL.AddNewUser(t, dbContextTransaction.UnderlyingTransaction); context.Teachers.Add(new Teacher { TeacherID = userID, Category = t.Category, Specialization = t.Specialization }); context.SaveChanges(); dbContextTransaction.Commit(); } } return(userID); }
public static int NewGroup(int teacherID, string subject, string gradeSection, string description) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { //get gradeID using gradeSection int gradeID = (from grade in context.Grades where grade.GradeNo + grade.Section == gradeSection select grade.GradeID).FirstOrDefault(); if (gradeID == 0) { throw new ArgumentOutOfRangeException("Grade name is invalid!"); } //get subjectID int subjID = (from subj in context.Subjects where subj.Title == subject select subj.SubjectID).FirstOrDefault(); if (subjID == 0) { throw new ArgumentOutOfRangeException("Subject title is invalid!"); } Group newGroup = new Group { TeacherID = teacherID, SubjectID = subjID, GradeID = gradeID, Description = description }; context.Groups.Add(newGroup); context.SaveChanges(); return(newGroup.GroupID); } }
public static void PlaceMark(int lessonID, int studentID, decimal? markValue, bool isPresent = true) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { try { //context.placeMark(lessonID, studentID, markValue, isPresent); int markID = -1; if (markValue.HasValue) //check for correct value { var value = from mark in context.Marks where mark.Value == markValue select mark; if (value.Count() == 0) throw new ArgumentOutOfRangeException(string.Format("Mark value '{0}' is incorrect!", markValue)); markID = value.First().MarkID; } var less_student = from record in context.lesson_student where record.LessonID == lessonID && record.StudentID == studentID select record; if (markID != -1) less_student.First().MarkID = markID; less_student.First().IsPresent = isPresent; context.SaveChanges(); } catch (Exception ex) { throw new Exception("PlaceMark failed!"+ex.Message+"\n"+ex.InnerException.Message); } } }
public static int NewGroup(int teacherID, string subject, string gradeSection, string description) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { //get gradeID using gradeSection int gradeID = (from grade in context.Grades where grade.GradeNo + grade.Section == gradeSection select grade.GradeID).FirstOrDefault(); if (gradeID == 0) throw new ArgumentOutOfRangeException("Grade name is invalid!"); //get subjectID int subjID = (from subj in context.Subjects where subj.Title == subject select subj.SubjectID).FirstOrDefault(); if (subjID == 0) throw new ArgumentOutOfRangeException("Subject title is invalid!"); Group newGroup = new Group { TeacherID = teacherID, SubjectID = subjID, GradeID = gradeID, Description = description }; context.Groups.Add(newGroup); context.SaveChanges(); return newGroup.GroupID; } }
public static void AddStudentToGroup(int groupID, int studentID) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { context.Database.ExecuteSqlCommand( string.Format("INSERT INTO student_group(group_id,student_id) VALUES({0},{1})", groupID, studentID)); context.SaveChanges(); } }
public static void RemoveStudentFromGroup(int groupID, int studentID) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { context.Database.ExecuteSqlCommand("DELETE FROM student_group WHERE group_id=@group_id AND student_id=@student_id", new SqlParameter("@group_id", groupID), new SqlParameter("@student_id", studentID)); context.SaveChanges(); } }
public static void AddStudentToGroup(int groupID, int studentID) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { context.Database.ExecuteSqlCommand( string.Format("INSERT INTO student_group(group_id,student_id) VALUES({0},{1})",groupID,studentID)); context.SaveChanges(); } }
public static void RemoveParent(int studentID, int parentID) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { context.Database.ExecuteSqlCommand("DELETE FROM parent_student WHERE student_id=@studentID AND parent_id=@parentID", new SqlParameter("@studentID", studentID), new SqlParameter("@parentID", parentID)); context.SaveChanges(); } }
public static void AddParent(int studentID, int parentID) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { context.Database.ExecuteSqlCommand("INSERT INTO parent_student VALUES (@student_id, @parent_id)", new SqlParameter("@student_id", studentID), new SqlParameter("@parent_id", parentID)); context.SaveChanges(); } }
public static void DeleteParent(int parentID) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { Parent parentToDelete = context.Set <Parent>().Find(parentID); context.Parents.Remove(parentToDelete); context.SaveChanges(); } }
public static void DeleteParent(int parentID) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { Parent parentToDelete = context.Set<Parent>().Find(parentID); context.Parents.Remove(parentToDelete); context.SaveChanges(); } }
public static void UpdateGroup(int groupID, string description) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { Group groupToUpdate = context.Set <Group>().Find(groupID); groupToUpdate.GroupID = groupID; groupToUpdate.Description = description; context.SaveChanges(); } }
public static int AddNewParent(Parent newParent) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { if (!Util.IsValidEmail(newParent.user.Email)) throw new ArgumentException("Email string is not a valid email!"); context.Parents.Add(newParent); context.SaveChanges(); return newParent.ParentID; } }
public static int AddNewParent(Parent newParent) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { if (!Util.IsValidEmail(newParent.user.Email)) { throw new ArgumentException("Email string is not a valid email!"); } context.Parents.Add(newParent); context.SaveChanges(); return(newParent.ParentID); } }
public static void UpdateTeacher(TeacherInfo t) { if (!Util.IsValidEmail(t.Email)) throw new ArgumentException("Email string is not a valid email!"); UsersDAL.UpdateUser(t); using (SchoolJournalEntities context = new SchoolJournalEntities()) { Teacher teacherToUpdate = context.Set<Teacher>().Find(t.TeacherID); teacherToUpdate.Specialization = t.Specialization; teacherToUpdate.Category = t.Category; context.SaveChanges(); } }
public static ILessonInfo StartNewLesson(string subj_title, string theme, int numberOfHours, string gradeName, string homeTask, int teacherID) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { ObjectParameter output = new ObjectParameter("addedLesson_id", typeof(int)); context.startNewLesson(subj_title, theme, numberOfHours, gradeName, homeTask, teacherID, output); context.SaveChanges(); int lessonID = (int)output.Value; var lsn = (from lesson in context.Lessons where lesson.LessonID == lessonID select lesson).FirstOrDefault(); return(new LessonInfo(lsn)); } }
public static void DeleteUser(int userID) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { User userToDelete = (User)context.Set<User>().Find(userID); if (userToDelete != null) { context.Users.Remove(userToDelete); context.SaveChanges(); } else throw new ArgumentOutOfRangeException("Invalid userID!"); } }
public static void UpdateTeacher(TeacherInfo t) { if (!Util.IsValidEmail(t.Email)) { throw new ArgumentException("Email string is not a valid email!"); } UsersDAL.UpdateUser(t); using (SchoolJournalEntities context = new SchoolJournalEntities()) { Teacher teacherToUpdate = context.Set <Teacher>().Find(t.TeacherID); teacherToUpdate.Specialization = t.Specialization; teacherToUpdate.Category = t.Category; context.SaveChanges(); } }
public static void DeleteUser(int userID) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { User userToDelete = (User)context.Set <User>().Find(userID); if (userToDelete != null) { context.Users.Remove(userToDelete); context.SaveChanges(); } else { throw new ArgumentOutOfRangeException("Invalid userID!"); } } }
//returns new UserID public static int AddNewUser(UserInfo u, System.Data.Common.DbTransaction transaction = null) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { if (transaction != null) { context.Database.UseTransaction(transaction); } User newUser = u.GetUser(); if (!Util.IsValidEmail(u.Email)) throw new ArgumentException("Email string is not a valid email!"); newUser.Password = Crypter.Blowfish.Crypt(newUser.Password); //newUser entity contains uncrypted password! context.Users.Add(newUser); context.SaveChanges(); return newUser.UserID; } }
public static void ShiftAllStudentsToNextGrade() { using (SchoolJournalEntities context = new SchoolJournalEntities()) { using (var dbContextTransaction = context.Database.BeginTransaction()) { try { foreach (Student s in context.Students) { //if the current grade is the last one, the student is deleted! int nextGradeID = GetNextGradeID(s.GradeID); if (nextGradeID == -1) //delete student from school { UsersDAL.DeleteUser(s.StudentID); } else { s.GradeID = nextGradeID; } } foreach (Group g in context.Groups) { int nextGradeID = GetNextGradeID(g.GradeID); if (nextGradeID == -1) //delete student from school { context.Groups.Remove(g); } else { g.GradeID = nextGradeID; } } context.SaveChanges(); dbContextTransaction.Commit(); } catch (Exception ex) { dbContextTransaction.Rollback(); throw ex; } } } }
/// <summary> /// Changes user password /// </summary> /// <param name="UserID">UserID</param> /// <param name="oldPass">old plain text password</param> /// <param name="newPass">new plain text password</param> public static void ChangeUserPassword(int UserID, string oldPass, string newPass) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { if (newPass.Length > 0) { User userToUpdate = (User)context.Set<User>().Find(UserID); //if (userToUpdate.Password.Equals(oldPass)) if(Crypter.CheckPassword(oldPass, userToUpdate.Password)) { //userToUpdate.Password = newPass; userToUpdate.Password = Crypter.Blowfish.Crypt(newPass); context.SaveChanges(); } else throw new ArgumentException("Old password is incorrect!"); } else throw new ArgumentOutOfRangeException("New password is empty!"); } }
public static int AddNewUser(UserInfo u, System.Data.Common.DbTransaction transaction = null) //returns new UserID { using (SchoolJournalEntities context = new SchoolJournalEntities()) { if (transaction != null) { context.Database.UseTransaction(transaction); } User newUser = u.GetUser(); if (!Util.IsValidEmail(u.Email)) { throw new ArgumentException("Email string is not a valid email!"); } newUser.Password = Crypter.Blowfish.Crypt(newUser.Password); //newUser entity contains uncrypted password! context.Users.Add(newUser); context.SaveChanges(); return(newUser.UserID); } }
public static int AddNewStudent(string firstName, string lastName, string patronymic, DateTime?dateOfBirth, string email, string password, string phone, string gradeName) { string cpassword = Crypter.Blowfish.Crypt(password); using (SchoolJournalEntities context = new SchoolJournalEntities()) { if (!Util.IsValidEmail(email)) { throw new ArgumentException("Email string is not a valid email!"); } context.addNewStudent(firstName, lastName, patronymic, dateOfBirth, email, cpassword, phone, gradeName); context.SaveChanges(); var users = (from user in context.Users where user.FirstName == firstName && user.LastName == lastName && user.Patronymic == patronymic && user.DateOfBirth == dateOfBirth && user.Email == email && user.Phone == phone select user).ToList(); foreach (User user in users) { if (user.Password == cpassword) { return(user.UserID); } } return(-1); } }
public static void PlaceMark(int lessonID, int studentID, decimal?markValue, bool isPresent = true) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { try { //context.placeMark(lessonID, studentID, markValue, isPresent); int markID = -1; if (markValue.HasValue) //check for correct value { var value = from mark in context.Marks where mark.Value == markValue select mark; if (value.Count() == 0) { throw new ArgumentOutOfRangeException(string.Format("Mark value '{0}' is incorrect!", markValue)); } markID = value.First().MarkID; } var less_student = from record in context.lesson_student where record.LessonID == lessonID && record.StudentID == studentID select record; if (markID != -1) { less_student.First().MarkID = markID; } less_student.First().IsPresent = isPresent; context.SaveChanges(); } catch (Exception ex) { throw new Exception("PlaceMark failed!" + ex.Message + "\n" + ex.InnerException.Message); } } }
/// <summary> /// Adds new teacher to the database. /// </summary> /// <param name="t"></param> /// <returns>ID of newly added teacher.</returns> public static int AddNewTeacher(TeacherInfo t) { int userID; if (!Util.IsValidEmail(t.Email)) throw new ArgumentException("Email string is not a valid email!"); using (SchoolJournalEntities context = new SchoolJournalEntities()) { using (var dbContextTransaction = context.Database.BeginTransaction()) { userID = UsersDAL.AddNewUser(t, dbContextTransaction.UnderlyingTransaction); context.Teachers.Add(new Teacher { TeacherID = userID, Category = t.Category, Specialization = t.Specialization }); context.SaveChanges(); dbContextTransaction.Commit(); } } return userID; }
public static void UpdateGroup(int groupID, int teacherID, string subject, string gradeSection, string description) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { //get gradeID using gradeSection int gradeID = (from grade in context.Grades where grade.GradeNo + grade.Section == gradeSection select grade.GradeID).FirstOrDefault(); if (gradeID == 0) throw new ArgumentOutOfRangeException("Grade name is invalid!"); //get subjectID int subjID = (from subj in context.Subjects where subj.Title == subject select subj.SubjectID).FirstOrDefault(); if (subjID == 0) throw new ArgumentOutOfRangeException("Subject title is invalid!"); Group groupToUpdate = context.Set<Group>().Find(groupID); groupToUpdate.GroupID = groupID; groupToUpdate.GradeID = gradeID; groupToUpdate.SubjectID = subjID; groupToUpdate.TeacherID = teacherID; groupToUpdate.Description = description; context.SaveChanges(); } }
public static void UpdateGroup(int groupID, string description) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { Group groupToUpdate = context.Set<Group>().Find(groupID); groupToUpdate.GroupID = groupID; groupToUpdate.Description = description; context.SaveChanges(); } }
public static ILessonInfo StartNewLesson(string subj_title, string theme, int numberOfHours, string gradeName, string homeTask, int teacherID) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { ObjectParameter output = new ObjectParameter("addedLesson_id", typeof(int)); context.startNewLesson(subj_title, theme, numberOfHours, gradeName, homeTask, teacherID, output); context.SaveChanges(); int lessonID = (int)output.Value; var lsn = (from lesson in context.Lessons where lesson.LessonID==lessonID select lesson).FirstOrDefault(); return new LessonInfo(lsn); } }
public static void UpdateUserInfo(UserInfo userInfo) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { User userToUpdate = (User)context.Set<User>().Find(userInfo.UserID); userToUpdate.UserID = userInfo.UserID; userToUpdate.LastName = userInfo.LastName; userToUpdate.FirstName = userInfo.FirstName; userToUpdate.Patronymic = userInfo.Patronymic; if (userToUpdate.Password == userInfo.Password) //password supplied is crypted already { //do nothing - passwords already the same } else { userToUpdate.Password = Crypter.Blowfish.Crypt(userInfo.Password); } if (!Util.IsValidEmail(userInfo.Email)) throw new ArgumentException("Email string is not a valid email!"); userToUpdate.Email = userInfo.Email; userToUpdate.Phone = userInfo.Phone; userToUpdate.DateOfBirth = userInfo.DoB; context.SaveChanges(); } }
public static void UpdateStudent(StudentInfo si) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { Student s = context.Set<Student>().Find(si.StudentID); if (s == null) throw new ArgumentNullException("StudentID to update cannot be null!"); if (!Util.IsValidEmail(si.Email)) throw new ArgumentException("Email string is not a valid email!"); UsersDAL.UpdateUser(si); s.DateOfJoin = si.DateOfJoin; //find grade ID var gID = (from grade in context.Grades where (grade.GradeNo+grade.Section).Equals(si.GradeName) select grade.GradeID); if (gID.Count() > 0) { s.GradeID = gID.First(); } else throw new ArgumentException("Invalid grade name!"); context.SaveChanges(); } }
public static int AddNewStudent(string firstName, string lastName, string patronymic, DateTime? dateOfBirth, string email, string password, string phone, string gradeName) { string cpassword = Crypter.Blowfish.Crypt(password); using (SchoolJournalEntities context = new SchoolJournalEntities()) { if (!Util.IsValidEmail(email)) throw new ArgumentException("Email string is not a valid email!"); context.addNewStudent(firstName, lastName, patronymic, dateOfBirth, email, cpassword, phone, gradeName); context.SaveChanges(); var users = (from user in context.Users where user.FirstName == firstName && user.LastName == lastName && user.Patronymic == patronymic && user.DateOfBirth == dateOfBirth && user.Email == email && user.Phone == phone select user).ToList(); foreach (User user in users) { if (user.Password==cpassword) return user.UserID; } return -1; } }
/// <summary> /// Updates a user /// </summary> /// <param name="u">New passwords supplied should be plain text!</param> public static void UpdateUser(UserInfo u) { using (SchoolJournalEntities context = new SchoolJournalEntities()) { User userToUpdate = (User)context.Set<User>().Find(u.UserID); if(userToUpdate==null) throw new ArgumentException("UserID is incorrect!"); userToUpdate.FirstName = u.FirstName; userToUpdate.LastName = u.LastName; userToUpdate.Patronymic = u.Patronymic; userToUpdate.DateOfBirth = u.DoB; if (!Util.IsValidEmail(u.Email)) throw new ArgumentException("Email string is not a valid email!"); userToUpdate.Email = u.Email; if (userToUpdate.Password != u.Password) //new password supplied => crypt it! { userToUpdate.Password = Crypter.Blowfish.Crypt(u.Password); } userToUpdate.Phone = u.Phone; context.SaveChanges(); } }