public JsonResult DeleteGrade(int gradeId)
 {
     Grade grade = new Grade();
     UnitOfWork unitOfWork = new UnitOfWork();
     GradeRepository gradeRepository = new GradeRepository();
     grade = gradeRepository.GetById(gradeId);
     gradeRepository.Delete(grade);
     unitOfWork.Commit();
     return Json("successfully deleted", JsonRequestBehavior.AllowGet);
 }
 public JsonResult EditGrade(int gradeId, double gradeValue, int subjectId, int studentId)
 {
     Grade grade = new Grade();
     GradeRepository gradeRepo = new GradeRepository();
     SelectListItem gradeItem = null;
     if (gradeId != 0)
     {
         grade = gradeRepo.GetById(gradeId);
         gradeValue = System.Math.Round(gradeValue, 2);
         grade.GradeValue = gradeValue;
         gradeRepo.Save(grade);
     }
     else
     {
         UnitOfWork unitOfWork = new UnitOfWork();
         StudentRepository studentRepository = new StudentRepository(unitOfWork);
         GradeRepository gradeRepository = new GradeRepository(unitOfWork);
         SubjectRepository subjectRepository = new SubjectRepository(unitOfWork);
         Student student = new Student();
         student = studentRepository.GetById(studentId);
         Subject subject = new Subject();
         subject = subjectRepository.GetById(subjectId);
         grade.SubjectID = subjectId;
         grade.Subject = subject;
         grade.Student = student;
         gradeValue = System.Math.Round(gradeValue, 2);
         grade.GradeValue = gradeValue;
         gradeRepository.Save(grade);
         unitOfWork.Commit();
     }
     gradeItem = new SelectListItem() { Text = grade.GradeValue.ToString(), Value = grade.Id.ToString() };
     return Json(gradeItem, JsonRequestBehavior.AllowGet);
 }
        public ActionResult AssignToCourse(AdminControllerTeacherVM model, string[] checkedSubjects)
        {
            UnitOfWork unitOfWork = new UnitOfWork();
            TeacherRepository teacherRepository = new TeacherRepository(unitOfWork);
            CourseSubjectRepository courseSubjectRepo = new CourseSubjectRepository(unitOfWork);
            TryUpdateModel(model);
            Teacher teacher = teacherRepository.GetById(model.Id);

            CourseSubject courseSubject = null;

            try
            {
                List<CourseSubject> courseSubjectsList = courseSubjectRepo.GetAll(c => c.CourseID == model.CourseID && c.Teacher.Any(t => t.Id == model.Id));
                foreach (var item in courseSubjectsList)
                {
                    teacher.CourseSubject.Remove(item);
                }

                if (checkedSubjects != null)
                {
                    foreach (var item in checkedSubjects)
                    {
                        courseSubject = courseSubjectRepo.GetAll(filter: cs => cs.CourseID == model.CourseID && cs.SubjectID.ToString() == item).FirstOrDefault();
                        teacher.CourseSubject.Add(courseSubject);
                    }
                }
                teacherRepository.Save(teacher);
                unitOfWork.Commit();
            }
            catch (Exception)
            {
                unitOfWork.RollBack();
            }

            return RedirectToAction("ManageTeachers", "Admin");
        }
        public ActionResult EditTeachers(int id, AdminControllerTeacherVM teacherModel)
        {
            UnitOfWork uOw = new UnitOfWork();
            Teacher teacher = null;
            Title title = new Title();
            TitleRepository titleRepository = new TitleRepository(uOw);
            TeacherRepository teacherRepository = new TeacherRepository(uOw);

            TryUpdateModel(teacherModel);
            if (teacherModel.ListItems == null)
            {
                teacherModel.TitleList = titleRepository.GetAll();
                List<SelectListItem> SelectListTitle = new List<SelectListItem>();

                foreach (var item in teacherModel.TitleList)
                {
                    SelectListTitle.Add(new SelectListItem() { Text = item.Name, Value = item.Id.ToString() });
                }
                teacherModel.ListItems = SelectListTitle;
            }

            if (ModelState.IsValid)
            {
                if (id > 0)
                {
                    teacher = teacherRepository.GetById(id);
                    teacher.FirstName = teacherModel.FirstName;
                    teacher.LastName = teacherModel.LastName;
                    teacher.UserName = teacherModel.UserName;
                    teacher.Password = teacherModel.Password;
                    title = titleRepository.GetById(teacherModel.TitleID);
                    teacher.Title = title;
                    teacher.IsActive = teacherModel.isActive;
                    teacherRepository.Save(teacher);
                    uOw.Commit();
                    return RedirectToAction("ManageTeachers");
                }
                else
                {
                    teacher = teacherRepository.GetAll(filter: t => t.UserName == teacherModel.UserName).FirstOrDefault();
                    if (teacher == null)
                    {
                        teacher = new Teacher();
                        teacher.FirstName = teacherModel.FirstName;
                        teacher.LastName = teacherModel.LastName;
                        teacher.UserName = teacherModel.UserName;
                        teacher.Password = SecurityService.CreateHash(teacherModel.Password);
                        title = titleRepository.GetById(teacherModel.TitleID);
                        teacher.Title = title;
                        teacher.IsActive = teacherModel.isActive;
                        teacherRepository.Save(teacher);
                        uOw.Commit();
                        return RedirectToAction("ManageTeachers");
                    }
                    else
                    {
                        throw new ArgumentException("Invalid username !");
                    }
                }
            }
            return View(teacherModel);
        }