コード例 #1
0
        public async Task <ActionResult <CourseResource> > UpdateCourse(int id, [FromBody] SaveCourseResource saveCourseResource)
        {
            var validator        = new SaveCourseResourceValidator();
            var validationResult = await validator.ValidateAsync(saveCourseResource);

            var requestIsInvalid = id <= 0 || !validationResult.IsValid;

            if (requestIsInvalid)
            {
                _logger.LogError(DateTime.Now + "\nUpdating course failed, validation isn't valid\n" + validationResult.Errors.ToString());
                return(BadRequest(validationResult.Errors));
            }

            var courseToBeUpdate = await _courseService.GetCourseWithLecturerById(id);

            var course = _mapper.Map <SaveCourseResource, Course>(saveCourseResource);

            course.Id = id;

            await _courseService.UpdateCourse(course);

            var updatedCourse = await _courseService.GetCourseWithLecturerById(id);

            var updatedCourseResource = _mapper.Map <Course, SaveCourseResource>(updatedCourse);

            return(Ok(updatedCourseResource));
        }
コード例 #2
0
        public async Task <ActionResult <CourseResource> > CreateCourse([FromBody] SaveCourseResource saveCourseResource)
        {
            var validator        = new SaveCourseResourceValidator();
            var validationResult = await validator.ValidateAsync(saveCourseResource);

            if (!validationResult.IsValid)
            {
                _logger.LogError(DateTime.Now + "\nCreating course failed, validation isn't valid\n" + validationResult.Errors);
                return(BadRequest(validationResult.Errors));
            }

            var courseToCreate = _mapper.Map <SaveCourseResource, Course>(saveCourseResource);

            var result = await _courseService.CreateCourse(courseToCreate);

            var createdCourse = _mapper.Map <Course, CourseResource>(result);

            return(Ok(createdCourse));
        }