Example #1
0
        public IActionResult AddAuthor([FromBody] AuthorForAddDto authorForAdd)
        {
            if (authorForAdd == null)
            {
                return(BadRequest());  // Did not deserialize properly.
            }
            var authorEntity = Mapper.Map <Author>(authorForAdd);

            Respository.AddAuthor(authorEntity);

            if (!Respository.Save())
            {
                throw new Exception("Failed to create the author.");  // Throw exception so the middleware handler does all of the error handling.
            }
            var authorToReturn = Mapper.Map <AuthorDto>(authorEntity);

            return(CreatedAtRoute("GetAuthor", new { id = authorToReturn.Id }, authorToReturn));   // Puts Location http://localhost:5000/api/Authors/53 in header
        }
        public IActionResult AddAuthorCollection([FromBody] IEnumerable <AuthorForAddDto> authorCollection)
        {
            if (authorCollection == null)
            {
                return(BadRequest());  // Did not deserialize properly.
            }
            var authorEntities = Mapper.Map <IEnumerable <Author> >(authorCollection);

            foreach (var author in authorEntities)
            {
                Respository.AddAuthor(author);
            }

            if (!Respository.Save())
            {
                throw new Exception("Failed to create the author collection.");  // Throw exception so the middleware handler does all of the error handling.
            }
            var authorCollectionToReturn = Mapper.Map <IEnumerable <AuthorDto> >(authorEntities);
            var idsAsString = string.Join(",", authorEntities.Select(a => a.Id.ToString()));

            return(CreatedAtRoute("GetAuthorCollectionX", new { ids = idsAsString }, authorCollectionToReturn));   // Puts Location http://localhost:5000/api/Authors/53 in header
        }