예제 #1
0
        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();
            }
        }
예제 #2
0
        /// <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);
        }
예제 #3
0
        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();
            }
        }
예제 #4
0
 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;
             }
         }
     }
 }