public string SaveTeacerCourse(TeacherCourse aTeacherCourse, float credit)
 {
     aTeacherGateway = new TeacherGateway();
     if (!CheckTeacherTime(aTeacherCourse))
        if (!DoesThisCourseExist(aTeacherCourse))
      return aTeacherGateway.SaveTeacerCourse(aTeacherCourse, credit);
         else
            return "This Course Already Assigned";
     else
         return "This Time Teacher Handel Another Course";
 }
        protected void assignButton_Click(object sender, EventArgs e)
        {
            try
            {
                TeacherCourse aTeacherCourse = new TeacherCourse();
                TeacherManager aTeacherManager = new TeacherManager();
                aTeacherCourse.CourseId = Convert.ToInt16(courseTitleDropDownList.Text);
                aTeacherCourse.DepartmentId = Convert.ToInt16(departmentDropDownList.Text);
                aTeacherCourse.TeacherId = teacherDropDownList.Text;
                aTeacherCourse.AssignDate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
                aTeacherCourse.Status = 0;
                float credit = float.Parse(creditTextBox.Value);
                string msg = aTeacherManager.SaveTeacerCourse(aTeacherCourse, credit);
                if (msg == "Saved")
                {
                    msgLabel.ForeColor = Color.Green;
                    msgLabel.Text = msg;
                    GetTeacherAssignCredit();
                }
                else
                {
                    msgLabel.ForeColor = Color.Red;
                    msgLabel.Text = msg;
                }

            }
            catch (SqlException sqlException)
            {
                msgLabel.ForeColor = Color.Red;
                msgLabel.Text = "Database error.See details error: " + sqlException.Message;

            }
            catch (Exception exception)
            {
                msgLabel.ForeColor = Color.Red;
                string errorMessage = "Unknow error occured.";
                errorMessage += exception.Message;
                if (exception.InnerException != null)
                {
                    errorMessage += exception.InnerException.Message;
                }
                msgLabel.Text = errorMessage;
            }
        }
 private bool CheckTeacherTime(TeacherCourse aTeacherCourse)
 {
     aTeacherGateway = new TeacherGateway();
     ScheduleGateway aScheduleGateway = new ScheduleGateway();
     List<int> coursesId = new List<int>();
     coursesId = aTeacherGateway.GetTeacherAssignCoursesId(aTeacherCourse);
     List<Schedule> schedules = new List<Schedule>();
     schedules = aScheduleGateway.GetTeacherAllSchedules(coursesId);
     List<Schedule> currentScheduleList = new List<Schedule>();
     currentScheduleList = aScheduleGateway.GetCurrentCourseSchedules(aTeacherCourse);
     foreach (Schedule schedule in currentScheduleList)
     {
         foreach (Schedule aSchedule in schedules)
         {
             if (schedule.DayId == aSchedule.DayId && ((schedule.StartTime <= aSchedule.StartTime && schedule.EndingTime >= aSchedule.StartTime) || (schedule.StartTime <= aSchedule.EndingTime & schedule.EndingTime >= aSchedule.EndingTime)))
             {
                 return true;
             }
         }
     }
     return false;
 }
        private bool DoesThisCourseExist(TeacherCourse aTeacherCourse)
        {
            int status = 0;
            List<Course> courses = new List<Course>();
            CourseManager aCourseManager = new CourseManager();
            courses = aCourseManager.GetAllCourses();
            foreach (Course course in courses)
            {
                if (aTeacherCourse.CourseId == course.CourseId && course.CourseStatus == 1)
                {
                    status = 1;
                }
            }

            if (status == 1)
                return true;
            else
            {
                return false;
            }
        }
        public List<Schedule> GetCurrentCourseSchedules(TeacherCourse aTeacherCourse)
        {
            try
            {
                int scheduleStatus = 0;
                List<Schedule> schedules = new List<Schedule>();
                connection.Open();
                string query = "SELECT dayId,startTime,endingTime FROM t_ScheduleClass WHERE CourseId=@courseId and ScheduleStatus=@status";
                command.CommandText = query;
                command.Parameters.Clear();
                command.Parameters.AddWithValue("@courseId", aTeacherCourse.CourseId);
                command.Parameters.AddWithValue("@status", scheduleStatus);
                SqlDataReader teacherCourseScheduleReader = command.ExecuteReader();
                while (teacherCourseScheduleReader.Read())
                {
                    Schedule aSchedule = new Schedule();
                    aSchedule.DayId = Convert.ToInt16(teacherCourseScheduleReader[0].ToString());
                    aSchedule.StartTime = float.Parse(teacherCourseScheduleReader[1].ToString());
                    aSchedule.EndingTime = float.Parse(teacherCourseScheduleReader[2].ToString());
                    schedules.Add(aSchedule);
                }

                return schedules;
            }
            finally
            {
                connection.Close();
            }
        }
        private void UpdateTeacherCredit(TeacherCourse aTeacherCourse, float credit)
        {
            try
            {
                Teacher aTeacher = new Teacher();
                aTeacher.TeacherId = aTeacherCourse.TeacherId;
                aTeacher.ADepartment = new Department();
                aTeacher.ADepartment.DepartmentId = aTeacherCourse.DepartmentId;
                aTeacher = GetTeacherCreditInfo(aTeacher);
                aTeacher.AssignCredit = Convert.ToDouble(aTeacher.AssignCredit) + credit;
                aTeacher.RemaningCredit = Convert.ToDouble(aTeacher.RemaningCredit) - credit;
                connection.Open();
                string query = "UPDATE t_TeacherCredit SET AssignCredit =@assignCredit,RemainingCredit=@remainingCredit Where teacherId =@teacherId";
                command.CommandText = query;
                command.Parameters.Clear();
                command.Parameters.AddWithValue("@assignCredit", aTeacher.AssignCredit);
                command.Parameters.AddWithValue("@remainingCredit", aTeacher.RemaningCredit);
                command.Parameters.AddWithValue("@teacherId", aTeacherCourse.TeacherId);
                command.ExecuteNonQuery();
            }

            finally
            {
                connection.Close();

            }
        }
 private void UpdateCourseStatus(TeacherCourse aTeacherCourse)
 {
     try
     {
         connection.Open();
         int courseStatus = 1;
         string query = "UPDATE t_Course SET CourseStatus =@status Where CourseId =@courseId";
         command.CommandText = query;
         command.Parameters.Clear();
         command.Parameters.AddWithValue("@status", courseStatus);
         command.Parameters.AddWithValue("@courseId", aTeacherCourse.CourseId);
         command.ExecuteNonQuery();
     }
     finally
     {
         connection.Close();
     }
 }
        public string SaveTeacerCourse(TeacherCourse aTeacherCourse, float credit)
        {
            try
            {
                connection.Open();
                string query = "INSERT INTO t_CourseAssignToTeacher VALUES(@deptId,@teacherId,@courseId,@assignDate,@status)";
                command.CommandText = query;
                command.Parameters.Clear();
                command.Parameters.AddWithValue("@deptId", aTeacherCourse.DepartmentId);
                command.Parameters.AddWithValue("@teacherId", aTeacherCourse.TeacherId);
                command.Parameters.AddWithValue("@courseId", aTeacherCourse.CourseId);
                command.Parameters.AddWithValue("@assignDate", aTeacherCourse.AssignDate);
                command.Parameters.AddWithValue("@status", aTeacherCourse.Status);
                command.ExecuteNonQuery();
                return "Saved";
            }
            finally
            {
                connection.Close();
                UpdateCourseStatus(aTeacherCourse);
                UpdateTeacherCredit(aTeacherCourse, credit);

            }
        }
        public List<int> GetTeacherAssignCoursesId(TeacherCourse aTeacherCourse)
        {
            try
            {
                connection.Open();
                List<int> coursesId = new List<int>();
                string query = "SELECT courseId from t_CourseAssignToTeacher where teacherId=@teacherId";
                command.CommandText = query;
                command.Parameters.Clear();
                command.Parameters.AddWithValue("@teacherId", aTeacherCourse.TeacherId);
                SqlDataReader courseIdDataReader = command.ExecuteReader();
                while (courseIdDataReader.Read())
                {
                    coursesId.Add(Convert.ToInt16(courseIdDataReader[0]));

                }
                return coursesId;

            }
            finally
            {
                connection.Close();
            }
        }