public IActionResult Create(GradeForStudentAddDTO gradeForStudentAddDTO)
 {
     if (ModelState.IsValid)
     {
         _gradeService.AddGradeToStudent(gradeForStudentAddDTO);
         return(RedirectToAction("Index", new { id = gradeForStudentAddDTO.StudentId }));
     }
     return(View());
 }
Пример #2
0
        public GradeVM AddGradeToStudent(GradeForStudentAddDTO gradeForStudentAddDTO)
        {
            if (gradeForStudentAddDTO == null)
            {
                throw new ArgumentNullException($"DTO is null");
            }
            var teacher = _dbContext.Users.OfType <Teacher>().FirstOrDefault(t => t.Id == gradeForStudentAddDTO.TeacherId);
            var student = _dbContext.Users.OfType <Student>().FirstOrDefault(t => t.Id == gradeForStudentAddDTO.StudentId);
            var subject = _dbContext.Subjects.FirstOrDefault(s => s.Id == gradeForStudentAddDTO.SubjectId);

            if (teacher == null)
            {
                throw new ArgumentNullException("Teacher is null");
            }
            if (!_userManager.IsInRoleAsync(teacher, "Teacher").Result)
            {
                throw new InvalidOperationException("Provided user is not a teacher");
            }
            if (student == null)
            {
                throw new ArgumentNullException("Student is null");
            }
            if (!_userManager.IsInRoleAsync(student, "Student").Result)
            {
                throw new InvalidOperationException("Provided user is not a student");
            }
            if (subject == null)
            {
                throw new ArgumentNullException("Subject is null");
            }
            var gradeEntity = _mapper.Map <Grade>(gradeForStudentAddDTO);

            _dbContext.Grades.Add(gradeEntity);
            _dbContext.SaveChanges();
            var gradeVM = _mapper.Map <GradeVM>(gradeEntity);

            return(gradeVM);
        }
        public IActionResult Create(int?id = null)
        {
            if (!id.HasValue)
            {
                return(View("Error"));
            }

            var teacher        = _userManager.GetUserAsync(User).Result as Teacher;
            var gradeForCreate = new GradeForStudentAddDTO()
            {
                TeacherId = teacher.Id
            };

            var subjectsVM = _mapper.Map <IEnumerable <SubjectVM> >(teacher.Subjects);

            ViewBag.SubjectsSelectList = _mapper.Map <IEnumerable <SelectListItem> >(subjectsVM);

            var studentVM = _studentService.GetStudent(x => x.Id == id.Value);

            ViewBag.Student = studentVM;

            return(View(gradeForCreate));
        }