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.
        }
示例#2
0
        public IActionResult CreateBooksForAuthor(Guid authorId, [FromBody] CreateBookDto book)
        {
            if (book == null)
            {
                return(NotFound());
            }
            if (string.Equals(book.Description, book.Description, StringComparison.InvariantCultureIgnoreCase))
            {
                ModelState.AddModelError(nameof(CreateBookDto), "The provider description should be different from the title");
            }
            if (!ModelState.IsValid)
            {
                return(new Helpers.UnprocessableEntityObjectResult(ModelState));
            }
            if (!libraryRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }
            var bookEntity = Mapper.Map <Book>(book);

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

            return(CreatedAtRoute("GetBookForAuthor", new { authorId, id = bookToReturn.Id }, bookToReturn));
        }
        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));
        }
示例#4
0
        public IActionResult CreateBookForAuthor(Guid authorId, [FromBody] BookForCreationDto book)
        {
            if (book == null)
            {
                return(BadRequest());
            }
            if (book.Description == book.Title)
            {
                ModelState.AddModelError(nameof(BookForCreationDto), "The provided description should be different from the title.");
            }
            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }
            if (!_libraryRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }
            var bookEntity = Mapper.Map <Book>(book);

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

            return(CreatedAtRoute("GetBookFroAuthor", new { authorId, id = bookToReturn.Id }, CreateLinksForBook(bookToReturn)));
        }
示例#5
0
        public IActionResult Post(Guid authorId, [FromBody] BookForCreationDto book)
        {
            if (book == null)
            {
                return(BadRequest( ));
            }
            if (book.Title == book.Description)
            {
                ModelState.AddModelError(nameof(BookForCreationDto), "Title & Description should be different.");
            }
            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }
            if (!_repo.AuthorExists(authorId))
            {
                return(BadRequest( ));
            }

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

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

            return(CreatedAtRoute("GetBookForAuthor",
                                  new { authorId = authorId, id = bookEntity.Id }, CreateLinksForBook(bookToReturn)));
        }
示例#6
0
        public IActionResult CreateBookForAuthor(Guid authorId, [FromBody] BookForCreationDto book)
        {
            if (book == null)
            {
                return(BadRequest());
            }
            //custom validation logic
            if (book.Description == book.Title)
            {
                ModelState.AddModelError(nameof(BookForCreationDto), "Descript and Title must be different");
            }
            //validation
            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState)); //422
            }
            if (!_libraryRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }

            var bookEntity = book.ConvertToBookEntity();

            _libraryRepository.AddBookForAuthor(authorId, bookEntity);
            _libraryRepository.Save();
            var bookToReturn = bookEntity.ConvertToBookDto();

            //return CreatedAtRoute("GetBook", new {authorId = bookToReturn.AuthorId, id = bookToReturn.Id }, bookToReturn);
            return(CreatedAtRoute("GetBook", new { authorId = bookToReturn.AuthorId, id = bookToReturn.Id }, CreateLinksForBook(bookToReturn)));
        }
        public IActionResult CreateBookForAuthor(Guid authorId, [FromBody] BookForCreationDto book)
        {
            if (book == null)
            {
                return(BadRequest());
            }
            // Add custom rule and add that to the model state
            if (book.Title == book.Description)
            {
                ModelState.AddModelError(nameof(BookForCreationDto), "The description should be different from the book title.");
            }
            if (!ModelState.IsValid)
            {
                // retrun 422[Un-Processable entity]
                return(new UnProcessableEntityObjectResult(ModelState));
            }
            if (!_libraryRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }
            var bookEntity = _mapper.Map <Book>(book);

            _libraryRepository.AddBookForAuthor(authorId, bookEntity);
            if (!_libraryRepository.Save())
            {
                return(StatusCode(500, "Error Occurred while adding the book for the author."));
            }
            var bookToReturn = _mapper.Map <BookDto>(bookEntity);

            return(CreatedAtRoute("GetBookForAuthor", new { authorId = bookToReturn.AuthorId, id = bookToReturn.Id }, CreateLinksForBook(bookToReturn)));
        }
