RESTAPI written in node using ExpressJS. This API records a single user's transactions.
- [Node.js] [https://nodejs.org/en]
- [Express.js][https://expressjs.com/]
- [Sequelize][https://sequelize.org/]
- [sqllite3][https://www.sqlite.org/index.html]
- [Docker][https://www.docker.com/]
Make sure you have nvm for easy node management.
- MacOS - Using HomeBrew
brew install nvm
- Linux -
Note - Make sure to source nvm before you can use it
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
source ~/.nvm/nvm.sh
- Install Node Version
nvm install 18.17.1 nvm use 18.17.1
If you'd like to run the containerized version of the API, you will need to have the Docker CLI
- Linux - Instructions here https://docs.docker.com/engine/install/ubuntu/
- MacOS and Windows - Install Docker Desktop here https://docs.docker.com/desktop/install/mac-install/ which includes the daemon and the CLI.
- Clone the repo
git clone https://github.com/dronavallipranav/FetchBackendAPI.git
- Install NPM packages
npm install
- Start the API server on port 8000
or build the docker image and run the container on port 8000
node index.js
docker build -t fetch_api . docker run -p 8000:8000 -d fetch_api
This API provides three endpoints
Request Body
{
payer: <String>
points: <Integer>
timestamp: <String>
}
-Creates a user record in the database for the given payer, points, and timestamp.
-response:
- 200 OK: Points successfully added.
- 400 Bad Request: Request not formatted correctly.
- 500 Internal Server Error: Unexpected server error.
Request Body
{
points: <Integer>
}
-Spends user's points based on order that transactions were received
-response:
- 200 OK: Ledger table updated,response contains a list of points deducted per payer.
- 400 Bad Request: Can't spend zero or negative points, or user does not have enough points to spend.
- 500 Internal Server Error: Unexpected server error.
-Gets user's total points for each payer
-response:
- 200 OK: Successfully retrieved the list of payers and their point balance.
- 500 Internal Server Error: Unexpected server error.
Integration Tests can be run using the command
npm run test
Or to run tests with watch mode
npm run test:watch
-TO ALLOW DATA IN DATABASE TO PERSIST BEYOND SERVER RESTARTS - OPEN THE "storage/database.js" FILE AND CHANGE LINE
sequelize
.sync({ force: true })
.then(() => console.log("Tables have been synchronized"))
.catch((error) => console.error("Unable to synchronize the tables:", error));
TO
sequelize
.sync({ force: false })
.then(() => console.log("Tables have been synchronized"))
.catch((error) => console.error("Unable to synchronize the tables:", error));
I chose force sync as a default as I figured it would be easier for the graders to test the functionality
Pranav Dronavalli - dronavallipranav@gmail.com