- Steps to run application
- Steps to load mock data
- Steps to interact with server
- Database design
- API endpoints
- Validation
-
Clone the project to local machine at any location using following command
git clone https://github.com/aadarshkt/letsbloom_server_assignment.git cd letsbloom_server_assignment
-
If node is not available, download it from here. https://nodejs.org/en/download/ Download git from here.https://git-scm.com/downloads
-
Run following command to install packages.
npm install
-
Download XAMPP server to host local mySQL database from here. https://www.apachefriends.org/download.html
-
Start the mySQL and apache web server. If on windows run it with administrator priviledge. On windows go to programs and click run as administrator.
-
To start the server on localhost, use following command
npm start
-
The server will start running at port 8080, terminal will show the following.
Server listening on 8080 Connected to the database as ID [your_id]
-
The application is running.
- Go to this link to import database to local server. Link to phpMyAdmin
- Choose library_database.sql file from github repository to import mock data to local server.
- Go to this link to public postman workspace. If account is not availabe create postman account. Link to postman workspace
- Use different files available in the workspace to interact with server.
Other way is to use curl commands in terminal to interact with server as specified in the API endpoint section.
- books table has five columns. id, title, author, year_published, ISBN.
- id is the primary key with auto_increment property.
- Postman workspace has get all books folder, click on the send button to get all books from the database. Example response is given below.
- API endpoint.
http://localhost:8080/api/books
- curl command
curl http://localhost:8080/api/books
- Response is array of json objects containing fields of id, title, author, year_published, ISBN.
[ { "id" : 1, "title" : "book's title", "author" : "book's author", "year_published" : "publication year", "ISBN" : "10 digit international standard book number" } ]
-
API end point
http://localhost:8080/api/books
-
req.body of create book request. Go to post book file of postman workspace. Use body tab of workspace and send the below format data to create a book in the database
{ "title": "Life 3.0", "author": "Max Tegmark", "year_published": "2017", "ISBN" : "978-1-101-94659-6" }
-
curl command
curl -X POST -H "Content-Type: application/json" -d '{ "title": "Life 3.0", "author": "Max Tegmark", "year_published": "2017", "ISBN" : "978-1-101-94659-6" }' http://localhost:8080/api/books
-
Response is returned in following format.
{ "fieldCount": 0, "affectedRows": 1, "insertId": 15, "info": "", "serverStatus": 2, "warningStatus": 0, "changedRows": 0 }
-
Affected rows one means that data is successfully inserted in the database.
-
Referesh the phpMyAdmin database link to see updated data as below.
-
API endpoint
http://localhost:8080/api/books?id=[id_from_database]
-
curl command
curl -X PUT -H "Content-Type: application/json" -d '{ "title": "Life 3.0", "author": "Max Erik Tegmark", "year_published": "2017", "ISBN" : "978-1-101-94659-6" }' 'http://localhost:8080/api/books?id=[check_id_from_local_database]'
-
Go to put book file of postman workspace. Use body tab of workspace and send the below request using the send button.
{ "title": "Life 3.0", "author": "Max Erik Tegmark", "year_published": "2017", "ISBN" : "978-1-101-94659-6" }
-
Response format.
{ "fieldCount": 0, "affectedRows": 1, "insertId": 0, "info": "Rows matched: 1 Changed: 1 Warnings: 0", "serverStatus": 2, "warningStatus": 0, "changedRows": 1 }
-
If affectedRows has value 1 means book is successfully updated.
- When making a create or update request if any of the fields is empty, server will return 400 bad request response as follows. In following request author is empty.
curl -X PUT -H "Content-Type: application/json" -d '{ "title": "Life 3.0", "author": "", "year_published": "2017", "ISBN" : "978-1-101-94659-6" }' http://localhost:8080/api/books
- Response is in following format
{ "errors": [ { "type": "field", "value": "", "msg": "author not valid", "path": "author", "location": "body" } ] }
- Location of error is req.body and path represents the value that is empty.
- When making a update request if the query parameter is empty, error is returned.
curl -X PUT -H "Content-Type: application/json" -d '{ "title": "Life 3.0", "author": "Max Erik Tegmark", "year_published": "2017", "ISBN" : "978-1-101-94659-6" }' 'http://localhost:8080/api/books?id='
- Error response format.
{ "errors": [ { "type": "field", "value": "", "msg": "Id not valid", "path": "id", "location": "query" } ] }
- location value represents that req.query is empty.
- In create or update request the year should be in range of 868 to 2023. If following request is sent.
curl -X POST -H "Content-Type: application/json" -d '{ "title": "Life 3.0", "author": "Max Erik Tegmark", "year_published": "2024", "ISBN" : "978-1-101-94659-6" }' http://localhost:8080/api/books
- Error response is returned in following format.
{ "errors": [ { "type": "field", "value": "2024", "msg": "Year range is wrong", "path": "year_published", "location": "body" } ] }
- Path and location field in error response represents that error has happened in req.body with field year_published.
- User cannot enter duplcate values. If one tries, error response is generated.
{ "message": "Duplicate book, Invalid request" }
- Updating non-existent book. If a request is being sent with invalid id query parameter, error response is received.
response
curl -X PUT -H "Content-Type: application/json" -d '{ "title": "Life 3.0", "author": "Max Erik Tegmark", "year_published": "2017", "ISBN" : "978-1-101-94659-6" }' 'http://localhost:8080/api/books?id=3000'
{ "message": "Invalid book ID, Bad request" }