This solution implements a CRUD REST API for books with a local database. Additionally, an API that calls an external service (ice and fire API) to get information about books is also implemented. All implementations are backed with tests.
- Clone the project:
git clone https://github.com/atunjeafolabi/estate-intel-book-api.git - Create a mysql database named
estate_intel - Rename
.env.exampleto.envand fill it with the database credentials (username and password) - From the project root directory, run
composer install - Run migrations
php artisan migrate - Run local dev server:
php artisan serve
The following endpoints are available and can easily be accessed as follows:
GET api/v1/books
curl -i -H 'Accept: application/json' http://localhost:8000/api/v1/books
{
"data":[
{
"id":1,
"name":"A Game of Thrones",
"isbn":"978-0553103540",
"authors":[
"George R. R. Martin"
],
"number_of_pages":694,
"publisher":"Bantam Books",
"country":"United States",
"release_date":"1996-08-01"
},
{
"id":2,
"name":"A Clash of Kings",
"authors":[
"George R. R. Martin"
],
"number_of_pages":768,
"publisher":"Bantam Books",
"country":"United States",
"release_date":"1999-02-02"
}
],
"status_code":200,
"status":"success"
}
If no books were found, the JSON below is returned;
{
"status_code":200,
"status":"success",
"data":[]
}
A book can be searched by a name, country, publisher or release date as follows:
api/v1/books?name=Essential Biology
api/v1/books?country=Nigeria
api/v1/books?publisher=Oxford Publishers
api/v1/books?release_date=1999-01-01
GET api/v1/books/id
curl -i -H 'Accept: application/json' http://localhost:8000/api/v1/books/25
{
"data":{
"id":1,
"name":"My First Book",
"isbn":"123-3213243567",
"authors":[
"John Doe"
],
"number_of_pages":350,
"publisher":"Acme Books Publishing",
"country":"United States",
"release_date":"2019-01-01"
},
"status_code":200,
"status":"success"
}
POST api/v1/books
curl -X POST -H "Content-Type: application/json" -d '{
"name": "Wonders of Astronomy",
"isbn": "999123-3213243567",
"authors": [
"Galileo Galileo",
"Adrian"
],
"number_of_pages": 350,
"publisher": "Oxford Publishers",
"country": "England",
"release_date": "1975-08-01"
}' http://localhost:8000/api/v1/books
{
"data":{
"name": "Wonders of Astronomy",
"isbn": "999123-3213243567",
"authors": [
"Galileo Galileo",
"Adrian"
],
"number_of_pages": 350,
"publisher": "Oxford Publishers",
"country": "England",
"release_date": "1975-08-01"
},
"status_code":201,
"status":"success"
}
PATCH api/vi/books/id
curl -X PATCH -H "Content-Type: application/json" -d '{
"name": "Wonders of Astronomy",
"isbn": "999123-3213243567",
"authors": [
"Galileo Galileo",
"Adrian"
],
"number_of_pages": 350,
"publisher": "Oxford Publishers",
"country": "England",
"release_date": "1975-08-01"
}' http://localhost:8000/api/v1/books/2
{
"data": {
"id": 2,
"name": "Wonders of Astronomy",
"isbn": "999123-3213243567",
"authors": [
"Galileo Galileo",
"Adrian"
],
"number_of_pages": 350,
"publisher": "Oxford Publishers",
"country": "England",
"release_date": "1975-08-01"
},
"status_code":200,
"status":"success"
}
DELETE api/v1/books/id
curl -i -H 'Accept: application/json' -X DELETE http://localhost:8000/api/v1/books/1
{
"status_code":204,
"status":"success",
"message":"The book ‘My first book’ was deleted successfully",
"data":[]
}
GET api/external-books
curl -i -H 'Accept: application/json' http://localhost:8000/api/v1/external-books
{
"data":[
{
"id":1,
"name":"A Game of Thrones",
"isbn":"978-0553103540",
"authors":[
"George R. R. Martin"
],
"number_of_pages":694,
"publisher":"Bantam Books",
"country":"United States",
"release_date":"1996-08-01"
},
{
"id":2,
"name":"A Clash of Kings",
"authors":[
"George R. R. Martin"
],
"number_of_pages":768,
"publisher":"Bantam Books",
"country":"United States",
"release_date":"1999-02-02"
}
],
"status_code":200,
"status":"success"
}
If no books were found, the JSON below is returned;
{
"status_code":200,
"status":"success",
"data":[]
}
Optionally, a query string may be passed with the request as follows:
api/external-books?name=A Clash of Kings
- Create a file
database.sqlitein the database folder of the project composer test- To run tests and generate test coverage,
composer test:coverage - Test coverage files are generated in the
tests/coveragedirectory. Theindex.htmlfile can be viewed in the browser.
- PHP 7
- Laravel 8 framework
- Mysql
- Sqlite (For running tests)
- Validation still needs to be added when creating or updating a book.
- More refactoring can still be done on the codebase
- Seeders can be added for easily loading sample data into the database
- Kindly let me know if any issues are encountered.