示例#8
0
        public IActionResult CreateBookForAuthor(Guid authorId, [FromBody] BookForCreationDto book)
        {
            if (book == null)
            {
                return(BadRequest());
            }

            CheckForAdditionalModelValidationsForBook(book);
            if (!ModelState.IsValid)
            {
                return(new UnprocessibleEntityObjectResult(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 faild at saving to database.");
            }

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

            return(CreatedAtRoute("GetBookForAuthor", new { authorId = authorId, bookId = bookToReturn.Id }, bookToReturn));
        }
示例#9
0
        public IActionResult CreateBookForAuthor(Guid authorId, [FromBody] BookForCreationDto dto)
        {
            if (dto == null)
            {
                return(BadRequest());
            }

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

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

            libraryRepository.AddBookForAuthor(authorId, bookEntity);

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

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

            return(CreatedAtAction(
                       nameof(GetBookForAuthor),
                       new { authorId, id = bookToReturn.Id },
                       bookToReturn
                       ));
        }
示例#10
0
        public IActionResult CreateBookForAuthor(Guid authorId, [FromBody] BookForCreationDto book)
        {
            if (book == null)
            {
                return(BadRequest());
            }

            if (book.Description == book.Title)
            {
                ModelState.AddModelError(nameof(BookForCreationDto),
                                         "Description debe ser diferente de Title");
            }

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

            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 {authorId} failed on save");
            }

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

            return(CreatedAtRoute("GetBookForAuthor", new { id = bookToReturn.Id }, bookToReturn));
        }
示例#11
0
        public IActionResult CreateBookForAuthor(Guid authorId, [FromBody] CreateBookDto createBookDto)
        {
            if (createBookDto == null)
            {
                return(BadRequest());
            }

            if (string.Equals(createBookDto.Title, createBookDto.Description))
            {
                ModelState.AddModelError(nameof(CreateBookDto), "The title cannot be the same as the description");
            }

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

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

            var book = createBookDto.ToBook(authorId);

            _libraryRepository.AddBookForAuthor(authorId, book);
            _libraryRepository.Save();

            var bookDto = new BookDto(book);

            bookDto = CreateLinksForBook(bookDto);
            return(CreatedAtRoute(nameof(GetBookForAuthor), new { authorId, id = book.Id }, bookDto));
        }
示例#12
0
        public ActionResult CreateBookForAuthor(Guid authorId, [FromBody] BookForCreationDto book)
        {
            if (book == null)
            {
                return(BadRequest());
            }

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

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

            _libraryRepository.AddBookForAuthor(authorId, bookEntity);

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

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

            return(CreatedAtRoute("GetBookForAuthor", new { authorId, id = bookToReturn.Id }, bookToReturn));
        }
示例#13
0
        public IActionResult CreateBookForAuthor(Guid authorId, [FromBody] Models.BookForCreation book)
        {
            if (book == null)
            {
                return(BadRequest());
            }

            if (book.Title == book.Description)
            {
                ModelState.AddModelError(nameof(Models.BookForCreation), "The title and the description must be different");
            }

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

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

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

            libraryRepository.AddBookForAuthor(authorId, bookCreated);
            if (!libraryRepository.Save())
            {
                throw new Exception("Creating book failed on save");
            }

            var bookToReturn = Mapper.Map <Models.Book>(bookCreated);

            return(CreatedAtRoute("GetBookForAuthor", new { authorId = authorId, bookId = bookToReturn.Id }, bookToReturn));
        }
示例#14
0
        public IActionResult CreateBookForAuthor(Guid authorID, [FromBody] BookCreationDto book)
        {
            if (book == null)
            {
                return(BadRequest());
            }

            if (!libraryRepository.AuthorExists(authorID))
            {
                return(NotFound());
            }

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

            libraryRepository.AddBookForAuthor(authorID, bookEntity);
            if (!libraryRepository.Save())
            {
                throw new Exception("Error !!!");
            }

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

            // below line of code provide location URL in response body for newly created resource
            return(CreatedAtRoute("GetBookForAuthor", new { authorID = authorID, bookID = bookDto.Id }, bookDto));
        }
示例#15
0
        public IActionResult CreateBookForAuthor(Guid authorId, [FromBody] BookForCreationDto book)
        {
            if (book == null)
            {
                return(BadRequest());
            }

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

            if (!_libraryRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }
            Book bookEntity = Mapper.Map <Book>(book);

            _libraryRepository.AddBookForAuthor(authorId, bookEntity);

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

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

            return(CreatedAtRoute("GetBookForAuthor", new { authorId = authorId, id = bookToReturn.Id }, CreateLinksForBook(bookToReturn)));
        }
示例#16
0
        public ActionResult CreateBookForAuthor(Guid authorid, [FromBody]  BookforCreationDto book)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
                if (!_libraryRepository.AuthorExists(authorid))
                {
                    return(NotFound());
                }
                var booketitiy = Mapper.Map <BookforCreationDto, Book>(book);
                _libraryRepository.AddBookForAuthor(authorid, booketitiy);

                if (!_libraryRepository.Save())
                {
                    throw new Exception($"Failed to Add the Book{book.Title} for {authorid}");
                }
                return(CreatedAtRoute("GetBookByAuthor", new { authorid = authorid, bookId = booketitiy.Id }, booketitiy));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
示例#17
0
        public IActionResult AddBookForAuthor(Guid authorId, [FromBody] CreateBookDto createBookDto)
        {
            if (createBookDto == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(UnprocessableEntity(ModelState));
            }

            var book = new Book
            {
                Id          = Guid.NewGuid(),
                AuthorId    = authorId,
                Title       = createBookDto.Title,
                Description = createBookDto.Description
            };

            _libraryRepository.AddBookForAuthor(authorId, book);
            _libraryRepository.Save();
            var bookForAuthorDto = _mapper.Map <BookDto>(book);

            return(CreatedAtAction("GetBookForAuthor", new { authorId, bookId = book.Id }, bookForAuthorDto));
        }
示例#18
0
        public IActionResult CreateBookForAuthor(Guid authorId,
                                                 [FromBody] BookForCreationDto bookDto)
        {
            if (bookDto == null)
            {
                return(BadRequest());
            }

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

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

            var entity = Mapper.Map <Book>(bookDto);

            _repository.AddBookForAuthor(authorId, entity);

            if (!_repository.Save())
            {
                throw new Exception("Failed on creating this book.");
            }

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

            return(CreatedAtRoute("GetBook",
                                  new { authorId = bookToReturn.AuthorId, id = bookToReturn.Id },
                                  CreateLinksForBook(bookToReturn)));
        }
示例#19
0
        public IActionResult CreateBookForAuthor(Guid authorId,
                                                 [FromBody] BookForCreationDto book)
        {
            if (book == null)
            {
                return(BadRequest());
            }

            if (book.Description == book.Title)
            {
                //  NAME of the property to be sent in headers as Key value pair
                ModelState.AddModelError(nameof(BookForCreationDto),
                                         "The provided description should be different from the title.");
                //  VALUE of the property to be sent in headers as Key value pair
            }

            if (!ModelState.IsValid)
            {
                // return 422
                // Custom Error Handler => returns the Key Value pair Error of the specific property
                //( e.g. Title, Description ) which causes error    =>  Below is example

                /*
                 *  {
                 *      "title": [
                 *          "The title shouldn't have more than 100 characters."
                 *      ],
                 *      "description": [
                 *          "The description shouldn't have more than 500 characters."
                 *      ],
                 *      "bookForCreationDto": [
                 *          "The provided description should be different from the title."
                 *      ]
                 *  }
                 */
                return(new UnprocessableEntityObjectResult(ModelState));
            }

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

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

            _libraryRepository.AddBookForAuthor(authorId, bookEntity);

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

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

            return(CreatedAtRoute("GetBookForAuthor",
                                  new { authorId = authorId, id = bookToReturn.Id },
                                  //  CreateLinksForBooks => Supporting HATEOAS (Base and Wrapper Class Approach)
                                  CreateLinksForBook(bookToReturn)));
        }
示例#20
0
        public IActionResult CreateBookForAuthor(Guid authorId, [FromBody] BookForCreationDto book, [FromHeader(Name = "Accept")] string mediaType)
        {
            if (book is null)
            {
                return(BadRequest());
            }

            if (book.Description == book.Title)
            {
                ModelState.AddModelError(nameof(BookForCreationDto), "description can not be the same as title");
            }

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

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

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

            _libraryRepository.AddBookForAuthor(authorId, bookEntity);

            if (!_libraryRepository.Save())
            {
                throw new Exception($"book fails in save for author {authorId}");
            }

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

            if (mediaType == "application/vnd.marvin.hateoas+json")
            {
                var links = CreateLinksForBook(bookToReturn.Id);
                var linkedResourceToReturn = bookToReturn.ShapeData(null) as IDictionary <string, object>;

                linkedResourceToReturn.Add("links", links);

                return(CreatedAtRoute("GetBookForAuthor",
                                      new { authorId = authorId, id = bookToReturn.Id },
                                      linkedResourceToReturn));
            }
            else
            {
                return(CreatedAtRoute("GetBookForAuthor",
                                      new { authorId = authorId, id = bookToReturn.Id },
                                      bookToReturn));
            }
        }
        public async Task <IActionResult> CreateBookForAuthor(Guid authorId,
                                                              [FromBody] BookForCreationDto book)
        {
            if (book == null)
            {
                return(BadRequest()); // 400
            }
            // Validation
            ////////////////////////////
            if (book.Description == book.Title)
            {
                // Our custom code validation should go after a bad request,
                // and before we check for IsValid and process. If the tile
                // and description are the same, we add a model error.
                ModelState.AddModelError(nameof(BookForCreationDto),
                                         "The Provided description should be different from the title");
            }

            // We check if our ModelState IsValid, for example if any of the requirements
            // estabilished in our BookForCreationDto in the form of annotations fail,
            // the state of IsValid will be false. We pass our model to our ObjectResult
            // constructor and the rest is handled by the framework.
            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState)); // 422
            }
            ////////////////////////////

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

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

            await libraryRepository.AddBookForAuthor(authorId, bookForAuthorEntity);

            if (!(await libraryRepository.Save()))
            {
                throw new Exception("Failed to save author. Try again.");
            }

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

            return(CreatedAtRoute("GetBookForAuthor",
                                  new { authorId = authorId, id = bookToReturn.Id },
                                  CreateLinksForBook(bookToReturn)));
        }
