Bookshelf API is a final submission on "Belajar Membuat Aplikasi Back-End untuk Pemula" from Dicoding course. This project built by npm package manager, Hapi Framework, and Nodemon. And for testing purposes, we using Postman.
This submission can perform various operations such as adding books, retrieving book details, updating book information, and deleting books. The submission includes two important files for testing: the "Bookshelf API Test Collection" and the "Environment" file.
- Clone this repository.
- Extract the files to your local destination.
- Make sure you have already installed
Node.js
andnpm (Node Package Manager)
. If you don't have one, you can download it onhttps://nodejs.org/en
. - Open the file and navigate to the project directory folders.
- Install the dependencies required by the Bookshelf API .project using this command
npm install
. - You can start the API server using this command
npm run start
. - By default, this program will run on localhost:9000.
- Open Postman application and click on the "Import" button and select the "Bookshelf API Test Collection" and "Environment" files that were provided with the project.
- Change the Environment Variables to
Bookshelf API Test
. - Finally, you can
Run Bookshelf API test
to run the entire tests. However, if you reach at the limit, you can try run one by one sequentially.
handler.js
: This file provides the logic for dealing with requests and responses. It serves as a bridge between the routes and the data.
books.js
: Represents the book collection's data structure. This file contains an array or interacts with a database to store and retrieve book information.
server.js
: Starts the server and configures it as needed. Initializes an HTTP server with a framework such as Hapi.
routers.js
: Specifies the app's routes. This file translates HTTP methods and endpoints (GET, POST, PUT, DELETE, etc.) to the proper handler functions in handler.js
.
- Request
- Method:
POST
- URL:
/books
- Body Request:
{ "name": "string", "year": "number", "author": "string", "summary": "string", "publisher": "string", "pageCount": "number", "readPage": "number", "reading": "boolean" }
- Method:
- Response
- If the client does not provide the
name
property in the request body, the server will respond with:- Status Code: 400
- Response body:
{ "status": "fail", "message": "Gagal menambahkan buku. Mohon isi nama buku" }
- If the client provides a
readPage
value greater than thepageCount
value, the server will respond with:- Status Code: 400
- Response Body:
{ "status": "fail", "message": "Gagal menambahkan buku. readPage tidak boleh lebih besar dari pageCount" }
- If the book is successfully added, the server will respond with:
- Status Code: 201
- Response Body:
{ "status": "success", "message": "Buku berhasil ditambahkan", "data": { "bookId": "1L7ZtDUFeGs7VlEt" } }
- If the client does not provide the
- Request
- Method:
GET
- URL:
/books
- Method:
- Response
- If there are books available, the server will respond with:
- Status Code: 200
- Response Body:
{ "status": "success", "data": { "books": [ { "id": "Qbax5Oy7L8WKf74l", "name": "Buku A", "publisher": "Dicoding Indonesia" }, { "id": "1L7ZtDUFeGs7VlEt", "name": "Buku B", "publisher": "Dicoding Indonesia" }, { "id": "K8DZbfI-t3LrY7lD", "name": "Buku C", "publisher": "Dicoding Indonesia" } ] } }
- If no books are available, the server will respond with an empty array:
- Status Code: 200
- Response Body:
{ "status": "success", "data": { "books": [] } }
- If there are books available, the server will respond with:
- Request
- Method:
GET
- URL:
/books/{bookId}
- Method:
- Response
- If the book with the specified
Id
is found, the server will respond with:- Status Code: 200
- Response Body:
{ "status": "success", "data": { "book": { "id": "aWZBUW3JN_VBE-9I", "name": "Buku A Revisi", "year": 2011, "author": "Jane Doe", "summary": "Lorem Dolor sit Amet", "publisher": "Dicoding", "pageCount": 200, "readPage": 26, "finished": false, "reading": false, "insertedAt": "2021-03-05T06:14:28.930Z", "updatedAt": "2021-03-05T06:14:30.718Z" } } }
- If the book with the specified
Id
is not found, the server will respond with:- Status Code: 404
- Response Body:
{ "status": "fail", "message": "Buku tidak ditemukan" }
- If the book with the specified
- Request
- Method:
PUT
- URL:
/books/{bookId}
- Body Request:
{ "name": "string", "year": "number", "author": "string", "summary": "string", "publisher": "string", "pageCount": "number", "readPage": "number", "reading": "boolean" }
- Method:
- Response
- If the client does not provide the
name
property in the request body, the server will respond with:- Status Code: 400
- Response Body:
{ "status": "fail", "message": "Gagal memperbarui buku. Mohon isi nama buku" }
- If the client provides a
readPage
value greater than thepageCount
value, the server will respond with:- Status Code: 400
- Response Body:
{ "status": "fail", "message": "Gagal memperbarui buku. readPage tidak boleh lebih besar dari pageCount" }
- If the specified
Id
is not found, the server will respond with:- Status Code: 404
- Response Body:
{ "status": "fail", "message": "Gagal memperbarui buku. Id tidak ditemukan" }
- If the book is successfully updated, the server will respond with:
- Status Code: 200
- Response Body:
{ "status": "success", "message": "Buku berhasil diperbarui" }
- If the client does not provide the
- Request
- Method:
DELETE
- URL:
/books/{bookId}
- Method:
- Response
- If the specified
id
is not found, the server will respond with:- Status Code: 404
- Response Body:
{ "status": "fail", "message": "Buku gagal dihapus. Id tidak ditemukan" }
- If the book is successfully deleted, the server will respond with:
- Status Code: 200
- Response Body:
{ "status": "success", "message": "Buku berhasil dihapus" }
- If the specified