public IActionResult AddSubjectToTeacher(AddSubjectToTeacherViewModel input)
        {
            var subjectIdToAdd = this.db.Subjects.Where(x => x.Id == input.Id).FirstOrDefault();

            var teacherId = this.GetRouteId();

            if (subjectIdToAdd == null)
            {
                return(this.RedirectToAction("AllTeachers", "Teacher"));
            }

            using (this.db)
            {
                this.db.Set <SubjectTeachers>().Add(new SubjectTeachers
                {
                    SubjectId = subjectIdToAdd.Id,
                    Subject   = this.subjectsService.GetById(subjectIdToAdd.Id),
                    TeacherId = teacherId,
                    Teacher   = this.teachersService.GetTeacherById(teacherId),
                });

                this.db.SaveChanges();
            }

            return(this.RedirectToAction("AllTeachers", "Teacher"));
        }
        public IActionResult RemoveSubjectFromTeacher()
        {
            int teacherId = this.GetRouteId();

            var allSubjectsForTeacher = this.subjectTeachersService.GetAllSubjectsForTeacher <SubjectDropDownListViewModel>(teacherId);

            var viewModel = new AddSubjectToTeacherViewModel()
            {
                Subjects = allSubjectsForTeacher,
            };

            return(this.View(viewModel));
        }
        public IActionResult AddSubjectToTeacher()
        {
            int teacherId = this.GetRouteId();

            var subjectsTeachersForTeacher = this.subjectTeachersService.GetAllSubjectsTeachersForTeacher(teacherId);
            var subjects = this.subjectTeachersService.GetAllSubjectsNotForTeacher <SubjectDropDownListViewModel>(subjectsTeachersForTeacher);

            var input = new AddSubjectToTeacherViewModel
            {
                Subjects = subjects,
            };

            return(this.View(input));
        }
        public IActionResult RemoveSubjectFromTeacher(AddSubjectToTeacherViewModel viewModel)
        {
            int teacheId = this.GetRouteId();

            var subject = this.db.Subjects.Where(x => x.Id == viewModel.Id).FirstOrDefault();

            if (subject == null)
            {
                return(this.RedirectToAction("AllTeachers"));
            }

            var subjectTeacherToDelete = this.db.SubjectTeachers.Where(x => x.SubjectId == subject.Id && x.TeacherId == teacheId)
                                         .FirstOrDefault();

            var teacherId = this.GetRouteId();

            if (subject == null)
            {
                return(this.View("This Subject does not exist"));
            }

            var allSubjectsTeachers       = this.subjectTeachersService.GetAllSubjectsTeachersForTeacher(teacherId);
            var allCourseSubjectsTeachers = this.courseSubjectTeacherService.GetAllSubjectsTeachersForTeacher(teacheId);

            using (this.db)
            {
                foreach (var subjectTeacher in allSubjectsTeachers)
                {
                    if (subjectTeacher.SubjectId == subjectTeacherToDelete.SubjectId)
                    {
                        this.db.SubjectTeachers.Remove(subjectTeacher);
                    }
                }

                foreach (var courseSubjectTeacher in allCourseSubjectsTeachers)
                {
                    if (courseSubjectTeacher.SubjectId == subjectTeacherToDelete.SubjectId)
                    {
                        this.db.CourseSubjectTeachers.Remove(courseSubjectTeacher);
                    }
                }

                this.db.SaveChanges();

                return(this.RedirectToAction("AllTeachers"));
            }
        }