public StudentVm AttachStudentToGroup(AttachDetachStudentToGroupDto attachStudentToGroupDto)
        {
            if (attachStudentToGroupDto == null)
            {
                throw new ArgumentNullException($"Dto of type is null");
            }

            var student = _dbContext.Users.OfType <Student>().FirstOrDefault(t => t.Id == attachStudentToGroupDto.StudentId);

            if (student == null || !_userManager.IsInRoleAsync(student, "Student").Result)
            {
                throw new ArgumentNullException($"Student is null or user is not student");
            }

            var group = _dbContext.Groups.FirstOrDefault(x => x.Id == attachStudentToGroupDto.GroupId);

            if (group == null)
            {
                throw new ArgumentNullException($"group is null");
            }

            student.GroupId = group.Id;
            student.Group   = group;
            _dbContext.SaveChanges();
            var studentVm = Mapper.Map <StudentVm>(student);

            return(studentVm);
        }
Ejemplo n.º 2
0
        private IActionResult AttachDetachGetView(int studentId)
        {
            var students       = _studentService.GetStudents();
            var groups         = _groupService.GetGroups();
            var currentStudent = students.FirstOrDefault(x => x.Id == studentId);

            if (currentStudent == null)
            {
                throw new ArgumentNullException("studentId not exists.");
            }

            var attachDetachStudentToGroupDto = new AttachDetachStudentToGroupDto
            {
                StudentId = currentStudent.Id
            };

            ViewBag.SubjectList = new SelectList(students.Select(s => new
            {
                Text     = $"{s.FirstName} {s.LastName}",
                Value    = s.Id,
                Selected = s.Id == currentStudent.Id
            }), "Value", "Text");
            ViewBag.GroupList = new SelectList(groups.Select(s => new
            {
                Text  = s.Name,
                Value = s.Id
            }), "Value", "Text");
            return(View("AttachDetachStudentToGroup", attachDetachStudentToGroupDto));
        }
Ejemplo n.º 3
0
        public IActionResult AttachStudentToGroup(AttachDetachStudentToGroupDto attachDetachStudentToGroupDto)
        {
            if (ModelState.IsValid)
            {
                _groupService.AttachStudentToGroup(attachDetachStudentToGroupDto);
                return(RedirectToAction("Index"));
            }

            return(View());
        }
        public IActionResult DetachStudentFromGroup(AttachDetachStudentToGroupDto attachDetachStudentToGroup)
        {
            if (ModelState.IsValid)
            {
                _groupService.DetachStudentFromGroup(attachDetachStudentToGroup);
                return(RedirectToAction("ViewStudents", "Student"));
            }

            return(View());
        }