public IActionResult CreateAuthor([FromBody] AuthorCreationDto author) { if (author == null || !ModelState.IsValid) { return(BadRequest()); } var authorEntity = Mapper.Map <Author>(author); _libraryRepository.AddAuthor(authorEntity); if (!_libraryRepository.Save()) { throw new Exception("Creating an author failed on save"); //return StatusCode(500, "A problem happened with handling your request"); } var authorToReturn = Mapper.Map <AuthorDto>(authorEntity); var links = CreateLinksForAuthor(authorToReturn.Id, null); var linkedResourceToReturn = authorToReturn.ShapeData(null) as IDictionary <string, object>; linkedResourceToReturn.Add("links", links); return(CreatedAtRoute("GetAuthor", new { id = linkedResourceToReturn["Id"] }, linkedResourceToReturn)); }
public IActionResult CreateAuthorCollection([FromBody] IEnumerable <AuthorCreationDto> 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("Creation of author collection failed"); } var authorCollectionToReturn = Mapper.Map <IEnumerable <AuthorDto> >(authorEntities); var idsAsString = string.Join(",", authorCollectionToReturn.Select(a => a.Id)); return(CreatedAtRoute("GetAuthorCollection", new { ids = idsAsString }, authorCollectionToReturn)); }