Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Powerbank Rental API (pbrsAPI)

A RESTful API for a powerbank sharing ecosystem. This service allows users to register, authenticate, view available powerbanks at specific stations, start a rental session, and securely return powerbanks.

🚀 Tech Stack

  • Backend: Node.js, Express.js
  • Database: PostgreSQL (using pg & pg-pool)
  • Authentication: JSON Web Tokens (JWT)

🛠️ Installation & Setup

1. Clone the Repository

git clone https://github.com/Kristian-Banar/pbrServiceAPI.git

2. Configure Environment Variables (.env)

PORT=3000
JWT_SECRET=your_super_secure_jwt_secret_key

DB_USER=user
DB_HOST=0.0.0.0
DB_NAME=pbrs_db
DB_PASSWORD=your_postgres_password
DB_PORT=5432

3. Run the Application

docker-compose up -d --build

The server will be live and running at http://localhost:3000

API Documentation (Endpoints)

Authentication & Authorization

Method Endpoint Description Access
POST /api/register Register a new user Public
POST /api/login Authenticate user & receive JWT Public
GET /api/profile Retrieve the logged-in user's profile Private (JWT Required)

Stations & Powerbanks

Method Endpoint Description Access
GET /api/stations/:station_id/powerbanks Get all powerbanks at a specific station Public

Rental Operations

Method Endpoint Description Access
POST /rents/:id/powerbank Start renting a powerbank Private (JWT Required)
POST /rents/:id/return Return a powerbank (terminate rental session) Private (JWT Required)

Sample Request & Response Payloads

1. User Login (POST /api/login)

  • URL Parameters: None
  • Body (JSON):
{
      "username": "user",
      "password": "password"
}
  • Response (200 OK):
{
      "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.ey..."
}

2. View Powerbanks (GET /api/stations/2/powerbanks)

  • URL Parameters: station_id (e.g., 2)
  • Response (200 OK):
[
      {
            "id": 105,
            "station_id": 2,
            "status": "available",
            "charge_level": 89
      },
      {
            "id": 108,
            "station_id": 2,
            "status": "available",
            "charge_level": 94
      }
]

3. Handle Errors (e.g., Missing Station ID)

  • Response (400 Bad Request):
{
      "error": "Station ID not specified"
}

Database Schema Overview

The relational PostgreSQL database consists of the following core entities:

users

id username password powerbank_id age

rents

id cost station_id status

stations

id number_of_powerbanks location

About

Power bank rental service (API).

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages