public ActionResult <CoursesDTO> CreateCourseForAuthor(int authorId, createCourseForAuthorDTOW createCourse)
        {
            //check if the author exists
            var singleauthor = _courseLibrary.GetAuthor(authorId);

            if (singleauthor == null)
            {
                return(NotFound(new JsonResponse <string>()
                {
                    Success = false,
                    ErrorMessage = "AuthorId is Invalid."
                }));
            }
            var course = _mapper.Map <Entities.Course>(createCourse);

            //course.ID = authorId;
            _courseLibrary.AddCourse(authorId, course);
            _courseLibrary.Save();

            var createdCourse = _mapper.Map <CoursesDTO>(course);

            return(CreatedAtRoute("getCourseForAnAuthor", new { authorId = authorId, courseId = createdCourse.ID }, createdCourse));
            //return Ok(new JsonResponses<AuhtorDTO>()
            //{
            //    Success = true,
            //    Result = new List<AuhtorDTO>() {
            //        createdAuthor
            //    }
            //});
        }
Ejemplo n.º 2
0
        public ActionResult <CourseDTO> CreateCouse(
            Guid authorId, CourseForCreationDTO course
            )
        {
            if (_clRepo.AuthorExists(authorId))
            {
                return(NotFound());
            }

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

            _clRepo.AddCourse(authorId, courseEntity);
            _clRepo.Save();

            var courseToReturn = _mapper.Map <CourseDTO>(courseEntity);

            return(CreatedAtRoute(
                       "GetCourseForAuthor",
                       new {
                authorId = courseToReturn.AuthorId,
                courseId = courseToReturn.Id
            },
                       courseToReturn
                       ));
        }
        public ActionResult <CourseDto> CreateCourseForAuthor(
            Guid authorId,
            [FromBody] CourseCreationDto courseCreationDto)
        {
            if (!_repository.AuthorExists(authorId))
            {
                return(NotFound());
            }

            var course = _mapper.Map <Course>(courseCreationDto);

            _repository.AddCourse(authorId, course);
            var isSaved = _repository.Save();

            if (!isSaved)
            {
                throw new InvalidOperationException("Error occurred: Unable to create course");
            }

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

            return(CreatedAtAction(
                       "GetCourseForAuthor",
                       new
            {
                authorId,
                courseId = courseDto.Id
            },
                       courseDto));
        }
Ejemplo n.º 4
0
        public IActionResult AddCourseForAuthor(Guid authorId, CourseForCreationDto courseDto)
        {
            var entity = mapper.Map <Course>(courseDto);

            courseLibraryRepository.AddCourse(authorId, entity);
            courseLibraryRepository.Save();
            return(CreatedAtRoute("GetCourse", new { authorId = authorId, courseId = entity.Id }, entity));
        }
Ejemplo n.º 5
0
        public ActionResult <AuthorDto> CreateAuthor(AuthorForCreationDto author)
        {
            var authorEntity = _mapper.Map <Author>(author);

            _courseLibraryRepository.AddAuthor(authorEntity);
            _courseLibraryRepository.Save();
            var authorToReturn = _mapper.Map <AuthorDto>(authorEntity);

            return(CreatedAtRoute("GetAuthor", new { authorId = authorToReturn.Id }, authorToReturn));
        }
Ejemplo n.º 6
0
        public ActionResult <AuthorDto> CreateAuthor(CreateAuthorDto createAuthorDto)
        {
            var author = _mapper.Map <Author>(createAuthorDto);

            _courseRepository.AddAuthor(author);
            _courseRepository.Save();
            var authorDto = _mapper.Map <AuthorDto>(author);

            return(CreatedAtRoute("GetAuthors", new { authorId = author.Id }, authorDto));
        }
Ejemplo n.º 7
0
        public ActionResult <CourseDto> CreateCourseForAuthor(Guid authorId, CourseForCreationDto courseDtoForCreation)
        {
            var courseEntity = _mapper.Map <Course>(courseDtoForCreation);

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

            return(CreatedAtRoute("GetCourseForAuthor", new { authorId = authorId, courseId = courseToReturn.Id }, courseToReturn));
        }
