コード例 #1
0
        public IActionResult CreateAuthor([FromBody] AuthorForCreationDto author)
        {
            if (author.IsNull())
            {
                return(BadRequest());
            }

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

            _libraryRepository.AddAuthor(authorEntity);

            if (!_libraryRepository.Save())
            {
                return(StatusCode(500, "A problem happend with handling your request."));
            }

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

            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));
        }
コード例 #2
0
        public IActionResult CreateAuthor([FromBody] AuthorForCreationDto author)
        {
            if (author == null)
            {
                return(BadRequest());
            }

            var authorEntity = author.Map();

            _libraryRepository.AddAuthor(authorEntity);

            if (!_libraryRepository.Save())
            {
                throw new Exception("Creating an author failed on save.");
            }

            var authorToReturn = authorEntity.Map();

            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));
        }