public async Task <IActionResult> EditStudents(CourseEditStudentsViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var result = await _coursesRepository.EditStudents(model);

                    if (result)
                    {
                        // success
                        return(RedirectToAction("Index", "Courses", new { message = "Редагування к-сті студентів успішно виконано." }));
                    }
                }
                catch
                {
                    // error
                    return(RedirectToAction("Index", "Courses", new { error = "При редагуванні к-сті студентів сталась невідома помилка!" }));
                }
            }
            else
            {
                ModelState.TryAddModelError(string.Empty, "Невідома помилка!");
            }
            return(View(model));
        }
        public async Task <IActionResult> EditStudents(int courseId)
        {
            var course   = _coursesRepository.GetById(courseId);
            var students = await _studentsRepository.GetActiveStudents();

            var subjectId = course.SubjectId;
            var subject   = _subjectsRepository.GetById(subjectId);

            var model = new CourseEditStudentsViewModel
            {
                CourseId = courseId,

                Course = new CourseViewModel
                {
                    CourseId    = courseId,
                    Description = subject.Description,
                    Start       = course.StartDate,
                    End         = course.EndDate,
                    Subject     = subject,
                    Name        = course.Name,
                    Teacher     = new UserViewModel
                    {
                        Email      = course.Teacher.Email,
                        FirstName  = course.Teacher.FirstName,
                        LastName   = course.Teacher.LastName,
                        MiddleName = course.Teacher.MiddleName,
                    },
                    TeacherId = course.TeacherId,
                    SubjectId = subjectId
                },
            };

            var studentsInCourse = (await _coursesRepository.GetStudents(model.CourseId)).Select(x => x.StudentId).ToList();

            model.Students = students.Select(x => new CourseStudentEditViewModel
            {
                Id       = x.StudentId,
                Name     = x.User.LastName + " " + x.User.FirstName + " " + x.User.MiddleName,
                Selected = studentsInCourse.Count == 0 ? false : studentsInCourse.Contains(x.StudentId)
            }).ToList();

            return(View(model));
        }
Esempio n. 3
0
        public async Task <bool> EditStudents(CourseEditStudentsViewModel model)
        {
            var courseId = model.CourseId;

            var courseModules = await _context.CourseModule.Where(x => x.CourseId == model.CourseId).Select(x => x.CourseModuleId).ToListAsync();

            var notSelectedStudents = model.Students.Where(x => !x.Selected).Select(x => x.Id).ToList();

            _context.CourseStudent.RemoveRange(await _context.CourseStudent.Where(x => x.CourseId == courseId && notSelectedStudents.Contains(x.StudentId.Value)).ToListAsync());
            _context.Mark.RemoveRange(await _context.Mark.Include(x => x.CourseModule).Where(x => x.CourseModule.CourseId == courseId && notSelectedStudents.Contains(x.StudentId)).ToListAsync());

            var existedStudents = await _context.CourseStudent.Where(cm => cm.CourseId == courseId).Select(x => x.StudentId).ToListAsync();

            var newStudents = model.Students.Where(x => x.Selected && !existedStudents.Contains(x.Id)).Select(x => x.Id).ToList();

            newStudents.ForEach(async studentId =>
            {
                await _context.CourseStudent.AddAsync(
                    new CourseStudent
                {
                    CourseId  = courseId,
                    StudentId = studentId
                }
                    );

                courseModules.ForEach(async courseModuleId =>
                {
                    await _context.Mark.AddAsync(new Mark
                    {
                        CourseModuleId = courseModuleId,
                        StudentId      = studentId,
                    });
                });
            });

            return(await _context.SaveChangesAsync() >= 0);
        }