Ejemplo n.º 8
0
        public ActionResult <AuthorDto> CreateAuthor([FromBody] AuthorCreationDto author)
        {
            var authorEntity = _mapper.Map <Author>(author);

            _courseLibraryRepository.AddAuthor(authorEntity);
            _courseLibraryRepository.Save();

            var authorDto = _mapper.Map <AuthorDto>(authorEntity);

            return(CreatedAtRoute(nameof(GetAuthor), new { authorId = authorDto.Id }, authorDto));
        }
Ejemplo n.º 9
0
        public ActionResult <AuthorDto> CreateAuthor(AuthorForCreationDto authorForCreation)
        {
            var author = mapper.Map <Author>(authorForCreation);

            repository.AddAuthor(author);
            repository.Save();

            var authorResult = mapper.Map <AuthorDto>(author);

            return(CreatedAtRoute("GetAuthor", new { authorId = author.Id }, authorResult));
        }
Ejemplo n.º 10
0
        public IActionResult AddAuthor([FromBody] AuthorForCreationDto authorForCreation)
        {
            var author = _mapper.Map <Author>(authorForCreation);

            _courseLibraryRepository.AddAuthor(author);

            _courseLibraryRepository.Save();

            var result = _mapper.Map <AuthorDto>(author);

            return(CreatedAtRoute("GetAuthor", new { authorId = author.Id }, result));
        }
Ejemplo n.º 11
0
        public ActionResult <AuthorDto> CreateAuthors(AuthorForCreationDto authorForCreation)
        {
            var authorEntity = _mapper.Map <Author>(authorForCreation);

            _courseLibraryRepository.AddAuthor(authorEntity);
            _courseLibraryRepository.Save();
            var linkedResourceToReturn = _mapper.Map <AuthorDto>(authorEntity).ShapeData(null) as IDictionary <string, object>;
            var links = CreateLinksForAuthor(authorEntity.Id, null);

            linkedResourceToReturn.Add("links", links);
            return(CreatedAtRoute("GetAuthor", new { authorId = linkedResourceToReturn["Id"] }, linkedResourceToReturn));
        }
Ejemplo n.º 12
0
        public IActionResult CreateAuthor(AuthorForCreationDto authorForCreationDto)
        {
            var authorForCreation = _mapper.Map <Author>(authorForCreationDto);

            _courseLibraryRepository.AddAuthor(authorForCreation);
            _courseLibraryRepository.Save();
            var createdAuthor          = _mapper.Map <AuthorDto>(authorForCreation);
            var links                  = GetLinksForAuthor(createdAuthor.Id);
            var linkedResourceToReturn = createdAuthor.ShapeData() as IDictionary <string, object>;

            linkedResourceToReturn.Add("links", links);
            return(CreatedAtRoute("GetAuthor", new { authorId = createdAuthor.Id }, linkedResourceToReturn));
        }
Ejemplo n.º 13
0
        public ActionResult <AuthorDTO> CreateAuthor(AuthorForCreationDTO author)
        {
            var authorEntity = _mapper.Map <Entities.Author>(author);

            _courseLibraryRepository.AddAuthor(authorEntity);
            _courseLibraryRepository.Save();
            // once we get the authorId via Save(), we can return the author
            var authorToReturn = _mapper.Map <AuthorDTO>(authorEntity);

            return(CreatedAtRoute("GetAuthor",
                                  new { authorId = authorToReturn.Id },
                                  authorToReturn));
        }
Ejemplo n.º 14
0
        public ActionResult <AuthorDTO> CreateAuthor(AuthorForCreationDTO authorForCreationDTO)
        {
            var authorEntity = _mapper.Map <Author>(authorForCreationDTO);

            _courseLibraryRepository.AddAuthor(authorEntity);

            _courseLibraryRepository.Save();

            var authorToRetrun = _mapper.Map <AuthorDTO>(authorEntity);

            // RouteName will be that, which is defined in [HttpPost, Name="..Name.."]
            return(CreatedAtRoute("GetAuthor", new { authorId = authorToRetrun.Id }, authorToRetrun));
        }
Ejemplo n.º 15
0
        public ActionResult <AuthorDto> CreateAuthor(AuthorsForCreationDto author)
        {
            // Added to the DbContext
            var authorEntity = _mapper.Map <Entities.Author>(author);

            // Added to the Repository
            _courseLibraryRepository.AddAuthor(authorEntity);
            _courseLibraryRepository.Save();

            var authorToReturn = _mapper.Map <AuthorDto>(authorEntity);

            return(CreatedAtRoute("GetAuthors", new { authorId = authorToReturn.Id }, authorToReturn));
        }
