AbdulRazzak6478 / Booking-service

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

This is a base node js project template, which anyone can use as it has been prepared, by keeping some of the most important code principles and project management recommendations. Feel free to change anything.

src -> Inside the src folder all the actual source code regarding the project will reside, this will not include any kind of tests. (You might want to make separate tests folder)

Lets take a look inside the src folder

  • config -> In this folder anything and everything regarding any configurations or setup of a library or module will be done. For example: setting up dotenv so that we can use the environment variables anywhere in a cleaner fashion, this is done in the server-config.js. One more example can be to setup you logging library that can help you to prepare meaningful logs, so configuration for this library should also be done here.

  • routes -> In the routes folder, we register a route and the corresponding middleware and controllers to it.

  • middlewares -> they are just going to intercept the incoming requests where we can write our validators, authenticators etc.

  • controllers -> they are kind of the last middlewares as post them you call your business layer to execute the business logic. In controllers we just receive the incoming requests and data and then pass it to the business layer, and once business layer returns an output, we structure the API response in controllers and send the output.

  • repositories -> this folder contains all the logic using which we interact the DB by writing queries, all the raw queries or ORM queries will go here.

  • services -> contains the business logic and interacts with repositories for data from the database

  • utils -> contains helper methods, error classes etc.

Setup the project

  • Download this template from github and open it in your favorite text editor.
  • Go inside the folder path and execute the following command:
npm install
  • In the root directory create a .env file and add the following env variables

        PORT=<port number of your choice>
    

    ex:

        PORT=3000
    
  • go inside the src folder and execute the following command:

      npx sequelize init or npx sequelize init --force
    
  • By executing the above command you will get migrations and seeders folder along with a config.json inside the config folder.

  • If you're setting up your development environment, then write the username of your db, password of your db and in dialect mention whatever db you are using for ex: mysql, mariadb etc

  • If you're setting up test or prod environment, make sure you also replace the host with the hosted db url.

  • To run the server execute

npm run dev

How to use Booking service routes

  • Bookings Bookings http://localhost:3000/api/v1/bookings/
  to create Booking
  /*
    POST /bookings/
    body {
      flightId :2,
      userId : 5
      noOfSeats : 2
    }

  */
  body {
        flightId : req.body.flightId,
        userId: req.body.userId,
        noOfSeats : req.body.noOfSeats,
      }

after that routes is about to make payment

  • MakePayment where we mimic like payment
  • http://localhost:3000/api/v1/bookings/payment
  Proceed for payment
  /*
    POST /bookings/payment
    body {
      bookingId :1,
      userId : 5
      totalCost : 5000 Ex : noOfSeats * price
    }
  */
  body {
        bookingId : req.body.bookingId,
        userId: req.body.userId,
        totalCost : req.body.totalCost,
      }

  • For uniquely identifying transaction mention idempotency 'x-idempotency-key' key in headers using like bearer-token
  • make payment request is valid for 10 minutes after that if payments is not done then no of seats that have booked will be released

About


Languages

Language:JavaScript 100.0%