This project is a REST API that can add, edit, delete, and show books. It was a submission project to acquire a certificate from Dicoding.
This API only stores the data in an array variable. The data will be lost upon every restart.
Data example:
{
"id": "Qbax5Oy7L8WKf74l",
"name": "Buku A",
"year": 2010,
"author": "John Doe",
"summary": "Lorem ipsum dolor sit amet",
"publisher": "Dicoding Indonesia",
"pageCount": 100,
"readPage": 25,
"finished": false,
"reading": false,
"insertedAt": "2021-03-04T09:11:44.598Z",
"updatedAt": "2021-03-04T09:11:44.598Z"
}
The properties id
, finished
, insertedAt
, updatedAt
are managed by the server.
The other properties are input by the client.
- Make sure you have
nvm
installed. If you don't have, install it using these commands below:-
You could skip this step if you would like to install NodeJS in the other way
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
-
- Install NodeJS v18.13.0 LTS via nvm:
-
nvm install v18.13.0 nvm use v18.13.0
-
- Install packages required in the project directory:
- Open a terminal and change the directory to the project directory and type this command below:
npm install
- Open a terminal and change the directory to the project directory and type this command below:
- Open a terminal and change the directory to the project directory.
- Type this command to run the API:
It will create a web server that runs on http://localhost:9000.
npm run start
Request:
- Method: POST
- URL: /books
- Body Request:
{ "name": string, "year": number, "author": string, "summary": string, "publisher": string, "pageCount": number, "readPage": number, "reading": boolean }
Response:
- If client doesn't send the property
name
:- Status Code: 400
- Response Body:
{ "status": "fail", "message": "Gagal menambahkan buku. Mohon isi nama buku" }
- If the
readPage
value is more than thepageCount
value:- Status Code: 400
- Response Body:
{ "status": "fail", "message": "Gagal menambahkan buku. readPage tidak boleh lebih besar dari pageCount" }
- If the book is successfully added:
- Status Code: 201
- Response Body:
{ "status": "success", "message": "Buku berhasil ditambahkan", "data": { "bookId": "1L7ZtDUFeGs7VlEt" } }
Request:
- Method: GET
- URL: /books
- Query parameters (optional, for filters):
- ?name, shows all books that have the name containing the value of this query.
- ?reading, shows all reading books if its value is
true
or1
, or all unread books if its value isfalse
or0
. - ?finished, shows all finished books if its value is
true
or1
, or all unfinished books if its value isfalse
or0
.
Response:
- If there are some books:
- 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 there have not been books yet:
- Status Code: 200
- Response Body:
{ "status": "success", "data": { "books": [] } }
Request:
- Method: GET
- URL: /books/{bookId}
Response:
- If the book's
id
is not found:- Status Code: 404
- Response Body:
{ "status": "fail", "message": "Buku tidak ditemukan" }
- If the book's
id
is found:- 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" } } }
Request:
- Method: PUT
- URL: /books/{bookId}
- Body Request:
{ "name": string, "year": number, "author": string, "summary": string, "publisher": string, "pageCount": number, "readPage": number, "reading": boolean }
Response:
- If client doesn't send the property
name
:- Status Code: 400
- Response Body:
{ "status": "fail", "message": "Gagal memperbarui buku. Mohon isi nama buku" }
- If the
readPage
value is more than thepageCount
value:- Status Code: 400
- Response Body:
{ "status": "fail", "message": "Gagal memperbarui buku. readPage tidak boleh lebih besar dari pageCount" }
- If the book's
id
is not found:- Status Code: 404
- Response Body:
{ "status": "fail", "message": "Gagal memperbarui buku. Id tidak ditemukan" }
- If the book is successfully updated:
- Status Code: 200
- Response Body:
{ "status": "success", "message": "Buku berhasil diperbarui" }
Request:
- Method: DELETE
- URL: books/{bookId}
Response:
- If the book's
id
is not found:- Status Code: 404
- Response Body:
{ "status": "fail", "message": "Buku gagal dihapus. Id tidak ditemukan" }
- If the book's
id
is found:- Status Code: 200
- Response Body:
{ "status": "success", "message": "Buku berhasil dihapus" }