public void CreateCourseValidator_ShouldValidateProperly(CourseInput input, bool valid) { var validator = new CourseInputValidator(); var result = validator.Validate(input); result.IsValid.Should().Be(valid); }
public async Task <CourseDto> AddAndGetObj(CourseInput input) { //验证传入参数 if (!input.SubjectProjectId.HasValue) { throw new UserFriendlyException("传入SubjectProjectId参数不正确!"); } if (string.IsNullOrEmpty(input.Name)) { throw new UserFriendlyException("传入Name参数不正确!"); } if (!input.Credit.HasValue) { throw new UserFriendlyException("传入Credit参数不正确!"); } if (!input.LimitNumbers.HasValue) { throw new UserFriendlyException("传入LimitNumbers参数不正确!"); } if (!input.SelectedNumbers.HasValue) { throw new UserFriendlyException("传入SelectedNumbers参数不正确!"); } //验证课程所属项目是否存在 var subject = await _subjectRepository.FirstOrDefaultAsync(x => x.Id == input.SubjectProjectId); if (subject == null) { throw new UserFriendlyException($"编号为{input.SubjectProjectId}的项目不存在!"); } //创建课程对象 var course = new Education.Course { SubjectProjectId = input.SubjectProjectId.Value, Name = input.Name, Credit = input.Credit.Value, LimitNumbers = input.LimitNumbers.Value, SelectedNumbers = input.SelectedNumbers.Value }; //备注属性赋值 if (!string.IsNullOrEmpty(input.Remark)) { course.Remark = input.Remark; } //执行插入数据方法 var id = await _courseRepository.InsertAndGetIdAsync(course); UnitOfWorkManager.Current.SaveChanges(); return((await _courseRepository.GetAsync(id)).MapTo <CourseDto>()); }
public async Task Edit(CourseInput input) { //检查传入参数 if (!input.Id.HasValue) { throw new UserFriendlyException("传入Id参数不正确!"); } //获取需要修改的对象 var course = await _courseRepository.FirstOrDefaultAsync(x => x.Id == input.Id.Value); if (course == null) { throw new UserFriendlyException("当前课程不存在!"); } //修改属性值 if (input.SubjectProjectId.HasValue) { //验证课程所属项目是否存在 var subject = await _subjectRepository.FirstOrDefaultAsync(x => x.Id == input.SubjectProjectId); if (subject == null) { throw new UserFriendlyException($"编号为{input.SubjectProjectId}的项目不存在!"); } course.SubjectProjectId = input.SubjectProjectId.Value; } if (!string.IsNullOrWhiteSpace(input.Name)) { course.Name = input.Name; } if (input.Credit.HasValue) { course.Credit = input.Credit.Value; } if (input.LimitNumbers.HasValue) { course.LimitNumbers = input.LimitNumbers.Value; } if (input.SelectedNumbers.HasValue) { course.SelectedNumbers = input.SelectedNumbers.Value; } if (!string.IsNullOrWhiteSpace(input.Remark)) { course.Remark = input.Remark; } //执行修改数据方法 await _courseRepository.UpdateAsync(course); }
public async Task Add(CourseInput input) { //验证传入参数 if (!input.SubjectProjectId.HasValue) { throw new UserFriendlyException("传入SubjectProjectId参数不正确!"); } if (string.IsNullOrEmpty(input.Name)) { throw new UserFriendlyException("传入Name参数不正确!"); } if (!input.Credit.HasValue) { throw new UserFriendlyException("传入Credit参数不正确!"); } if (!input.LimitNumbers.HasValue) { throw new UserFriendlyException("传入LimitNumbers参数不正确!"); } if (!input.SelectedNumbers.HasValue) { throw new UserFriendlyException("传入SelectedNumbers参数不正确!"); } //验证课程所属项目是否存在 var subject = await _subjectRepository.FirstOrDefaultAsync(x => x.Id == input.SubjectProjectId); if (subject == null) { throw new UserFriendlyException($"编号为{input.SubjectProjectId}的项目不存在!"); } //创建课程对象 var course = new Education.Course { SubjectProjectId = input.SubjectProjectId.Value, Name = input.Name, Credit = input.Credit.Value, LimitNumbers = input.LimitNumbers.Value, SelectedNumbers = input.SelectedNumbers.Value }; //备注属性赋值 if (!string.IsNullOrEmpty(input.Remark)) { course.Remark = input.Remark; } //执行插入数据方法 await _courseRepository.InsertAsync(course); }
private async Task CreateCourseHandler_ShouldAddItemToDatabase() { var input = new CourseInput { Name = "Test", DayOfWeek = DayOfWeek.Monday, StartTime = new TimeOfDay(0, 0), EndTime = new TimeOfDay(12, 12), }; await _handler.Handle(new CreateCourse(input), CancellationToken.None); DbContext.Courses.AsEnumerable().Should().NotBeEmpty(); var course = DbContext.Courses.SingleOrDefault(); course.Should().NotBeNull(); course?.Name.Should().Be(input.Name); }
public CreateCourse(CourseInput input) => Input = input;
public async Task <IActionResult> Create([FromBody] CourseInput input) { var dto = await Mediator.Send(new CreateCourse(input)); return(CreatedAtAction(nameof(Get), new { dto.Id }, dto)); }