示例#22
0
        public IActionResult CreateBookForAuthor(Guid authorId, [FromBody] BookForCreationDto book)
        {
            if (book == null)
            {
                return(BadRequest());
            }

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

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

            _libraryRepository.AddBookForAuthor(authorId, bookEntity);

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

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

            //Notice the bookToReturn (newly created book) at the end. It is required as this is
            //going to be seralised into request body
            return(CreatedAtRoute("GetBookForAuthor",
                                  new { authorId = authorId, id = bookToReturn.Id }, bookToReturn));
        }
示例#23
0
        public IActionResult CreateBookForAuthor(Guid authorId,
                                                 [FromBody] BookForCreationDto book)
        {
            if (book == null)
            {
                return(BadRequest());
            }

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

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

            _libraryRepository.AddBookForAuthor(authorId, bookEntity);

            if (!_libraryRepository.Save())
            {
                throw new Exception($"Fallo la creacion del libreo para el autor {authorId} durante la grabación");
            }

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

            return(CreatedAtRoute("GetBookForAuthor",
                                  new { authorId = authorId, id = bookToReturn.Id },
                                  bookToReturn));
        }
示例#24
0
        public IActionResult CreateBookForAuthor(Guid authorId, [FromBody] BookCreateResourceDTO bookResourceDTO)
        {
            if (bookResourceDTO == null)
            {
                return(BadRequest());
            }

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

            var bookEntity = _mapper.Map <Book>(bookResourceDTO);

            _libraryRepository.AddBookForAuthor(authorId, bookEntity);

            if (!_libraryRepository.Save())
            {
                throw new Exception($"Creating a book for author {authorId} failed on save");
                //return StatusCode(500, "A problem happened with handling your request.")
            }

            var book = _mapper.Map <BookDTO>(bookEntity);

            return(CreatedAtRoute("GetBookForAuthor", new { authorId = authorId, bookId = book.Id }, book));
        }
