Skip to content

MdMuzahid07/P-HERO-Level-2-Assignment-3

Repository files navigation

Sports Facility Booking Platform

Overview

Welcome to the Sports Facility Booking Platform! This project is designed to provide a hassle-free way for users to book sports facilities. The backend is built with TypeScript, Express.js, and Mongoose for MongoDB. It includes user authentication, facility management, booking creation, and availability checking.

Table of Contents

Technology Stack

  • Programming Language: TypeScript
  • Web Framework: Express.js
  • ODM & Validation Library: Mongoose for MongoDB
  • Authentication: JWT (JSON Web Tokens)
  • Validation: Zod
  • Package Manager: Yarn

Getting Started

Prerequisites

  • Node.js (v18 or higher)
  • Yarn

Installation

  1. Clone the repository:
    git clone https://github.com/MdMuzahid07/P-HERO-Level-2-Assignment-3.git
  2. Navigate to the project directory:
    cd ./P-HERO-Level-2-Assignment-3
  3. Install dependencies:
    yarn install
  4. Create a .env file in the root directory and add the following environment variables:
DB_USERNAME=
DB_PASSWORD=
DB_URL=
PORT=5000
NODE_ENV=development
BCRYPT_SALT_ROUND=10
JWT_ACCESS_SECRET_KEY=
JWT_REFRESH_SECRET_KEY=
JWT_ACCESS_TOKEN_EXPIRES_IN=3d
JWT_REFRESH_TOKEN_EXPIRES_IN=10d
  1. Start the development server:
    yarn start-dev

Build and Run

To build the project and run it in production mode:

yarn build
yarn start

scripts

yarn lint
yarn lint-fix
yarn prettier
yarn prettier-fix

API Endpoints

User Routes

  1. User Sign Up

    • Route: POST /api/auth/signup
    • Request Body:
      {
        "name": "Programming Hero",
        "email": "web@programming-hero.com",
        "password": "programming-hero",
        "phone": "01322901105",
        "role": "admin", // or 'user'
        "address": "Level-4, 34, Awal Centre, Banani, Dhaka"
      }
    • Response:
      {
        "success": true,
        "statusCode": 200,
        "message": "User registered successfully",
        "data": {
          "_id": "60d9c4e4f3b4b544b8b8d1c4",
          "name": "Programming Hero",
          "email": "web@programming-hero.com",
          "role": "admin",
          "phone": "01322901105",
          "address": "Level-4, 34, Awal Centre, Banani, Dhaka"
        }
      }
  2. User Login

    • Route: POST /api/auth/login
    • Request Body:
      {
        "email": "web@programming-hero.com",
        "password": "programming-hero"
      }
    • Response:
      {
        "success": true,
        "statusCode": 200,
        "message": "User logged in successfully",
        "token": "JWT_TOKEN",
        "data": {
          "_id": "60d9c4e4f3b4b544b8b8d1c4",
          "name": "Programming Hero",
          "email": "web@programming-hero.com",
          "role": "admin",
          "phone": "01322901105",
          "address": "Level-4, 34, Awal Centre, Banani, Dhaka"
        }
      }

Facility Routes

  1. Create a Facility (Admin Only)

    • Route: POST /api/facility
    • Headers:
      Authorization: Bearer JWT_TOKEN
      
    • Request Body:
      {
        "name": "Tennis Court",
        "description": "Outdoor tennis court with synthetic surface.",
        "pricePerHour": 30,
        "location": "456 Sports Ave, Springfield"
      }
    • Response:
      {
        "success": true,
        "statusCode": 200,
        "message": "Facility added successfully",
        "data": {
          "_id": "60d9c4e4f3b4b544b8b8d1c5",
          "name": "Tennis Court",
          "description": "Outdoor tennis court with synthetic surface.",
          "pricePerHour": 30,
          "location": "456 Sports Ave, Springfield",
          "isDeleted": false
        }
      }
  2. Update a Facility (Admin Only)

    • Route: PUT /api/facility/:id
    • Headers:
      Authorization: Bearer JWT_TOKEN
      
    • Request Body:
      {
        "name": "Updated Tennis Court",
        "description": "Updated outdoor tennis court with synthetic surface.",
        "pricePerHour": 35,
        "location": "789 Sports Ave, Springfield"
      }
    • Response:
      {
        "success": true,
        "statusCode": 200,
        "message": "Facility updated successfully",
        "data": {
          "_id": "60d9c4e4f3b4b544b8b8d1c5",
          "name": "Updated Tennis Court",
          "description": "Updated outdoor tennis court with synthetic surface.",
          "pricePerHour": 35,
          "location": "789 Sports Ave, Springfield",
          "isDeleted": false
        }
      }
  3. Delete a Facility - Soft Delete (Admin Only)

    • Route: DELETE /api/facility/:id
    • Headers:
      Authorization: Bearer JWT_TOKEN
      
    • Response:
      {
        "success": true,
        "statusCode": 200,
        "message": "Facility deleted successfully",
        "data": {
          "_id": "60d9c4e4f3b4b544b8b8d1c5",
          "name": "Updated Tennis Court",
          "description": "Updated outdoor tennis court with synthetic surface.",
          "pricePerHour": 35,
          "location": "789 Sports Ave, Springfield",
          "isDeleted": true
        }
      }
  4. Get All Facilities

    • Route: GET /api/facility
    • Response:
      {
        "success": true,
        "statusCode": 200,
        "message": "Facilities retrieved successfully",
        "data": [
          {
            "_id": "60d9c4e4f3b4b544b8b8d1c5",
            "name": "Tennis Court",
            "description": "Outdoor tennis court with synthetic surface.",
            "pricePerHour": 30,
            "location": "456 Sports Ave, Springfield",
            "isDeleted": false
          }
        ]
      }

Booking Routes

  1. Check Availability

    • Route: GET /api/check-availability
    • Query Parameters:
      • date (optional): The date for which availability is to be checked. Format: YYYY-MM-DD. If not provided, today's date will be used by default.
    • Response:
      {
        "success": true,
        "statusCode": 200,
        "message": "Availability checked successfully",
        "data": [
          {
            "startTime": "08:00",
            "endTime": "10:00"
          },
          {
            "startTime": "14:00",
            "endTime": "16:00"
          }
        ]
      }
  2. Create a Booking (User Only)

    • Route: POST /api/bookings
    • Headers:
      Authorization: Bearer JWT_TOKEN
      
    • Request Body:
      {
        "facility": "60d9c4e4f3b4b544b8b8d1c5",
        "date": "2024-06-15",
        "startTime": "10:00",
        "endTime": "13:00"
      }
    • Response:
      {
        "success": true,
        "statusCode": 200,
        "message": "Booking

sports-facility-booking-client