public async Task <IActionResult> Edit(int id, string[] selectedPriority, [Bind("Id, FirstName, LastName, Email, PhoneNumber, Gender, Comment")] StudentViewModel studentViewModel)
        {
            var student = await _repository.Query <Student>()
                          .Include(x => x.StudentCourses)
                          .ThenInclude(x => x.Course)
                          .ThenInclude(x => x.Programs)
                          .FirstOrDefaultAsync(x => x.Id == id);

            if (id != student.Id)
            {
                return(NotFound());
            }
            if (student.StudentDataLocked == true)
            {
                ModelState.AddModelError("StudentEdit", "Kandidato duomenys yra užrakinti, redaguoti kandidato negalima");
                TempData["ErrorMessage"] = "Kandidato duomenys yra užrakinti, redaguoti kandidato negalima";
                return(RedirectToAction("Edit", "Students", new { id = student.Id }));
            }
            if (ModelState.IsValid)
            {
                try
                {
                    StudentsMappings.ToEntity(student, studentViewModel);
                    var studentCourses = student.StudentCourses.ToList();
                    var selectedPriorityWithoutNullAndDuplictates = selectedPriority.Where(priority => !string.IsNullOrEmpty(priority)).Distinct().ToArray();
                    var listOfSelectedPriority = selectedPriorityWithoutNullAndDuplictates.ToList();

                    foreach (var studentCourse in studentCourses)
                    {
                        studentCourse.Priority = null;
                        int priority = listOfSelectedPriority.IndexOf(studentCourse.Course.Title);
                        if (priority == -1)
                        {
                            studentCourse.Priority = null;
                        }
                        else
                        {
                            studentCourse.Priority = priority + 1;
                        }
                    }
                    await _repository.SaveAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!_repository.Exists <Student>(student.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                var previousUrl = TempData["ReturnPath"].ToString();
                return(Redirect(previousUrl));
            }
            return(View(student));
        }
        public async Task <IActionResult> Create(string[] selectedPriority, [Bind("Id, FirstName, LastName, Email, PhoneNumber, Gender, Comment")] StudentViewModel studentViewModel)
        {
            int.TryParse(TempData["ExamId"].ToString(), out int ExamId);
            var backToExam = RedirectToAction("Index", "Students", new { id = ExamId });

            if (ModelState.IsValid)
            {
                var    student = StudentsMappings.ToEntity(studentViewModel);
                Result result  = new Result
                {
                    ExamId = ExamId,
                };

                result = await _repository.InsertAsync <Result>(result);

                result.Student   = student;
                student.Result   = result;
                student.ResultId = result.Id;
                await _repository.InsertAsync <Student>(student);

                var studentsExam = _repository.FindByIdAsync <Exam>(ExamId).Result;

                await _repository.SaveAsync();

                studentViewModel = StudentsMappings.ToViewModel(student);


                var programs = await _repository.Query <Core.Library.Entities.Program>().ToListAsync();

                foreach (var program in programs)
                {
                    StudentCourse studentCourse = new StudentCourse
                    {
                        StudentId = student.Id,
                        Student   = student
                    };

                    var priorityProgram = _repository.Query <Core.Library.Entities.Program>()
                                          .Where(x => x.Name == program.Name)
                                          .FirstOrDefault();
                    var priorityCourse = _repository.Query <Course>()
                                         .Where(x => x.ProgramId == priorityProgram.Id)
                                         .Where(x => x.StartYear.Year == studentsExam.Date.Year)
                                         .FirstOrDefault();

                    if (priorityCourse != null)
                    {
                        studentCourse.CourseId = priorityCourse.Id;
                    }
                    else
                    {
                        Course course = new Course
                        {
                            StartYear = studentsExam.Date,
                            EndYear   = studentsExam.Date.AddYears(1),
                            CityId    = studentsExam.CityId,
                            ProgramId = priorityProgram.Id,
                            Title     = priorityProgram.Name
                        };
                        studentCourse.CourseId = course.Id;
                        studentCourse.Course   = course;
                        await _repository.InsertAsync <Course>(course);

                        _logger.LogInformation($"Added new course: {course.Title}. User {_user}.");
                    }

                    var selectedPriorityWithoutNullAndDuplictates = selectedPriority.Where(priority => !string.IsNullOrEmpty(priority)).Distinct().ToArray();
                    var listOfSelectedPriority = selectedPriorityWithoutNullAndDuplictates.ToList();
                    int priority = listOfSelectedPriority.IndexOf(program.Name);
                    if (priority == -1)
                    {
                        studentCourse.Priority = null;
                    }
                    else
                    {
                        studentCourse.Priority = priority + 1;
                    }

                    await _repository.InsertAsync <StudentCourse>(studentCourse);

                    _logger.LogInformation($"Created student: {studentViewModel.FirstName} {studentViewModel.LastName}. User {_user}.");
                }
                return(backToExam);
            }
            return(backToExam);
        }