public CourseDto CreateCourse(CreateCourseDto createCourseDto, string currentUserId) { CreateCourseValidator authorValidator = new CreateCourseValidator(); if (!authorValidator.Validate(createCourseDto).IsValid) throw new Exception("Check_Your_Fileds"); Course course = _mapper.Map<CreateCourseDto, Course>(createCourseDto); course.CreatedOn = DateTime.Now; course.CreatedBy = currentUserId; _unitOfWork.CourseRepository.Add(course); _unitOfWork.Save(); return _mapper.Map<Course, CourseDto>(course); }
public async Task Validate_Should_Succeed_When_Command_Valid() { var unitOfWork = Substitute.For <ISchoolUnitOfWork>(); var sut = new CreateCourseValidator(unitOfWork); var command = new CreateCourse(Guid.NewGuid(), "new Course"); var result = await sut.ValidateAsync(command, CancellationToken.None); result.Should().NotBeNull(); result.Success.Should().BeTrue(); result.Errors.Should().BeEmpty(); }
public async Task ValidateAsync_should_fail_when_course_with_same_id_already_exists() { var course = new Course(Guid.NewGuid(), "existing course"); var repo = NSubstitute.Substitute.For <ICoursesRepository>(); repo.FindByIdAsync(Arg.Any <Guid>(), Arg.Any <CancellationToken>()) .ReturnsForAnyArgs(course); var unitOfWork = NSubstitute.Substitute.For <ISchoolUnitOfWork>(); unitOfWork.CoursesRepository.ReturnsForAnyArgs(repo); var sut = new CreateCourseValidator(unitOfWork); var command = new CreateCourse(course.Id, "another course"); var result = await sut.ValidateAsync(command, CancellationToken.None); result.Should().NotBeNull(); result.Success.Should().BeFalse(); result.Errors.Should().Contain(e => e.Context == nameof(CreateCourse.CourseId) && e.Message.Contains(course.Id.ToString())); }
public async Task Validate_Should_Fail_When_Course_With_Same_Title_Already_Exists() { var course = new Course(Guid.NewGuid(), "existing course"); var repo = Substitute.For <ICoursesRepository>(); repo.FindAsync(null, Arg.Any <CancellationToken>()) .ReturnsForAnyArgs(new[] { course }); var unitOfWork = Substitute.For <ISchoolUnitOfWork>(); unitOfWork.CoursesRepository.ReturnsForAnyArgs(repo); var sut = new CreateCourseValidator(unitOfWork); var command = new CreateCourse(Guid.NewGuid(), course.Title); var result = await sut.ValidateAsync(command, CancellationToken.None); result.Should().NotBeNull(); result.Success.Should().BeFalse(); result.Errors.Should().Contain(e => e.Context == nameof(CreateCourse.CourseTitle) && e.Message.Contains(course.Title)); }
public CreateCourseCommand(CoursesContext context, CreateCourseValidator validator, IMapper mapper) { _context = context; _validator = validator; _mapper = mapper; }