Ejemplo n.º 16
0
        public ActionResult <AuthorDto> Post(AuthorForCreationDto author)
        {
            var newAuthor = _mapper.Map <Author>(author);

            _repository.AddAuthor(newAuthor);
            _repository.Save();
            var authorDto    = _mapper.Map <AuthorDto>(newAuthor);
            var shapedAuthor = _dataShapingService.ShapeData(authorDto) as IDictionary <string, object>;
            var links        = CreateAuthorLinks(authorDto.Id, null);

            shapedAuthor.Add("links", links);
            return(CreatedAtRoute("GetAuthor", new { authorId = authorDto.Id }, shapedAuthor));
        }
Ejemplo n.º 17
0
        public ActionResult <AuthorDto> CreateAuthor(AuthorForCreationDto author)
        {
            var authorEntity = _mapper.Map <Entities.Author>(author);

            _courseLibraryRepository.AddAuthor(authorEntity);
            _courseLibraryRepository.Save();

            var authorToReturn = _mapper.Map <AuthorDto>(authorEntity);

            // return othe route method, in this case GetAuthor
            return(CreatedAtRoute("GetAuthor",
                                  new { authorId = authorEntity.Id },
                                  authorToReturn));
        }
Ejemplo n.º 18
0
        public async Task <ActionResult <AuthorForReturn> > CreateAuthor(AuthorForCreation author)
        {
            var authorToAdd = _mapper.Map <Author>(author);

            _repository.AddAuthor(authorToAdd);
            if (!await _repository.Save())
            {
                return(BadRequest("Error Happens when saving in Data Base"));
            }

            var authorToReturn = _mapper.Map <AuthorForReturn>(authorToAdd);

            return(CreatedAtRoute("getAuthor", new { authorId = authorToAdd.Id }, authorToReturn));
        }
        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));
        }
Ejemplo n.º 20
0
        public IActionResult CreateAuthorWithDateOfDeath(AuthorForCreationWithDateOfDeathDto authorToAdd)
        {
            Author authorEntity = _mapper.Map <Author>(authorToAdd);

            _repository.AddAuthor(authorEntity);
            _repository.Save();

            var linkResourceToReturn = _mapper.Map <AuthorDto>(authorEntity).ShapeData() as IDictionary <string, object>;
            var links = CreateLinksForAuthor(authorEntity.Id);

            linkResourceToReturn.Add("links", links);

            return(CreatedAtRoute("GetAuthor", new { authorId = authorEntity.Id }, linkResourceToReturn));
        }
Ejemplo n.º 21
0
        public IActionResult CreateAuthor([FromBody] AuthorForCreationDto author)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var authorEntity = _mapper.Map <Author>(author);

            _repository.AddAuthor(authorEntity);
            _repository.Save();

            var authorEntityToReturn = _mapper.Map <AuthorDto>(authorEntity);

            return(CreatedAtRoute("GetAuthor", new { authorId = authorEntityToReturn.Id }, authorEntityToReturn));
        }
        public async Task <ActionResult <AuthorDto> > CreateAuthor(AuthorForCreationDto author)
        {
            if (author == null)
            {
                return(BadRequest());
            }
            //creating Author entity from AuthorForCreation
            var authorEntity = _mapper.Map <Author>(author);

            _courseLibaryService.AddAuthor(authorEntity);
            await _courseLibaryService.Save();

            var authorToReturn = _mapper.Map <AuthorDto>(authorEntity);

            return(CreatedAtRoute("getAuthor", new { AuthorId = authorToReturn.Id }, authorToReturn));
        }
