7 API Books
kolaente edited this page 2018-01-29 16:00:23 +01:00

[API] Books

GET /books

Returns all books as an array.

Parameters

s: [optional] Searchstring. If provided, returns only those books whose title match the search term.

Response

200 All found books.
500 A server error occured.

Example

Request:

curl http://localhost:8082/api/v1/books

Result:

[
  {
    "id": 54,
    "title": "Test with quantity",
    "description": "Lorem Impsum dolor sit amet.",
    "isbn": "124-456-789-126",
    "year": 2017,
    "price": 9.99,
    "status": 2,
    "publisherID": 16,
    "created": 1510915863,
    "updated": 1511970163,
    "quantity": 98,
    "publisher": {
      "id": 16,
      "name": "Fantasy Publishing LTD.",
      "created": 1507747610,
      "updated": 1507747610
    },
    "authors": [
      {
        "id": 1,
        "forename": "John",
        "lastname": "Doe",
        "created": 1511968500,
        "updated": 1511968500
      },
      {
        "id": 3,
        "forename": "Jane",
        "lastname": "Doe",
        "created": 1507735006,
        "updated": 1507746120
      }
    ]
  },
  {
    "id": 66,
    "title": "Loremk",
    "description": "",
    "isbn": "",
    "year": 2017,
    "price": 5.99,
    "status": 0,
    "publisherID": 1,
    "created": 1511260700,
    "updated": 1512551025,
    "quantity": 4,
    "publisher": {
      "id": 1,
      "name": "møtus",
      "created": 0,
      "updated": 1511968730
    },
    "authors": null
  }
]

GET /books/:id

Returns a JSON-Object with all informations about a single book.

Parameters

id: The ID of the book you want to retrieve.

Response

200 The book.
400 ID is invalid or empty.
404 The book does not exist.
500 A server error occured.


PUT /books 🔒

Creates a new book.

Inside the book object, you can either pass a publisher's ID or a publisher object. If the publisher does not exist, it will be created. Same for authors.

Payload

Expects a JSON book object. Either directly as a JSON-object see the example below or as a string via the form value book. When sending as JSON, you need to include the Content-Type: application/json;charset=utf-8-Header.

Response

200 The book was successfully inserted. Returns the newly created book.
400 No book payload was provided or the title is empty. Returns a more specific message.
500 A server error occured.

Example payload

In this example, the publisher does alredy exist, it is only linked to the new book. One author with the `ID` 1 also does already exist, the other one is newly created.

{
  "book": {
    "title": "Test",
    "isbn": "978-2-36011-001-8",
    "year": 2015,
    "price": 9.99,
    "status": 1,
    "quantity": 11,
    "publisher": {
      "id": 1,
      "name": "Fantasy Publishing Ltd."
    },
    "authors": [
      {
        "id": 1
      },
      {
        "forename": "Test",
        "lastname": "Author"
      }
    ]
  }
}

Example

Request:

curl \ 
-H 'Authorization: Bearer <YOUR TOKEN HERE>' \
-X PUT \
-d book='{"title": "Test","isbn": "978-2-36011-001-8","year": 2015,"price": 9.99,"status": 1,"quantity": 11,"publisher": {"id": 1,"name": "Baum"},"authors":[{"id": 1},{"forename": "Test","lastname": "Author"}]}' \
http://localhost:8082/api/v1/books

Response:

{
  "id": 110,
  "title": "Test",
  "description": "",
  "isbn": "978-2-36011-001-8",
  "year": 2015,
  "price": 9.99,
  "status": 1,
  "publisherID": 1,
  "created": 1512557017,
  "updated": 1512557017,
  "quantity": 11,
  "publisher": {
    "id": 1,
    "name": "møtus",
    "created": 0,
    "updated": 1511968730
  },
  "authors": [
    {
      "id": 1,
      "forename": "Françoisc",
      "lastname": "David",
      "created": 1511968500,
      "updated": 1511968500
    },
    {
      "id": 2406,
      "forename": "Test",
      "lastname": "Author",
      "created": 1512557017,
      "updated": 1512557017
    }
  ]
}

DELETE /books/:id 🔒

Deletes a book by its ID.

Parameters

ID: The ID of the book you want to delete.

Response

200 The book was successfully deleted.
400 ID is invalid or empty.
404 No book with this ID exists.
500 A server error occured.

Example

Request:

curl \ 
-H 'Authorization: Bearer <YOUR TOKEN HERE>' \
-X DELETE \
http://localhost:8082/api/v1/books/111

Response:

{"message":"success"}

POST /books/:id 🔒

Updates a book by its ID. Returns the newly updated book.

Parameters

ID: The ID of the book you want to update.

Payload

Expects a JSON book object see inserting a book.

You need to provide a quantity, otherwise the quantity is set to 0! This is because the send JSON values are interpreted as an object to be able to deal with it internally. Any not-given value is interpreted to its standard `""` for strings, `0` for integers.

Response

200 The book was successfully deleted.
400 Book payload is empty, not valid orID is invalid. Returns a more specific message.
404 No book with this ID exists.
500 A server error occured.

Example

This example will update only the title.

Request:

curl \ 
-H 'Authorization: Bearer <YOUR TOKEN HERE>' \
-X POST \
-d book='{"title": "Test with lorem"}' \
http://localhost:8082/api/v1/books/110

Response:

{
  "id": 110,
  "title": "Test with lorem",
  "description": "",
  "isbn": "978-2-36011-001-8",
  "year": 2015,
  "price": 9.99,
  "status": 1,
  "publisherID": 1,
  "created": 1512557017,
  "updated": 1512558582,
  "quantity": 0,
  "publisher": {
    "id": 1,
    "name": "møtus",
    "created": 0,
    "updated": 1511968730
  },
  "authors": [
    {
      "id": 1,
      "forename": "Françoisc",
      "lastname": "David",
      "created": 1511968500,
      "updated": 1511968500
    },
    {
      "id": 2406,
      "forename": "Test",
      "lastname": "Author",
      "created": 1512557017,
      "updated": 1512557017
    }
  ]
}