public ActionResult <CourseDto> CreateCourseForAuthor(Guid authorId, CourseForCreateDto course)
        {
            if (!_courseLibraryRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }
            var courseEntity = _mapper.Map <Entities.Course>(course);

            _courseLibraryRepository.AddCourse(authorId, courseEntity);
            _courseLibraryRepository.Save();
            var courseToReturn = _mapper.Map <Models.CourseDto>(courseEntity);

            return(CreatedAtRoute("GetCourseForAuthor", new { authorId = authorId, courseId = courseToReturn.Id }, courseToReturn));
        }
        public async Task <IActionResult> CreateCourse(CourseForCreateDto courseForCreateDto)
        {
            courseForCreateDto.Name = courseForCreateDto.Name.ToLower();
            if (await _repo.CourseExist(courseForCreateDto.Name))
            {
                return(BadRequest("This Course alreaady exist, you can edit it to reflect new updates"));
            }
            var courseToCreate = _mapper.Map <Course>(courseForCreateDto);

            _repo.Add(courseToCreate);
            if (await _repo.SaveAll())
            {
                return(CreatedAtAction("GetCourse", new { id = courseToCreate.Id }, courseToCreate));
            }
            throw new Exception($"creating course {courseToCreate.Name} failed on save");
        }
Exemplo n.º 3
0
        public ActionResult <CourseDto> CreateCourseForAuthor([FromRoute] Guid authorId, [FromBody] CourseForCreateDto courseCreateDto) // Can be declared without annotations
        {
            if (!this._repos.AuthorExists(authorId))
            {
                return(NotFound());
            }

            var courseEntity = this._mapper.Map <Course>(courseCreateDto);

            this._repos.AddCourse(authorId, courseEntity);
            this._repos.Save();

            var courseDto = this._mapper.Map <CourseDto>(courseEntity);

            return(CreatedAtRoute("GetCourse", new { courseId = courseDto.Id, authorId = authorId }, courseDto));
        }