示例#25
0
        public IActionResult Post(Guid authorId, [FromBody] BookPostModel model)
        {
            if (model == null)
            {
                return(BadRequest());
            }

            if (model.Title == model.Description)
            {
                ModelState.AddModelError(nameof(AuthorBooksController), "Title cannot equal description");
            }

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

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

            _rpeo.AddBookForAuthor(authorId, model.GetEntity());

            _rpeo.Save();

            return(CreatedAtRoute("GetAuthorBook",
                                  new { authorId, id = model.Id },
                                  new BookGetModel(model.GetEntity())
                                  ));
        }
示例#26
0
        public IActionResult CreateBookCollection(Guid authorId,
                                                  [FromBody] IEnumerable <BookForCreationDto> bookCollection)
        {
            if (!_libraryRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }

            if (bookCollection == null)
            {
                return(BadRequest());
            }

            var booksForDb = Mapper.Map <IEnumerable <Book> >(bookCollection);

            foreach (var book in booksForDb)
            {
                _libraryRepository.AddBookForAuthor(authorId, book);
            }

            if (!_libraryRepository.Save())
            {
                throw new Exception();
            }

            var bookCollectionToReturn = Mapper.Map <IEnumerable <BookDto> >(booksForDb);
            var routeIds = string.Join(',', bookCollectionToReturn.Select(a => a.Id));

            return(CreatedAtRoute("GetBooksForAuthorCollection",
                                  new { bookIds = routeIds },
                                  bookCollectionToReturn));
        }
