Rules:
- No Authentication
- 1 User Only Can Borrow 1 Book At A Time
- User can fill the due date with maximum 30 days
Instruction:
- Run
npm i
- Change the
.env
file to your liking - Run
npx sequelize db:create
- Run
npx sequelize db:migrate
- Run
npx sequelize db:seed:all
- Run
npm start
to start the API
For Future Development:
- Create Unit Tests with Test Driven Development
- Create Authentication for Admin and User, example JWT
- Create 1 more status for booklog ('late')
- Use cronjob to track current date and due date, if current date is more than due date and the book is not returned yet. Change the book log status from 'borrowing' to 'late'
- Make Users can borrow multiple book at the same time (same start date and due date)
POST /v1/admin/book
PATCH /v1/admin/book/add-stocks
GET /v1/admin/user/book-log
POST /v1/user/register
GET /v1/user/profile
POST /v1/user/book/:book_id/borrow
PUT /v1/user/book/:book_id/return
GET /v1/guest/book
GET /v1/guest/book/:book_id
Create New Book
Request:
- Body
{ "title": "string", "isbn": "string", "stocks": integer }
Response:
-
Response (201 - Created)_
{ "message": "Success Create Book" }
-
Response (400 - Bad Request)
{ "message": "Title is Required" } OR { "message": "ISBN Has Been Taken" } OR { "message": "ISBN is Required" } OR { "message": "Stocks is Required" } OR { "message": "Minimum Stocks Is 1" }
Add Stock To Existing Books
Request:
- Body
{ "title": "string", "isbn": "string", "stocks": integer }
Response:
-
Response (200 - OK)
{ "message": "Success Add Stocks" }
-
Response (404 - Not Found)
{ "status": 404, "message": "Book Not Found" }
-
Response (400 - Bad Request)
{ "status": 400, "message": "Minimum Stocks Is 1" }
Get All Book Log With Data Of User Borrower and Book
Request:
- Query
- limit (default = 5)
- page (default = 0)
- status (default = "", can only receive = 'borrowing', 'returned ontime', 'returned late')
- isbn (default = "")
- title (default = "")
Response:
- Response (200 - OK)
{
"count": 2,
"rows": [
{
"id": 2,
"user_id": 1,
"book_id": 3,
"status": "returned ontime",
"start_date": "2022-10-13",
"due_date": "2022-10-15",
"return_date": "2022-10-13",
"createdAt": "2022-10-13T10:10:17.615Z",
"updatedAt": "2022-10-13T10:12:57.756Z",
"User": {
"id": 1,
"no_ktp": "082110517592",
"name": "Bimo",
"email": "bimonugraraga@gmail.com"
},
"Book": {
"id": 3,
"title": "Monalisa OverDrive",
"isbn": "c123"
}
},
...
]
}
Create New User
Request:
- Body
{
"email": "string",
"no_ktp": "string",
"name": "string"
}
Respose
- Response (201-Creadted)
{
"message": "Success Create User"
}
- Response (400-Bad Request)
{
"message": "KTP is Required"
}
OR
{
"message": "KTP Has Been Taken"
}
OR
{
"message": "Email is Required"
}
OR
{
"message": "Email Has Been Taken"
}
OR
{
"message": "Invalid Email Format"
}
Get Profile User And Their Book Log History
Request:
- Body
{
"email": "string"
}
Response:
- Response (200-OK)
{
"id": 1,
"no_ktp": "082110517592",
"name": "Bimo",
"email": "bimonugraraga@gmail.com",
"BookLogs": [
{
"id": 2,
"user_id": 1,
"book_id": 3,
"status": "returned ontime",
"start_date": "2022-10-13",
"due_date": "2022-10-15",
"return_date": "2022-10-13",
"Book": {
"id": 3,
"title": "Monalisa OverDrive",
"isbn": "c123"
}
},
{
"id": 1,
"user_id": 1,
"book_id": 1,
"status": "returned late",
"start_date": "2022-10-12",
"due_date": "2022-10-10",
"return_date": "2022-10-13",
"Book": {
"id": 1,
"title": "Neuromancer",
"isbn": "a123"
}
}
]
}
- Response (404-Not Found)
{
"status": 404,
"message": "User Not Found"
}
Borrow A Book, Create Book Log With Status Borrowing
Request:
- Body
{
"email": "string",
"no_ktp": "string",
"due_date": "date(format: yyyy-mm-dd)"
}
- Params
- book_id
Response:
- Response (201 - Created)
{
"message": "Success Borrow Book"
}
- Response (400 - Bad Request)
{
"message": "Must Provide Email And KTP"
}
OR
{
"message": "Status value is Invalid"
}
OR
{
"message": "Cannot Borrow More Than 1 Book"
}
OR
{
"message": "Invalid Date"
}
OR
{
"message": "Maximum Borrwing Time Is 30 Days"
}
- Response (404 - Not Found)
{
"message": "User Not Found"
}
OR
{
"message": "Book Not Found Or Book Out Of Stock"
}
Return Borrowed Book, Create Book Log With Status 'returned ontime' (before to exact due date) and 'returned late' (after due date)
Request:
- Body
{
"email": "string",
"no_ktp": "string"
}
- Params
- book_id
Response:
- Response (201 - Created)
{
"message": "Success Return Book"
}
- Response (400 - Bad Request)
{
"message": "Must Provide Email And KTP"
}
OR
{
"message": "Not Yet Borrow Any Book Or Wrong Book ID"
}
- Response (404 - Not Found)
{
"message": "User Not Found"
}
OR
{
"message": "Book Not Found"
}
Get All Existing Book With Stocks DESC Request:
- Query
- limit (default = 5)
- page (default = 0)
- title (default = "")
Response:
- Response (200 - OK)
{
"count": 4,
"rows": [
{
"id": 4,
"title": "Laskar Pelangi",
"isbn": "mygod123",
"stocks": 22,
"createdAt": "2022-10-13T10:51:27.821Z",
"updatedAt": "2022-10-14T18:00:42.345Z"
},
{
"id": 1,
"title": "Neuromancer",
"isbn": "a123",
"stocks": 10,
"createdAt": "2022-10-12T16:32:53.253Z",
"updatedAt": "2022-10-13T10:09:36.148Z"
},
...
]
}
Get Detail of One Book
Request:
- Params
- book_id
Response:
- Response (200 - OK)
{
"id": integer,
"title": "string",
"isbn": "string",
"stocks": integer,
"createdAt": "date time",
"updatedAt": "date time"
}
- Response (404 - Not Found)
{
"message": "Book Not Found"
}
{
"code": 500,
"message": "internal server error"
}