public ActionResult Index()
 {
     if (AuthenticationManager.LoggedUser == null)
     {
         return RedirectToAction("Login", "Default");
     }
     Student student = new Student();
     StudentRepository studentRepository = new StudentRepository();
     student = studentRepository.GetById(AuthenticationManager.LoggedUser.Id);
     StudentControllerStudentVM model = new StudentControllerStudentVM();
     Course course = new Course();
     CourseRepository courseRepository = new CourseRepository();
     course = courseRepository.GetAll(filter: c => c.Id == student.CourseID).FirstOrDefault();
     List<Subject> subjectList = new List<Subject>();
     CourseSubjectRepository courseSubjectRepository = new CourseSubjectRepository();
     subjectList = courseSubjectRepository.GetAll(filter: c => c.CourseID == course.Id).Select(s => s.Subject).ToList();
     List<string> subjectNames = new List<string>();
     foreach (var subject in subjectList)
     {
         subjectNames.Add(subject.Name);
     }
     model.Subjects = subjectNames;
     model.FirstName = student.FirstName;
     model.LastName = student.LastName;
     model.StudentID = AuthenticationManager.LoggedUser.Id;
     model.Course = course.Name;
     model.FaculityNumber = student.FacultyNumber;
     return View(model);
 }
 public ActionResult Index()
 {
     if (AuthenticationManager.LoggedUser == null || !AuthenticationManager.LoggedUser.GetType().BaseType.Equals(typeof(Teacher)))
     {
         return RedirectToAction("Login", "Default");
     }
     TeacherControllerTeacherVM model = new TeacherControllerTeacherVM();
     Teacher teacher = new Teacher();
     TeacherRepository teacherRepository = new TeacherRepository();
     teacher = teacherRepository.GetById(AuthenticationManager.LoggedUser.Id);
     model.FirstName = teacher.FirstName;
     model.LastName = teacher.LastName;
     CourseSubjectRepository courseSubjectRepository = new CourseSubjectRepository();
     CourseRepository courseRepository = new CourseRepository();
     SubjectRepository subjectRepository = new SubjectRepository();
     List<int> subjectList = new List<int>();
     Dictionary<string, List<Subject>> courseSubjectList = new Dictionary<string, List<Subject>>();
     List<Subject> subjects = new List<Subject>();
     List<int> courseList = new List<int>();
     foreach (var courseSubject in teacher.CourseSubject)
     {
         courseList.Add(courseSubject.Course.Id);
         subjectList.Add(courseSubject.Subject.Id);
     }
     subjectList = subjectList.Distinct().ToList();
     courseList = courseList.Distinct().ToList();
     Course course = new Course();
     foreach (var courseID in courseList)
     {
         course = courseRepository.GetById(courseID);
         subjects = courseSubjectRepository.GetAll(filter: c => c.Course.Id == courseID && subjectList.Contains(c.Subject.Id)).Select(s => s.Subject).ToList();
         courseSubjectList.Add(course.Name, subjects);
     }
     model.CourseSubjectList = courseSubjectList;
     return View(model);
 }
 public ActionResult EditCourses(int id, AdminControllerCourseVM courseModel)
 {
     TryUpdateModel(courseModel);
     if (ModelState.IsValid)
     {
         Course course = null;
         CourseRepository coureseRepository = new CourseRepository();
         if (id > 0)
         {
             course = coureseRepository.GetById(id);
             course.Name = courseModel.Name;
             course.Code = courseModel.Code;
             coureseRepository.Save(course);
             return RedirectToAction("ManageCourses");
         }
         else
         {
             course = coureseRepository.GetAll(filter: c => c.Name == courseModel.Name).FirstOrDefault();
             if (course == null)
             {
                 course = new Course();
                 course.Name = courseModel.Name;
                 course.Code = courseModel.Code;
                 coureseRepository.Save(course);
                 return RedirectToAction("ManageCourses");
             }
             else
             {
                 throw new ArgumentException("Invalid course name");
             }
         }
     }
     return View(courseModel);
 }
        public ActionResult EditCourses(int id)
        {
            Course course = new Course();
            CourseRepository courseRepository = new CourseRepository();
            AdminControllerCourseVM courseModel = new AdminControllerCourseVM();

            if (id > 0)
            {
                course = courseRepository.GetById(id);
                courseModel.Name = course.Name;
                courseModel.Code = course.Code;
                courseModel.CourseID = id;
            }

            if (id == 0)
            {
                courseModel.Code = GenerateCode();
                course.Name = courseModel.Name;
                course.Code = courseModel.Code;
            }
            return View(courseModel);
        }
 public JsonResult DeleteCourses(int id)
 {
     bool courseInUse = false;
     List<Student> studentList = new List<Student>();
     StudentRepository studentRepository = new StudentRepository();
     studentList = studentRepository.GetAll(filter: s => s.Course.Id == id);
     if (studentList.Count > 0)
     {
         courseInUse = true;
     }
     else
     {
         Course course = new Course();
         CourseRepository courseRepository = new CourseRepository();
         course = courseRepository.GetById(id);
         courseRepository.Delete(course);
     }
     return Json(courseInUse, JsonRequestBehavior.AllowGet);
 }
        public JsonResult CheckForExistingName(string name, string type, int id)
        {
            bool exist = false;
            switch (type)
            {
                case "Admin":
                    Administrator admin = new Administrator();
                    AdministratorRepository adminRepository = new AdministratorRepository();
                    admin = adminRepository.GetAll(filter: a => a.UserName == name && a.Id != id).FirstOrDefault();
                    if (admin != null)
                    {
                        exist = true;
                    };
                    break;
                case "Student":
                    Student student = new Student();
                    StudentRepository studentRepository = new StudentRepository();
                    student = studentRepository.GetAll(filter: s => s.UserName == name && s.Id != id).FirstOrDefault();
                    if (student != null)
                    {
                        exist = true;
                    };
                    break;
                case "Teacher":
                    Teacher teacher = new Teacher();
                    TeacherRepository teacherRepository = new TeacherRepository();
                    teacher = teacherRepository.GetAll(filter: t => t.UserName == name && t.Id != id).FirstOrDefault();
                    if (teacher != null)
                    {
                        exist = true;
                    };
                    break;
                case "Course":
                    Course course = new Course();
                    CourseRepository courseRepository = new CourseRepository();
                    course = courseRepository.GetAll(filter: c => c.Name == name).FirstOrDefault();
                    if (course != null)
                    {
                        exist = true;
                    };
                    break;
                case "Title":
                    Title title = new Title();
                    TitleRepository titleRepository = new TitleRepository();
                    title = titleRepository.GetAll(filter: t => t.Name == name).FirstOrDefault();
                    if (title != null)
                    {
                        exist = true;
                    };
                    break;
                case "Subject":
                    Subject subject = new Subject();
                    SubjectRepository subjectRepository = new SubjectRepository();
                    subject = subjectRepository.GetAll(filter: s => s.Name == name).FirstOrDefault();
                    if (subject != null)
                    {
                        exist = true;
                    };
                    break;
            }

            return Json(exist, JsonRequestBehavior.AllowGet);
        }
        private int GenerateCode()
        {
            Random rand = new Random();
            int code = 0;
            CourseRepository courseRepo = new CourseRepository();
            Course course = new Course();
            code = rand.Next(0, 99);
            course = courseRepo.GetAll(filter: c => c.Code == code).FirstOrDefault();

            while (course != null)
            {
                code = rand.Next(0, 99);
                course = courseRepo.GetAll(filter: c => c.Code == code).FirstOrDefault();
            }
            return code;
        }