Exemplo n.º 1
0
                                               "application/vnd.tmaturano.authorwithdateofdeath.full+xml" })] //should only accept requests with these media type
        public IActionResult CreateAuthorWithDateOfDeath([FromBody] AuthorInputWithDateOfDeathDto authorDto)
        {
            if (authorDto == null)
            {
                return(BadRequest());
            }

            var result = _authorService.Add(authorDto);

            if (!result.sucess)
            {
                //Throwing an exception is expensive, but at this case, we have at the global level on Startup handling all the
                //500 Error, so to keep it in one place, I'm throwing .
                throw new Exception("Creating an author failed on save.");
                //return StatusCode(500, "");
            }

            var authorToReturn = _mapper.Map <AuthorOutputDto>(authorDto);

            authorToReturn.Id = result.id;

            //CreatedAtRoute will contain the URI where the newly author can be found 1st parameter
            //also, the id of the generated author in 2nd parameter
            //The URI is sent through response's header Location
            return(CreatedAtRoute("GetAuthor",
                                  new { id = authorToReturn.Id },
                                  authorToReturn));
        }
Exemplo n.º 2
0
        public (bool sucess, Guid id) Add(AuthorInputWithDateOfDeathDto obj)
        {
            try
            {
                var author = _mapper.Map <AuthorInputWithDateOfDeathDto, Author>(obj);
                author.SetDateOfDeath(obj.DateOfDeath.Value);
                _authorService.Add(author);

                return(unitOfWork.Commit(), author.Id);
            }
            catch (Exception)
            {
                throw;
            }
        }