public ActionResult AddSubject(int id, int[] subjectID, string Choose)
        {
            UnitOfWork unitOfWork = new UnitOfWork();
            CourseRepository courseRepo = new CourseRepository();
            CourseSubject cs = new CourseSubject();
            CourseSubjectRepository csRepo = new CourseSubjectRepository(unitOfWork);
            UserRepository<Teacher> teacherRepo = new UserRepository<Teacher>(unitOfWork);
            Teacher teacher = teacherRepo.GetAll(filter: t => t.ID == id).FirstOrDefault();

            int courseId = Convert.ToInt32(Choose);

            var curSubj = csRepo.GetAll(filter: x => x.CourseID == courseId && x.Teachers.Any(t => t.ID == id));

            try
            {
                foreach (var item in curSubj)
                {
                    teacher.CourseSubject.Remove(item);
                }
                if (subjectID != null)
                {
                    foreach (var item in subjectID)
                    {
                        cs = csRepo.GetAll(filter: c => c.SubjectID == item && c.CourseID == courseId).FirstOrDefault();
                        teacher.CourseSubject.Add(cs);
                    }
                }
                teacherRepo.Save(teacher);
                unitOfWork.Commit();
            }
            catch (Exception)
            {
                unitOfWork.RollBack();
            }

            return RedirectToAction("ListTeachers", "Teacher");
        }
        /// <summary>
        /// This method is called to add or remove Keyword from article
        /// </summary>
        public void AddRemoveKeywords(string keywords, int infoID)
        {
            UnitOfWork unitOfWork = new UnitOfWork();
            KeywordsRepository keywordsRepository = new KeywordsRepository(unitOfWork);
            InfoRepository infoRepository = new InfoRepository(unitOfWork);

            Info info = infoRepository.GetByID(infoID);

            List<int> KeywordsIDList = new List<int>();
            if (keywords != null)
            {
                string[] keywordsArray = keywords.Split(',');

                foreach (var keywordInput in keywordsArray)
                {
                    string value = keywordInput.Trim();
                    value = Regex.Replace(value, @"\s+", " ");

                    Keyword keyword = keywordsRepository.GetAll(filter: x => x.Value == value).FirstOrDefault();

                    if (keyword == null)
                    {
                        if (value != string.Empty)
                        {
                            keyword = new Keyword();
                            keyword.Value = value;
                            keywordsRepository.Save(keyword);

                            KeywordsIDList.Add(keyword.ID);
                        }
                    }
                    else
                    {
                        KeywordsIDList.Add(keyword.ID);
                    }
                }
            }

            List<int> currentArticleKeywordsID = new List<int>();
            info.Keywords = info.Keywords == null ? info.Keywords = new List<Keyword>() : info.Keywords;

            try
            {
                foreach (var item in info.Keywords)
                {
                    currentArticleKeywordsID.Add(item.ID);
                }

                IEnumerable<int> removedKeywordsID = currentArticleKeywordsID.Except(KeywordsIDList);

                foreach (Keyword keyword in info.Keywords.ToList().Where(x => removedKeywordsID.Any(k => k == x.ID)))
                {
                    keyword.Info.Remove(info);
                    keywordsRepository.Save(keyword);
                }

                IEnumerable<int> addKeywordToArticle = KeywordsIDList.Except(currentArticleKeywordsID);

                foreach (var keyID in addKeywordToArticle)
                {
                    Keyword keyword = keywordsRepository.GetByID(keyID);
                    keyword.Info = keyword.Info == null ? new List<Info>() : keyword.Info;
                    keyword.Info.Add(info);

                    keywordsRepository.Save(keyword);
                }
                unitOfWork.Commit();
            }
            catch (Exception ex)
            {
                unitOfWork.RollBack();
                throw ex;
            }
        }
        public ActionResult EditInfo(InfoEditInfoVM model)
        {
            UnitOfWork unitOfWork = new UnitOfWork();
            InfoRepository infoRepository = new InfoRepository(unitOfWork);
            Info info = new Info();

            if (infoRepository.GetAll(filter: x => x.Title == model.Title && x.ID != model.ID).FirstOrDefault() != null)
            {
                ModelState.AddModelError("Title", "Article with same name exist");
            }

            model.Title = model.Title.Trim();

            string message = "Successfully created ";

            if (model.ID > 0)
            {
                info = infoRepository.GetByID(model.ID);
                message = "Successfully edited ";
                if (info.Title != model.Title)
                {
                    string filesLocalPath = "~/Content/Files/" + model.Title;
                    if (Directory.Exists(HostingEnvironment.MapPath(filesLocalPath + info.Title)))
                    {
                        Directory.Move(HostingEnvironment.MapPath(filesLocalPath + info.Title),
                                       HostingEnvironment.MapPath(filesLocalPath + model.Title));
                    }
                }
            }

            model.Content = model.Content.Trim();

            info.Content = model.Content;
            info.Title = model.Title;

            infoRepository.Save(info);

            InfoImageRepository infoImageRepository = new InfoImageRepository(unitOfWork);

            HttpFileCollection UploadedFiles = System.Web.HttpContext.Current.Request.Files;

            string filePath = "~/Content/Files/" + model.Title + "/";

            Regex imageRegex = new Regex(ConfigurationManager.AppSettings["ImageRestriction"]);
            Regex fileRegex = new Regex(ConfigurationManager.AppSettings["FileRestriction"]);

            for (int i = 0; i < UploadedFiles.Count; i++)
            {
                if (imageRegex.Match(UploadedFiles[i].FileName).Success)
                {
                    InfoImage infoImage = new InfoImage();

                    infoImage.Name = UploadedFiles[i].FileName;

                    if (infoImageRepository.GetAll(filter: x => x.Name == infoImage.Name && x.InfoID == info.ID).FirstOrDefault() == null)
                    {
                        info.Images = info.Images == null ? info.Images = new List<InfoImage>() : info.Images;
                        info.Images.Add(infoImage);

                        //Create  folder
                        if (!Directory.Exists(HostingEnvironment.MapPath(filePath)))
                            Directory.CreateDirectory(HostingEnvironment.MapPath(filePath));

                        UploadedFiles[i].SaveAs(HostingEnvironment.MapPath(filePath) + UploadedFiles[i].FileName);
                    }
                }
                else if (fileRegex.Match(UploadedFiles[i].FileName).Success)
                {
                    var file = UploadedFiles[i];

                    //Create  folder
                    if (!Directory.Exists(HostingEnvironment.MapPath(filePath)))
                        Directory.CreateDirectory(HostingEnvironment.MapPath(filePath));

                    //If other file is uploaded we delete the old
                    if (info.FileName != null && System.IO.File.Exists(HostingEnvironment.MapPath(filePath + info.FileName)) && file.FileName != info.FileName)
                        System.IO.File.Delete(HostingEnvironment.MapPath(filePath + info.FileName));

                    info.FileName = file.FileName;

                    file.SaveAs(HostingEnvironment.MapPath(filePath) + file.FileName);
                }
            }

            infoRepository.Save(info);
            unitOfWork.Commit();

            AddRemoveKeywords(model.KeywordsInput, info.ID);

            return RedirectToAction("InfoPage", "Info");
        }
        public ActionResult CreateStudent(StudentCreateStudentVM model, int? id)
        {
            Student student = new Student();
            Course course = new Course();
            UnitOfWork unitOfWork = new UnitOfWork();
            UserRepository<Student> sRepo = new UserRepository<Student>();
            CourseRepository courseRepo = new CourseRepository();

            if (!ModelState.IsValid)
            {
                List<SelectListItem> list = new List<SelectListItem>();
                if (id != null)
                {
                    var courses = courseRepo.GetAll(filter: c => c.ID == id.Value).FirstOrDefault();
                    list.Add(new SelectListItem() { Text = courses.Name + " || " + courses.FKNumber, Value = courses.ID.ToString() });
                }
                else
                {
                    var courses = courseRepo.GetAll();

                    foreach (var item in courses)
                    {
                        list.Add(new SelectListItem() { Text = item.Name + " || " + item.FKNumber, Value = item.ID.ToString() });
                    }
                }
                model.CoursesList = list;

                return View(model);
            }

            int courseID = Convert.ToInt32(model.selectedValueID);
            course = courseRepo.GetAll(filter: c => c.ID == courseID).FirstOrDefault();

            var fkNumberCount = (from t in sRepo.GetAll(filter: x => x.FacultiNumber.Contains(course.FKNumber)).OrderByDescending(s => s.FacultiNumber)
                                 select t).FirstOrDefault();

            student.FirstName = model.FirstName;
            student.LastName = model.LastName;

            if (fkNumberCount == null)
            {
                student.FacultiNumber = (Convert.ToInt32(course.FKNumber) * 1000 + 1).ToString();
            }
            else
            {
                student.FacultiNumber = (Convert.ToInt32(fkNumberCount.FacultiNumber) + 1).ToString();
            }

            student.CourseID = courseID;
            student.Active = true;
            Passphrase hash = PasswordHasher.Hash("password");
            student.Hash = hash.Hash;
            student.Salt = hash.Salt;

            student.Username = "******" + student.FacultiNumber;

            sRepo.Save(student);
            if (id != null)
            {
                return RedirectToAction("CreateStudent", "Student", new { id = id });
            }
            return RedirectToAction("ListStudents", "Student");
        }
        public ActionResult CreateTeacher(TeacherCreateTeacherVM model)
        {
            UnitOfWork unitOfWork = new UnitOfWork();
            Teacher teacher = new Teacher();
            UserRepository<Teacher> teacherRepo = new UserRepository<Teacher>(unitOfWork);
            TitleRepository titleRepo = new TitleRepository(unitOfWork);

            if (!ModelState.IsValid)
            {
                model.TitleList = TeacherTitle();

                return View(model);
            }

            try
            {
                teacher.Username = model.FirstName;
                teacher.FirstName = model.FirstName;
                teacher.LastName = model.LastName;

                int tittleId = Convert.ToInt32(model.selectedValueID);

                Title title = titleRepo.GetByID(tittleId);

                teacher.Title = title;

                UniversitySystem.Hasher.Passphrase hash = UniversitySystem.Hasher.PasswordHasher.Hash("password");
                teacher.Hash = hash.Hash;
                teacher.Salt = hash.Salt;
                teacher.Active = true;

                teacherRepo.Save(teacher);

                unitOfWork.Commit();
            }

            catch (Exception)
            {
                unitOfWork.RollBack();
                throw;
            }

            return RedirectToAction("ListTeachers", "Teacher");
        }
        public ActionResult EditTeacher(TeacherEditTeacherVM model)
        {
            if (!ModelState.IsValid)
            {
                model.TitleList = TeacherTitle();
                return View(model);
            }
            UnitOfWork unitOfWork = new UnitOfWork();
            UserRepository<Teacher> teacherRepo = new UserRepository<Teacher>(unitOfWork);
            TitleRepository tiRepo = new TitleRepository(unitOfWork);

            Teacher teacher = teacherRepo.GetByID(model.TeacherID);

            try
            {
                int TitleId = Convert.ToInt32(model.selectedValueID);
                Title title = tiRepo.GetByID(TitleId);

                if (model.FirstName != teacher.FirstName || model.LastName != teacher.LastName || model.Active != teacher.Active || TitleId != teacher.Title.ID)
                {
                    teacher.FirstName = model.FirstName;
                    teacher.LastName = model.LastName;
                    teacher.Username = model.FirstName;
                    teacher.Active = model.Active;
                    teacher.Title = title;

                    teacherRepo.Save(teacher);
                    unitOfWork.Commit();
                }
            }
            catch (Exception)
            {
                unitOfWork.RollBack();
            }
            return RedirectToAction("ListTeachers", "Teacher");
        }