Skip to content

shahnCM/library_api_express_mysql_docker

Repository files navigation

How to Run the App

Using Docker:

  • clone / download
  • go to the project dir
  • run docker-compose up
  • wait for the MySql db , PhpMyAdmin & the App to fully initialize
  • Once every thing is properly runnig start playing with the end points

Using Local Environment

Need to have the followings installed in your machine

  1. Mysql
  2. NodeJs, Npm
  3. Globally installed sequelize-cli
    • to install sequelize-cli globally run this -> npm install -g sequelize-cli in the terminal
  4. Once you have all of this, go to the project dir
    • put your database username password host port in config/config.json file
  5. Open terminal in the project root dir and type
    • npm install
  6. Make sure your mysql database is running & then in console type npm run local-boot-up

Take a look at scripts in package.json so you know what command you need to run,

  "scripts": {
    "start": "node server.js",
    "migU": "sequelize db:migrate",
    "migD": "sequelize db:migrate:undo:all",
    "migF": "npm install && npm run migD && npm run migU",
    "docker-server": "nodemon server.js 0.0.0.0 5000",
    "docker-boot-up": "npm run migU && npm run docker-server",
    "local-server": "nodemon server.js 127.0.0.0 5000",
    "local-boot-up": "npm run migU && npm run local-server"
  }

If any problem occurs with migration, run the scripts for migration manually

About the APP: Its a library management application api

It has the followin Resources

  1. Books
  2. Authors
  3. Book-loans
  4. Users

Specification

The purpose of the API is to provide a management system for a library. There are two types of users in a Library.

  • Library Member Browse books, authors, request and view Book-Loans

  • Library Admin Create, update, remove Books and Authors. Accepet, reject Book-Loan requests. Update Book-Loan when book is returned In addtion to providing the basic RESTful API endpoints and their role based access specified above, API should also have the following features

  • Token Based Authentication (timeout can be as much as wish) Profile image upload for users (store image anywhere like) Browse books by author Excel export for Book-Loans data (only Library Admin) Implementation It is required to implement the API using the either Django or NodeJs and any database(relational, nosql etc) of choice

Constrainsts it maintaines

  • From Performence Perspective
  1. Fast response time. No endpoint should have a response time over 1 second regardless of the data size
  2. Appropriate status codes with all the responses.
  • From Coding Prespective
  1. Proper and easy to understand naming of variables, function and classes
  2. Clear and to the point commenting of code
  3. Good common sense in selecting fields for the resources

All End Points at a Glance


POST localhost:5000/api/auth/register/admin/{:key}
POST localhost:5000/api/auth/register
POST localhost:5000/profile-image/User-One-1617391248.jpeg
POST localhost:5000/api/auth/login
GET localhost:5000/api/auth/user


GET localhost:5000/api/authors?page=1
GET localhost:5000/api/authors (req.query.page is set 0 by default)
GET localhost:5000/api/authors/{:id?}
POST localhost:5000/api/authors
PUT localhost:5000/api/authors/{:id}
DELETE localhost:5000/api/authors/1


GET localhost:5000/api/books?page=1
GET localhost:5000/api/books (req.query.page is set to 0 by default)
GET localhost:5000/api/books/{:id?}
POST localhost:5000/api/search/by-any?page=1
POST localhost:5000/api/search/by-any (req.query.page is set to 0 by default)
POST localhost:5000/api/books
PUT localhost:5000/api/books/{:id}
DELETE localhost:5000/api/books/{:id}


GET localhost:5000/book-loans?page=0
GET localhost:5000/book-loans/{:id?}
GET localhost:5000/book-loans/users/{:id}?page=3
GET localhost:5000/book-loans/admin/{:id}
POST localhost:5000/book-loans/loan
POST localhost:5000/book-loans/return
PUT localhost:5000/book-loans/{id}/take/{action} -> await/accept/reject
GET localhost:5000/api/book-loans/report/excel
GET localhost:5000/api/book-loans/report/excel/composite

User Registration & Authentication

Resource: Users

Admin Registration


Admin registration can be turned on/off from config/default.json

{
    "jwtSecret": "secret",
    "jwtTokenName": "x-auth-token", // Name of Token, Auth Middleware will look for
    "adminRegistrationSecret": "admin-101", // Needs to be passed in URL
    "adminRegistrationOn": false, // true for enabling Admin Registration
    "perPage": 5, // Default Query / Result Limit
    "baseUrl": "http://localhost:5000/" 
}

Admin Registration Route ,
POST: localhost:5000/api/auth/register/admin/admin-101

Validation is enabled with proper status code
Profile Picture is not required for Admins but required for Users
Following fields are required:

{
    "name": "Admin 1",
    "email": "admin1@admin.com",
    "password": "123456"
}
A JWT Token will be returned after successful registration
  • In users table isAdmin field is set to 1 for Admins

User Registration


User Registration Route ,
POST: localhost:5000/api/auth/register/

Form Data is Required for User Registration
As Profile Image is Mandatory Validation is enabled with proper status code
Profile Picture is required for Users
Following fields are required:

  • name <type: text>
  • password <type: text>
  • email <type: text>
  • profile_image <type: file, Only JPEG or PNG>
  • Profile Image is saved in storage/images/UserProfileImage
  • base url, localhost:5000/ needs to be added
    before returned profile image link
    /profile-image/User-One-1617391248.jpeg
    ie: localhost:5000/profile-image/User-One-1617391248.jpeg

