A bookstore application with web API methods.
The project contains 50 functional tests and 100% code coverage.
![Screenshot 2023-11-05 at 11 09 51 PM](https://private-user-images.githubusercontent.com/45903885/280592651-aa80c3cf-d619-41a0-bf83-430ec7760391.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjIyNjMyMTUsIm5iZiI6MTcyMjI2MjkxNSwicGF0aCI6Ii80NTkwMzg4NS8yODA1OTI2NTEtYWE4MGMzY2YtZDYxOS00MWEwLWJmODMtNDMwZWM3NzYwMzkxLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MjklMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzI5VDE0MjE1NVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTJmMTQ2ZjA0YjRmYjEyNWY2NmE3NjVhYWJkZTMwNDYzMTgzYTE0MTdmYzVhZmI4NWQ1ZjdlM2YxNjk4MTk2YjgmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.DeFSTjElN9BLOFp8l54Ids-RLwcn94InWaLK_RiZxqE)
composer install
composer refresh-db
composer test
GET /api/books - a list of Books with pagination, sorting and filtering options.
Available query parameters:
page
- page number
sortColumn
- one of title
, avg_review
or published_year
sortDirection
- one of ASC
or DESC
title
- search by book title
authors
- search by author’s ID (comma-separated)
Sample response (HTTP 200)
{
"data":[
{
"id":1,
"isbn":"9077765476",
"title":"Et hic et mollitia ea nihil culpa.",
"description":"Possimus voluptatem rerum harum nemo asperiores. Consequuntur tenetur ut nemo ipsam placeat. Sunt eos cum assumenda quasi est. Dolores earum qui quod nihil commodi nisi.",
"published_year": 2020,
"authors":[
{
"id":1,
"name":"Dr. Beth Weber PhD",
"surname":"Jenkins"
}
],
"review":{
"avg":4,
"count":3
}
}
],
"links":{
"first":"http:\/\/localhost\/api\/books?page=1",
"last":"http:\/\/localhost\/api\/books?page=1",
"prev":null,
"next":null
},
"meta":{
"current_page":1,
"from":1,
"last_page":1,
"path":"http:\/\/localhost\/api\/books",
"per_page":15,
"to":5,
"total":5
}
}
Implementation details:
- Implemented
App\Http\Resources\BookResource::toArray
method. - Queried the data from
Book
Eloquent model and responded withBookResource
collection. - Implemented pagination feature (from Eloquent).
- Allowed sorting by title.
- Allowed sorting by average review.
- Allowed searching by title (SQL like query).
- Allowed searching by author’s ID.
POST /api/books - creates a new Book resource.
Access to this endpoint requires authentication with an API token and admin privileges.
Required parameters:
isbn
- string (13 characters, digits only)
title
- string
description
- string
authors
- int[] - author’s ID
published_year
- int (between 1900 and 2020)
Sample response (HTTP 201)
{
"data":{
"id":1,
"isbn":"9788328302341",
"title":"Clean code",
"description":"Lorem ipsum",
"published_year": 2020,
"authors":[
{
"id":1,
"name":"Prof. Darrin Mraz Jr.",
"surname":"Bins"
}
],
"review":{
"avg":0,
"count":0
}
}
}
In case of validation errors, the API responds with the default error list from the Laravel framework and the 422 HTTP code.
Implementation details:
- Validated the required fields.
- Ensured that the ISBN is unique and author’s ID exist in the DB.
- Stored Book in the DB.
- Restricted access only for administrators with
auth.admin
middleware. - Responded with
BookResource
.
POST /api/books/{id}/reviews - creates a new BookReview resource.
Access to this endpoint requires authentication with an API token.
Required parameters:
review
- int (1-10)
comment
- string
Sample response (HTTP 201)
{
"data":{
"id":1,
"review":5,
"comment":"Lorem ipsum",
"user":{
"id":1,
"name":"Kody Lebsack"
}
}
}
In case of an invalid Book ID, the API responds with the 404 HTTP code.
In case of validation errors, the API responds with the default error list from the Laravel framework and the 422 HTTP code.
Implementation details:
- Validated the required fields.
- Stored BookReview in the DB.
- Restricted access only for authenticated users.
- Responded with
BookReviewResource
.
DELETE /api/books/{id}/reviews/{id} - deletes a BookReview resource.
Access to this endpoint requires authentication with an API token and admin privileges.
Sample response (HTTP 204)
{}
In case of an invalid Book ID, the API responds with the 404 HTTP code.\
Implementation details:
- Validated if the book exists.
- Deleted the book review from DB.
- Returned an empty body and 204 status code.