public IActionResult CreateAuthorCollection([FromBody] IEnumerable <AuthorForCreationDto> authorCollection) { if (authorCollection == null) { return(BadRequest()); } var authorEntities = Mapper.Map <IEnumerable <Author> >(authorCollection); foreach (var author in authorEntities) { _libraryRepository.AddAuthor(author); } if (!_libraryRepository.Save()) { throw new Exception("Creating an author collection failed on save."); } var authorCollectionToReturn = Mapper.Map <IEnumerable <AuthorDto> >(authorEntities); var idsAsString = string.Join(",", authorCollectionToReturn.Select(a => a.Id)); return(CreatedAtRoute("GetAuthorCollection", new { ids = idsAsString }, authorCollectionToReturn)); // return Ok(); }
public IActionResult CreateAuthor([FromBody] AuthorForCreationDto author) { if (author == null) { return(BadRequest()); } var authorEntity = Mapper.Map <Author>(author); _libraryRepository.AddAuthor(authorEntity); if (!_libraryRepository.Save()) { return(StatusCode(500, "A problem happened with handling your request.")); } var authorToReturn = Mapper.Map <AuthorDto>(authorEntity); return(CreatedAtRoute("GetAuthor", new { id = authorToReturn.Id }, authorToReturn)); }
public IActionResult CreateBookForAuthor(Guid authorId, [FromBody] BookForCreationDto book) { if (book == null) { return(BadRequest()); } if (book.Description == book.Title) { ModelState.AddModelError(nameof(BookForCreationDto), "The provided description should be different from the title"); } if (!ModelState.IsValid) { //return 422 return(new UnprocessableEntityObjectResult(ModelState)); } if (!_libraryRepository.AuthorExists(authorId)) { return(NotFound()); } var bookEntity = Mapper.Map <Book>(book); _libraryRepository.AddBookForAuthor(authorId, bookEntity); if (!_libraryRepository.Save()) { throw new Exception($"Creating a book for author {authorId} failed on save"); } var bookToReturn = Mapper.Map <BookDto>(bookEntity); return(CreatedAtRoute("GetBookForAuthor", new { authorId = authorId, id = bookToReturn.Id }, bookToReturn)); }