示例#27
0
        public IActionResult CreateBookForAuthor(Guid authorId, [FromBody] BookForCreateDto book)
        {
            if (book == null)
            {
                return(BadRequest());
            }

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

            book.AuthorId = authorId;
            if (!_libraryRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }
            var bookEntity = Mapper.Map <Book>(book);

            _libraryRepository.AddBookForAuthor(authorId, bookEntity);
            if (!_libraryRepository.Save())
            {
                return(StatusCode(500, "A problem when add book"));
            }
            var bookToReturn = Mapper.Map <BookDto>(bookEntity);

            return(CreatedAtRoute("GetBook", new { authorId = book.AuthorId, bookId = bookToReturn.Id }, bookToReturn));
        }
示例#28
0
        public IActionResult CreateBookForAuthor(Guid authorId, [FromBody] BookForCreationDto book)
        {
            if (book == null)
            {
                return(BadRequest());
            }

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

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

            _libraryRepository.AddBookForAuthor(authorId, bookEntity);

            if (!_libraryRepository.Save())
            {
                throw new Exception($"Fail to create book for {authorId}");
            }

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

            // create a uri in returned header, the parameters must be exact with GetBookByAuthor
            // otherwise it won't work
            return(CreatedAtRoute("GetBook4Author", new { authorId = authorId, bookId = bookToReturn.Id }, bookToReturn));
        }
示例#29
0
        public IActionResult CreateBookForAuthor(
            Guid authorId,
            [FromBody] BookForCreationDto book)
        {
            if (book == null)
            {
                return(BadRequest());
            }

            // The whole validation approach used relies on; models, data annotations, and rules in a way which
            // leads to duplication of code and merging of concerns. JeremySkinner's FluentValidation is worth a look.

            if (book.Title == book.Description)
            {
                ModelState.AddModelError(nameof(BookForCreationDto), "Please enter a proper description for the book.");
            }

            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));                // Custom ObjectResult for 422
            }
            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 for author {authorId} failed on save.");
            }

            //return CreatedAtRoute(
            //	routeName: "GetBookForAuthor",
            //	routeValues: new { authorId = authorId, id = bookEntity.Id },
            //	value: AutoMapper.Mapper.Map<BookDto>(bookEntity));

            // Support HATEOAS using the statically typed approach by including links in response
            return(CreatedAtRoute(
                       routeName: "GetBookForAuthor",
                       routeValues: new { authorId = authorId, id = bookEntity.Id },
                       value: CreateLinksForBook(AutoMapper.Mapper.Map <BookDto>(bookEntity))));
        }
        public IActionResult CreateBookForAuthor(Guid authorId, [FromBody] CreateBook book,
                                                 [FromHeader(Name = Headers.Accept)] string mediaType)
        {
            if (book == null)
            {
                return(BadRequest());
            }

            if (book.Description == book.Title)
            {
                ModelState.AddModelError(nameof(CreateBook), "The description should differ from the title.");
            }
            if (!ModelState.IsValid)
            {
                return(UnprocessableEntity(ModelState));
            }

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

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

            _libraryRepository.AddBookForAuthor(authorId, newBook);

            if (!_libraryRepository.Save())
            {
                throw new Exception($"Couldn't save new book for Author {authorId}");
            }

            var currentMediaType = new MediaType(mediaType);
            var includeLinks     = currentMediaType.IsSubsetOf(VendorMediaType.HateoasLinksMediaType);

            var bookToReturn = Mapper.Map <Book>(newBook);

            if (includeLinks)
            {
                return(CreatedAtRoute(nameof(GetBookForAuthor), new { authorId, id = newBook.Id }, CreateLinks(bookToReturn)));
            }
            else
            {
                return(CreatedAtRoute(nameof(GetBookForAuthor), new { authorId, id = newBook.Id }, bookToReturn));
            }
        }