public async Task AddCourse(Guid id, StudentCourseCreationDto studentCourseCreationDto) { var student = await this.readRepository.GetAll <Domain.Entities.Student>().Where(s => s.Id == id) .Include(s => s.StudentCourses).ThenInclude(sc => sc.Course).FirstOrDefaultAsync(); if (student == null) { throw new StudentNotFoundException(id); } var course = await this.readRepository.GetByIdAsync <Domain.Entities.Course>(studentCourseCreationDto.CourseId); if (course == null) { throw new CourseNotFoundException(studentCourseCreationDto.CourseId); } if (student.YearOfStudy < course.Year) { throw new StudentCannotApplyException(id, studentCourseCreationDto.CourseId); } var appliedCourses = student.StudentCourses; if (appliedCourses.Any(c => c.CourseId == course.Id)) { throw new StudentAlreadyAppliedException(id, studentCourseCreationDto.CourseId); } var studentCourse = this.studentCourseMapper.Map(id, studentCourseCreationDto); await this.writeRepository.AddNewAsync(studentCourse); await this.writeRepository.SaveAsync(); }
public void Cleanup() { this._studentCourse = null; this._studentCourseCreationDto = null; this._studentCourseDetailsDto = null; this._studentCourseMapper = null; }
public void Setup() { this._studentCourse = StudentCourseTestUtils.GetStudentCourse(); this._studentCourseCreationDto = StudentCourseTestUtils.GetStudentCourseCreationDto(this._studentCourse.CourseId); this._studentCourseDetailsDto = StudentCourseTestUtils.GetStudentCourseDetailsDto(this._studentCourse.StudentId, this._studentCourse.CourseId); this._studentCourseMapper = new StudentCourseMapper(); }
public void Setup() { client = new CustomWebApplicationFactory <Startup>().CreateClient(); student1 = StudentTestUtils.GetStudent(); student2 = StudentTestUtils.GetStudent2(); studentDetailsDto1 = StudentTestUtils.GetStudentDetailsDto(student1.Id); studentDetailsDto2 = StudentTestUtils.GetStudentDetailsDto(student2.Id); studentCreationDto = StudentTestUtils.GetStudentCreationDto(); course1 = CourseTestUtils.GetCourse(); course2 = CourseTestUtils.GetCourse2(); courseDetailsDto = CourseTestUtils.GetCourseDetailsDto(course1.Id); studentCourseCreationDto = StudentCourseTestUtils.GetStudentCourseCreationDto(course2.Id); }
public void Cleanup() { this._student = null; this._course = null; this._courseDto = null; this._studentCourse1 = null; this._studentCourse2 = null; this._studentCourseCreationDto = null; this._mockReadRepository = null; this._mockWriteRepository = null; this._mockStudentCourseMapper = null; this._mockCourseMapper = null; this._studentCourseService = null; }
public void Setup() { this._student = StudentTestUtils.GetStudent(); this._course = CourseTestUtils.GetCourse2(); this._courseDto = CourseTestUtils.GetCourseDetailsDto(_course.Id); this._studentCourse1 = StudentCourseTestUtils.GetStudentCourse(_student.Id, _course.Id); this._studentCourse2 = StudentCourseTestUtils.GetStudentCourse2(); this._studentCourseCreationDto = StudentCourseTestUtils.GetStudentCourseCreationDto(this._studentCourse1.CourseId); this._mockReadRepository = new Mock <IReadRepository>(); this._mockWriteRepository = new Mock <IWriteRepository>(); this._mockStudentCourseMapper = new Mock <IStudentCourseMapper>(); this._mockCourseMapper = new Mock <ICourseMapper>(); this._studentCourseService = new StudentCourseService(_mockReadRepository.Object, _mockWriteRepository.Object, _mockStudentCourseMapper.Object, _mockCourseMapper.Object); }
public async Task<IActionResult> AddCourse(Guid id, [FromBody] StudentCourseCreationDto studentCourseCreationDto) { if (!ModelState.IsValid) { return BadRequest(ModelState); } try { await this.studentCourseService.AddCourse(id, studentCourseCreationDto); return NoContent(); } catch (Exception exception) when (exception is StudentNotFoundException || exception is CourseNotFoundException) { return NotFound(exception.Message); } catch (Exception exception) when (exception is StudentCannotApplyException || exception is StudentAlreadyAppliedException) { return BadRequest(exception.Message); } }