Beispiel #1
0
        public void Delete(CourseStudentDTO item)
        {
            var courseStudent = _mapper.Map <CourseStudent>(item);

            _database.CourseStudentRepository.DeleteByEntity(courseStudent);
            _database.Save();
        }
Beispiel #2
0
 public CourseStudent MapCourseStudentDTO(CourseStudentDTO student)
 {
     return(new CourseStudent {
         CourseId = student.CourseId,
         StudentId = student.Student.Id
     });
 }
Beispiel #3
0
        public void UpdateCourseStudent(CourseStudentDTO item)
        {
            var course = _database.CourseRepository.Get(item.CourseId);

            course.IsUpdated = true;
            _database.CourseRepository.Update(course);
            _database.Save();
            var courseStudent = _mapper.Map <CourseStudent>(item);

            _database.CourseStudentRepository.Update(courseStudent);
            _database.Save();;
        }
        public async Task <ActionResult <CourseStudentDTO> > PostCourseStudent(Guid courseId, CourseStudentDTO student)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (courseId != student.CourseId)
            {
                ModelState.AddModelError(string.Empty, "The courseId request parameter did not match the courseId property in the data.");
                return(BadRequest(ModelState));
            }

            var cStudent = courseMapper.MapCourseStudentDTO(student);

            var existingStudent = await _context.CourseStudents.FindAsync(cStudent.CourseId, cStudent.StudentId);

            if (existingStudent != null)
            {
                ModelState.AddModelError(string.Empty, "The student you are trying to add already exists.");
                return(BadRequest(ModelState));
            }

            var course = await _context.Courses
                         .Include(c => c.Professor)
                         .Include(c => c.CourseInfo)
                         .FirstOrDefaultAsync(c => c.Id == cStudent.CourseId);

            var authorization = await authorizationService.AuthorizeAsync(User, course, AuthorizationConstants.CanUpdateCoursePolicy);

            if (!authorization.Succeeded)
            {
                return(Forbid());
            }

            _context.CourseStudents.Add(cStudent);
            await _context.SaveChangesAsync();

            cStudent = await _context.CourseStudents
                       .Include(cs => cs.Student).ThenInclude(s => s.User)
                       .FirstOrDefaultAsync(cs => cs.CourseId == cStudent.CourseId && cs.StudentId == cStudent.StudentId);

            await cache.ClearCourseCacheAsync(courseId, course.CourseInfo.Slug);

            return(CreatedAtAction("GetCourse", new { id = cStudent.CourseId }, courseMapper.MapCourseStudent(cStudent)));
        }