GithubHelp home page GithubHelp logo

slice---dice's Introduction

Slice🍕 & Dice🎲 Express.js REST API with JWT Authentication

This is a simple Express.js application that provides a REST API for managing employee records and calculating summary statistics. The API is secured using JSON Web Tokens (JWT) for authentication.

Features

  • User login and token generation.
  • CRUD operations on employee records.
  • Calculation of summary statistics over the dataset.
  • Middleware for checking the validity of tokens.

Dependencies

  • Express.js
  • Body-parser
  • JSON Web Token (JWT)

Installation

  1. Clone this repository to your local machine.
  2. Run npm install to install all necessary dependencies.
  3. To start the server, run node index.js.

API Endpoints

POST /login

Authenticates a user and returns a JWT token.

Request body:

{
  "username": "Akshay",
  "password": "Akshay@123"
}

POST /add_record

Adds a new record to the dataset.

Request headers:

Authorization: Bearer <token>

Request body:

{
  "name": "John Doe",
  "salary": "5000",
  "currency": "USD",
  "department": "Marketing",
  "sub_department": "Digital Marketing"
}

DELETE /delete_record/:recordIndex

Deletes a record by index from the dataset.

Request headers:

Authorization: Bearer <token>

URL parameters:

/delete_record/0

GET /summary_stats/all_salary

Fetches summary statistics for salary over the entire dataset.

Example Response:

{
  "mean": 5000,
  "min": 5000,
  "max": 5000
}

GET /summary_stats/on_contract_salary

Fetches summary statistics for salaries of records with "on_contract": "true".

Example Response:

{
  "mean": 5000,
  "min": 5000,
  "max": 5000
}

GET /summary_stats/department_salary/:department

Fetches summary statistics for salaries of employees in a specific department.

URL parameters:

/summary_stats/department_salary/Engineering

Example Response:

{
  "mean": 5000,
  "min": 5000,
  "max": 5000
}

GET /summary_stats/department_sub_department_salary/:department/:sub_department

Fetches summary statistics for salaries of employees in a specific department and sub-department.

URL parameters:

/summary_stats/department_sub_department_salary/Engineering/Platform

Example Response:

{
  "mean": 5000,
  "min": 5000,
  "max": 5000
}

Please replace <token> with the actual token received from the /login endpoint.

Error Handling

The API will return an error response if:

  • The request does not include a valid token.
  • The payload for adding a record is invalid.
  • A record index is invalid or out of range.
  • There are no records found for a specific query.

Testing

This application includes a test suite that tests all the API endpoints. The tests are written using the Chai assertion library and the Chai HTTP plugin to perform HTTP requests.

Dependencies

  • Chai
  • Chai HTTP

Running the Tests

To run the tests, use the following command:

npm test

Test Suite

The test suite includes the following tests:

  1. User login and token generation.
  2. Adding a new record to the dataset.
  3. Deleting a record by index from the dataset.
  4. Fetching summary statistics for salary over the entire dataset.
  5. Fetching summary statistics for salaries of records with "on_contract": "true".
  6. Fetching summary statistics for salaries of employees in a specific department.
  7. Fetching summary statistics for salaries of employees in a specific department and sub-department.

The tests check the HTTP response status code and the structure and content of the response body to ensure the API is functioning correctly.

Contributing

If you want to add more tests or improve the existing ones, feel free to submit a pull request.

slice---dice's People

Contributors

astrra avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.