File size limit can be changed from config/imageUpload.settings.json through sizeLimit

{
    "userProfileImagePath" : "storage/images/UserProfileImage",
    "serverStaticPath": "/profile-image",
    "sizeLimit" : 1000000 // in-bytes
}
A JWT Token will be returned after successful registration
  • In users table isAdmin field is set to 0 for Users

Authentication / Login (User/Admin)


Authentication / Login for both User & Admin is handled through same route
User/Admin Login/Authentication Route
POST: localhost:5000/api/auth/login/

Validation is enabled with proper status code
Following fields are required:

{
    "name": "User One",
    "email": "user@One.com",
    "password": "123456"
}
A JWT Token will be returned after successful authentication

Getting Logged in User's Info GET localhost:5000/api/auth/user/

Access Control through Middlewares


Here we have implemented 3 middlewares for Access Control

  1. Auth Middleware
    1. Auth.pass checks auth access & let pass
    2. Auth.block checks auth access & blocks (Prevents Login & Registration when already logged in)
  2. Role Middleware
    1. Role.Admin only lets Admin pass & blocks User
    2. Role.User only lets User pass & blocks Admin
  3. Feature Middleware
    1. Admin Registration, Enable access to Admin Registration Link, checks Url key
    2. setDefaultPage, sets {req.query.page} to 0 if not provided in the Url

Books & Authors

Users can view / browse books & authors as a collection as well as individually
Users can also search books by book name / author name

Only Admins can CREATE , UPDATE , DELETE books & authors

Resource: Authors


  1. Show All, Access user , admin
    GET localhost:5000/api/authors?page=1
    GET localhost:5000/api/authors (req.query.page is set 0 by default)

  2. Show Individual, Access admin
    GET localhost:5000/api/authors/1

  3. Create Author, Access admin (Expects JSON)
    POST localhost:5000/api/authors/

{
    "name": "Degemon Big Brain"
}
  1. Update Author, Access admin (Expects JSON)
    PUT localhost:5000/api/authors/1
{
    "name": "Degemon Small Brain"
}
  1. Delete Author, Access admin
    DELETE localhost:5000/api/authors/1

Resource: Books


  1. Show All, Access admin , user
    GET localhost:5000/api/books?page=1
    GET localhost:5000/api/books (req.query.page is set to 0 by default)

  2. Search By Any(Book/Author), Access admin , user
    POST localhost:5000/api/search/by-any?page=1
    POST localhost:5000/api/search/by-any (req.query.page is set to 0 by default)

{
    "key" : "Author Name"
}

Or,

{
    "key" : "Book Name"
}
  1. Show Individual, Access admin , user
    GET localhost:5000/api/books/1

  2. Create Book, Access admin (Expects JSON)
    POST localhost:5000/api/books/

{
    "name": "Leopard's Hustle",
    "authors": [3,4] // authors id can be passed 
}
  1. Update Book, Access admin (Expects JSON)
    PUT localhost:5000/api/books/1
{
    "name" : "Leopards Hustle",
    "authors" : [4]
}
  1. Delete Book, Access admin
    DELETE localhost:5000/api/books/1

Book Loan & Return

  • Users can request for a book to loan or return
  • Users can also view their own Book-loans
  • Only Admins can Handle the Requests for Books Loan / Return

Resource: Book-loans


  1. Show All Book-loans , Access admin, user
    GET localhost:5000/book-loans?page=0

    • User will see all of his / her Book-loan / return requests
    • Admin will see all kinds of requests of all users
  2. Show Individual Book-loan , Access admin, user
    GET localhost:5000/book-loans/1

    • Admin can view any ones Book-loan / return request
    • User can view the request matching {route.params.id} if that request was made by him / her
  3. Show Individual Users Book-loan , Access admin
    GET localhost:5000/book-loans/users/1
    GET localhost:5000/book-loans/users/1?page=3

    • Admin can view any ones Book-loan / return request matching {route.params.id}
  4. Show Processed / handled Book-loan by individual admin , Access admin
    GET localhost:5000/book-loans/admin/1

    • Admin can view all Book-loan / return requests handled by any admin matching {route.params.id}
  5. Loan Request for A Book , Access user (Expects JSON)
    POST localhost:5000/book-loans/loan

{
    "book_slug" : "Book-6-1617084129"
}
  • Have to send the slug to make a loan request for a book
  1. Return Request for A Book , Access user (Expects JSON)
    POST localhost:5000/book-loans/return
{
    "tracking_id" : "4Book-6-1617084129-1617104050"
}
  • Have to send the tracking id of the loan request to make a return request for a lend book
  1. Accept / Reject / Await a Request , Access admin
    PUT localhost:5000/book-loans/17/take/await
    PUT localhost:5000/book-loans/17/take/accept
    PUT localhost:5000/book-loans/17/take/reject
  • Have to send the request id in the Url, Update the status of Request matching that {req.params.id}
  • await changes status to pending
  • reject changes status to accepted
  • accept changes status to rejected
  1. Generate Book-loans Report & Export , Access admin
  • A Usual Excel Report with all the required column
    GET localhost:5000/api/book-loans/report/excel/

  • A Pair Matched Excel Composite Report
    GET localhost:5000/api/book-loans/report/excel/composite

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published