Skip to content

Ka4tik3y/event-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Event Management API

A comprehensive REST API for managing events and user registrations built with Node.js, Express, and MongoDB.

πŸš€ Features

  • Event Management: Create, view, and manage events with capacity limits
  • User Registration: Register users for events with duplicate and capacity validation
  • Smart Filtering: List upcoming events with custom sorting
  • Real-time Stats: Get event statistics and capacity information
  • Robust Validation: Manual input validation and error handling
  • MongoDB Integration: Efficient data storage with proper indexing

πŸ“‹ Prerequisites

  • Node.js (v14 or higher)
  • MongoDB (v4.4 or higher)
  • npm or yarn

πŸ› οΈ Installation & Setup

  1. Clone the repository
git clone <repository-url>
cd event-management-api
  1. Install dependencies
npm install
  1. Environment Setup Create a .env file in the root directory:
PORT=3000
MONGODB_URI=mongodb://localhost:5002/event_management
NODE_ENV=development
  1. Start MongoDB Make sure MongoDB is running on your system:
# For macOS with Homebrew
brew services start mongodb/brew/mongodb-community

# For Ubuntu/Debian
sudo systemctl start mongod

# For Windows
net start MongoDB
  1. Initialize Database
npm run setup
  1. Start the server
# Development mode
npm run dev

# Production mode
npm start

The API will be available at http://localhost:5002

πŸ“š API Documentation

Base URL

http://localhost:5002/api

Authentication

Currently, no authentication is required. All endpoints are publicly accessible.


🎯 Events Endpoints

1. Create Event

POST /events

Creates a new event with the specified details.

Request Body:

{
  "title": "Tech Conference 2024",
  "date_time": "2024-08-15T10:00:00.000Z",
  "location": "San Francisco",
  "capacity": 100
}

Success Response (201):

{
  "message": "Event created successfully",
  "event_id": "60f7b3b3b3b3b3b3b3b3b3b3",
  "event": {
    "id": "60f7b3b3b3b3b3b3b3b3b3b3",
    "title": "Tech Conference 2024",
    "date_time": "2024-08-15T10:00:00.000Z",
    "location": "San Francisco",
    "capacity": 100,
    "registrations": []
  }
}

Validation Rules:

  • title: Required, 1-255 characters
  • date_time: Required, ISO 8601 format
  • location: Required, 1-255 characters
  • capacity: Required, integer between 1-1000

2. Get Event Details

GET /events/{id}

Retrieves detailed information about a specific event including registered users.

Success Response (200):

{
  "id": "60f7b3b3b3b3b3b3b3b3b3b3",
  "title": "Tech Conference 2024",
  "date_time": "2024-08-15T10:00:00.000Z",
  "location": "San Francisco",
  "capacity": 100,
  "registrations": [
    {
      "_id": "60f7b3b3b3b3b3b3b3b3b3b4",
      "name": "John Doe",
      "email": "john@example.com"
    }
  ],
  "remaining_capacity": 99,
  "capacity_percentage": "1.00"
}

3. Register for Event

POST /events/{id}/register

Registers a user for a specific event.

Request Body:

{
  "user_id": "60f7b3b3b3b3b3b3b3b3b3b4"
}

Success Response (200):

{
  "message": "Registration successful",
  "event_id": "60f7b3b3b3b3b3b3b3b3b3b3",
  "user_id": "60f7b3b3b3b3b3b3b3b3b3b4",
  "remaining_capacity": 99
}

Business Rules:

  • Cannot register for past events
  • Cannot register if event is full
  • Cannot register the same user twice
  • User must exist in the database

4. Cancel Registration

DELETE /events/{id}/register

Cancels a user's registration for an event.

Request Body:

{
  "user_id": "60f7b3b3b3b3b3b3b3b3b3b4"
}

Success Response (200):

{
  "message": "Registration cancelled successfully",
  "event_id": "60f7b3b3b3b3b3b3b3b3b3b3",
  "user_id": "60f7b3b3b3b3b3b3b3b3b3b4",
  "remaining_capacity": 100
}

5. List Upcoming Events

GET /events

Returns all future events with custom sorting.

Success Response (200):

{
  "count": 2,
  "events": [
    {
      "id": "60f7b3b3b3b3b3b3b3b3b3b3",
      "title": "Tech Conference 2024",
      "date_time": "2024-08-15T10:00:00.000Z",
      "location": "San Francisco",
      "capacity": 100,
      "total_registrations": 1,
      "remaining_capacity": 99,
      "capacity_percentage": "1.00"
    }
  ]
}

Sorting Logic:

  1. First by date (ascending)
  2. Then by location (alphabetically)

6. Event Statistics

GET /events/{id}/stats

Returns statistical information about an event.

Success Response (200):

{
  "event_id": "60f7b3b3b3b3b3b3b3b3b3b3",
  "title": "Tech Conference 2024",
  "total_registrations": 1,
  "remaining_capacity": 99,
  "capacity_percentage": 1.0
}

πŸ‘₯ Users Endpoints

1. Create User

POST /users

Creates a new user in the system.

Request Body:

{
  "name": "John Doe",
  "email": "john@example.com"
}

Success Response (201):

{
  "message": "User created successfully",
  "user": {
    "id": "60f7b3b3b3b3b3b3b3b3b3b4",
    "name": "John Doe",
    "email": "john@example.com"
  }
}

2. Get User Details

GET /users/{id}

Retrieves information about a specific user.

Success Response (200):

{
  "id": "60f7b3b3b3b3b3b3b3b3b3b4",
  "name": "John Doe",
  "email": "john@example.com"
}

3. List All Users

GET /users

Returns all users in the system.

Success Response (200):

{
  "count": 1,
  "users": [
    {
      "id": "60f7b3b3b3b3b3b3b3b3b3b4",
      "name": "John Doe",
      "email": "john@example.com"
    }
  ]
}

πŸ”§ Development

Available Scripts

  • npm start - Start the production server
  • npm run dev - Start the development server with auto-reload
  • npm run setup - Initialize database and create sample data

Environment Variables

Variable Description Default
PORT Server port 5002
MONGODB_URI MongoDB connection string mongodb://localhost:27017/event_management
NODE_ENV Environment mode development

πŸš€ Features Implemented

βœ… Core Requirements:

  • Event CRUD operations
  • User registration/cancellation
  • Capacity management
  • Past event validation
  • Duplicate registration prevention

βœ… Advanced Features:

  • Custom sorting algorithm
  • Real-time statistics
  • Comprehensive error handling
  • Input validation
  • MongoDB indexing for performance

βœ… Business Logic:

  • Prevent double registrations
  • Capacity enforcement
  • Past event restrictions
  • Proper HTTP status codes

About

Event Mangement REST api

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published