Ejemplo n.º 23
0
        public ActionResult <AuthorDTO> CreateAuthor(AuthorForCreationDTO author)
        {
            if (author == null)
            {
                return(BadRequest());
            }

            var authorEntity = _mapper.Map <Entities.Author>(author);

            _courseLibraryRepository.AddAuthor(authorEntity);
            _courseLibraryRepository.Save();

            var authorReturned = _mapper.Map <AuthorDTO>(authorEntity);

            return(CreatedAtRoute("GetAuthor", new { authorId = authorReturned.Id }, authorReturned));
        }
        public ActionResult <AuthorDto> CreateAuthor(AuthorForCreationDto authorToCreate)
        {
            //Using the [ApiController] and ASP.NET Core 3.x - this is done automatically
            //if (authorToCreate == null)
            //    return BadRequest();

            var authorEntity = _mapper.Map <Author>(authorToCreate);

            _courseLibraryRepository.AddAuthor(authorEntity);

            //if the DB is down a 500 server error will be thrown (and a default message provided in middleware)
            _courseLibraryRepository.Save();
            var returnAuthorDto = _mapper.Map <AuthorDto>(authorEntity);

            return(CreatedAtRoute(nameof(GetAuthor), new { authorId = returnAuthorDto.Id }, returnAuthorDto));
        }
        public ActionResult <AuthorDto> CreateAuthor(AuthorForCreationDto author)
        {
            if (author == null)
            {
                return(BadRequest());
            }

            var authorEntity = _mapper.Map <Author>(author);

            _courseLibraryRepository.AddAuthor(authorEntity);
            _courseLibraryRepository.Save();

            var res = _mapper.Map <AuthorDto>(authorEntity);

            return(CreatedAtRoute("GetAuthor", new { authorId = res.Id }, res));
        }
        public ActionResult <AuthorDto> CreateAuthor([FromBody] AuthorForCreationDto author)
        {
            if (author == null) //This validation is no longer needed in Core 3, if author cannot be set then returns 400 error code when WebAPI attribute is set.
            {
                return(BadRequest());
            }

            var authorEntity = _mapper.Map <Entities.Author>(author);

            _courseLibraryRepository.AddAuthor(authorEntity);
            _courseLibraryRepository.Save();

            var authorToReturn = _mapper.Map <AuthorDto>(authorEntity);

            return(CreatedAtRoute("GetAuthor", new { authorId = authorToReturn.Id }, authorToReturn));
        }
Ejemplo n.º 27
0
        public IActionResult CreateAuthor([FromBody] CreateAuthorDto createAuthorDto)
        {
            var author = mapper.Map <Author>(createAuthorDto);

            courseRepository.AddAuthor(author);
            courseRepository.Save();
            return(CreatedAtRoute("GetAuthor", new { authorId = author.Id }, author));
        }
Ejemplo n.º 28
0
        public ActionResult <CourseGetDto> CreateCourseForAuthor(Guid authorId, CourseCreateDto course)
        {
            if (!_repo.AuthorExists(authorId))
            {
                return(NotFound());
            }
            var courseEntity = _mapper.Map <Entities.Course>(course);

            _repo.AddCourse(authorId, courseEntity);
            _repo.Save();

            var courseToReturn = _mapper.Map <CourseGetDto>(courseEntity);

            return(CreatedAtAction(nameof(GetCoursesForAuthor),
                                   new { authorId = authorId, courseId = courseToReturn.Id },
                                   courseToReturn));
        }
        public IActionResult CreateCourseForAuthor([FromRoute] Guid authorId, CreateCourseDTO course)
        {
            if (!_courseLibrary.AuthorExists(authorId))
            {
                return(NotFound());
            }

            var courseToMap = _mapper.Map <Course>(course);

            _courseLibrary.AddCourse(authorId, courseToMap);
            _courseLibrary.Save();

            var courseToReturn = _mapper.Map <CourseDTO>(courseToMap);

            return(CreatedAtRoute("GetCourseForAuthor", new { authorId = authorId, courseId = courseToReturn.Id }
                                  , courseToReturn));
        }
        public ActionResult <AuthorDto> CreateAuthor(AuthorForCreationDto author)
        {
            // Since .net core 2.1, [ApiController] attribute returns 400 errors for validation automatically.
            //if (author == null)
            //    return BadRequest();

            var authorEntity = _mapper.Map <Author>(author);

            _courseLibraryRepository.AddAuthor(authorEntity);
            _courseLibraryRepository.Save();

            var authorToReturn = _mapper.Map <AuthorDto>(authorEntity);

            return(CreatedAtRoute("GetAuthor",
                                  new { authorId = authorToReturn.Id },
                                  authorToReturn));
        }