Exemple #1
0
        public IHttpActionResult CreateBook([FromBody] Book book)
        {
            if (book == null)
            {
                return(BadRequest("Content is empty"));
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (GetBookListIndex(book.Id) != booksContext.InvalidListIndex)
            {
                return(BadRequest($"Book with Id={book.Id} already created."));
            }
            book.ISBN = Validator_ISBN.CorrectISBN(book.ISBN);
            booksContext.Books.Add(book);

            return(Ok());
        }
Exemple #2
0
        public IHttpActionResult EditBook(int id, [FromBody] Book book)
        {
            if (book == null)
            {
                return(BadRequest("Content is empty"));
            }
            if (id != book.Id)
            {
                return(BadRequest($"Target Id={id} does not equal book's Id={book.Id}."));
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            book.ISBN = Validator_ISBN.CorrectISBN(book.ISBN);
            var listIndex = GetBookListIndex(id);

            if (listIndex == booksContext.InvalidListIndex)
            {
                return(BadRequest(GetNotFoundErrorText(id)));
            }
            booksContext.Books[listIndex] = book;
            return(Ok());
        }