Skip to content

This is a RESTful API built using Node.js, Express, and PostgreSQL. It allows CRUD operations on rooms and reservations. The key business rule is preventing overlapping reservations for the same room, which I enforce at the application level using date comparisons in SQL queries. I also added proper validation, error handling, and HTTP status codes

Notifications You must be signed in to change notification settings

ShakeelGadafi/Room_booking_API

Repository files navigation

Room Booking API

A RESTful API for managing room bookings and reservations, built with Node.js, Express, and PostgreSQL.

Prerequisites

  • Node.js (v14 or higher recommended)
  • PostgreSQL

Installation

  1. Clone the repository:

    git clone https://github.com/ShakeelGadafi/Room_booking_API.git
    cd Room_booking_API
  2. Install dependencies:

    npm install
    npm i pg express dotenv nodemon

Configuration

  1. Create a .env file in the root directory.

  2. Add the following environment variables with your PostgreSQL database credentials:

    DB_HOST=localhost
    DB_PORT=5432
    DB_USER=your_username
    DB_PASSWORD=your_password
    DB_NAME=your_database_name

    Note: Ensure the database specified in DB_NAME exists.

Database Setup

You need to create the necessary tables in your PostgreSQL database. Run the following SQL queries:

-- Create rooms table
CREATE TABLE rooms (
    id SERIAL PRIMARY KEY,
    room_number VARCHAR(10) UNIQUE NOT NULL,
    category VARCHAR(50) NOT NULL,
    price DECIMAL(10, 2) NOT NULL,
    beds INTEGER NOT NULL
);

-- Create reservations table
CREATE TABLE reservations (
    id SERIAL PRIMARY KEY,
    room_id INTEGER REFERENCES rooms(id) ON DELETE CASCADE,
    guest_name VARCHAR(100) NOT NULL,
    check_in DATE NOT NULL,
    check_out DATE NOT NULL
);

Running the Application

To start the server in development mode (using nodemon):

npm start

The server will start on http://localhost:3000.

Running Tests

To run the test suite (using Jest):

npm test

API Endpoints

Rooms

  • List Rooms: GET /rooms
  • Create Room: POST /rooms
  • Update Room: PUT /rooms/:id
  • Delete Room: DELETE /rooms/:id

Reservations

  • List Reservations: GET /reservations
  • List Reservations by Room: GET /reservations/room/:roomId
  • Create Reservation: POST /reservations
  • Update Reservation: PUT /reservations/:id
  • Delete Reservation: DELETE /reservations/:id

Project Structure

  • app.js: Express application setup.
  • server.js: Server entry point.
  • configure/db.js: Database connection configuration.
  • controllers/: Request handlers.
  • routes/: API route definitions.
  • testing/: Unit and integration tests.

About

This is a RESTful API built using Node.js, Express, and PostgreSQL. It allows CRUD operations on rooms and reservations. The key business rule is preventing overlapping reservations for the same room, which I enforce at the application level using date comparisons in SQL queries. I also added proper validation, error handling, and HTTP status codes

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published