public IActionResult CreateBooksForAuthors(Guid id, [FromBody] BooksForCreationDto books)
        {
            if (books == null)
            {
                return(BadRequest());
            }

            if (!_libraryrepository.AuthorExist(id))
            {
                return(NotFound());
            }

            var bookentity = Mapper.Map <Books>(books);

            _libraryrepository.AddBookForAuthor(id, bookentity);

            if (!_libraryrepository.Save())
            {
                return(StatusCode(500, "Error al guardar en la base de datos"));
            }

            var bookss = Mapper.Map <BookDto>(bookentity);

            return(CreatedAtRoute("GetBookForAuthor", new { authorid = bookss.Id }, books));
        }
        public IActionResult CreateBookForAuthor(Guid authorId, [FromBody] BooksForCreationDto book)
        {
            if (book == null)
            {
                return(BadRequest());
            }
            if (book.Description == book.Title)
            {
                //If title and description match we add a model error to the state
                //In a kvp format.
                ModelState.AddModelError(nameof(BooksForCreationDto), "The provided description should be different from the title.");
            }
            if (!ModelState.IsValid)                                     //If model state is not valid, this will happen for exanple, if the title is null since the BookForCreationDto has the attribute required on the title.
            {
                return(new UnprocessableEntityObjectResult(ModelState)); //We do this because there is no helper method in the controller to defin a 422 error, ex like Ok = 200 code.
            }
            if (!_libraryRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }


            var bookEntity = AutoMapper.Mapper.Map <Entities.Book>(book);

            _libraryRepository.AddBookForAuthor(authorId, bookEntity);
            if (!_libraryRepository.Save())
            {
                throw new Exception($"Creating a book for author {authorId} failed on save");
            }
            var bookToReturn = AutoMapper.Mapper.Map <BookDto>(bookEntity);

            return(CreatedAtRoute("GetBookForAuthor", new { authorId = authorId, id = bookToReturn.Id }, bookToReturn)); //2nd parameter is called an anonymous object.
        }
Exemple #3
0
        public IActionResult UpdateBook(int authorId, int id, [FromBody] JsonPatchDocument <BooksForCreationDto> patchDocument)
        {
            if (patchDocument == null)
            {
                return(BadRequest());
            }

            var author = AuthorsDatastore.Current.Authors.FirstOrDefault(a => a.Id == authorId);
            {
                if (author == null)
                {
                    return(NotFound());
                }
            }

            var book = author.Books.FirstOrDefault(a => a.Id == id);
            {
                if (book == null)
                {
                    return(NotFound());
                }
            }


            var bookToPatch = new BooksForCreationDto()
            {
                Name        = book.Name,
                Description = book.Description
            };

            patchDocument.ApplyTo(bookToPatch, ModelState);


            TryValidateModel(bookToPatch);

            //Is the model in a valid state
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }


            book.Name        = bookToPatch.Name;
            book.Description = bookToPatch.Description;

            return(NoContent());
        }
Exemple #4
0
        public IActionResult CreateBook(int authorId, [FromBody] BooksForCreationDto book)
        {
            //Validations
            if (book == null)
            {
                return(BadRequest());
            }

            if (book.Name == book.Description)
            {
                ModelState.AddModelError("Description", "The book name and the description cannot be the same");
            }

            //Is the model in a valid state
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var author = AuthorsDatastore.Current.Authors.FirstOrDefault(c => c.Id == authorId);

            if (author == null)
            {
                return(NotFound("Author was not found"));
            }

            //Demo Purpose will be improved
            //Get Max Book Id
            var maxBookId = AuthorsDatastore.Current.Authors.SelectMany(a => a.Books).Max(b => b.Id);
            //The above method is slow
            //The above method does not take into account scenarios where multiple users will try to get an ID simultaneously


            var newBook = new BooksDto()
            {
                Id          = ++maxBookId,
                Name        = book.Name,
                Description = book.Description
            };

            author.Books.Add(newBook);

            //return Ok(author.Books);

            //For Post the advised response is 201 Created
            return(CreatedAtRoute("GetbookByAuthorId", new { authorId = authorId, bookid = newBook.Id }, newBook));
        }
        public IActionResult CreateBookForAuthor(Guid authorId, [FromBody] BooksForCreationDto book)
        {
            if (book == null)
            {
                return(BadRequest());
            }

            if (book.Description == book.Title)
            {
                ModelState.AddModelError(nameof(BooksForCreationDto),
                                         "The provided description should be different from the title.");
            }

            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            if (!_libraryRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }

            var bookEntity = AutoMapper.Mapper.Map <Book>(book);

            _libraryRepository.AddBookForAuthor(authorId, bookEntity);

            if (!_libraryRepository.Save())
            {
                throw new Exception($"Creating a book fot author {authorId} failed on save");
            }

            var bookToReturn = AutoMapper.Mapper.Map <BookDto>(bookEntity);

            return(CreatedAtRoute("GetBookForAuthor", new { authorId, id = bookToReturn.Id }, CreateLinksForBook(bookToReturn)));
        }