Event
The event.controller.ts file manages world events, including creation, retrieval, joining, and leaving events. These endpoints allow users to interact with events in a specific world.
Note: All event routes use the
requireAuthmiddleware to ensure the user is authenticated. For more details, see the requireAuth documentation.
Key Functions
createEvent
Creates a new event in a specified world and automatically RSVPs the creator.
Flow:
- Extracts the
idparameter from the request at the endpoint/api/event/:id(whereidis the world ID). - Reads
name,description, andscheduledAtfrom the request body. - Creates the event in the database, associating it with the world and creator.
- Automatically creates an RSVP for the creator.
- Returns a success response.
getWorldEvents
Retrieves all events for a specific world, including RSVP details.
Flow:
- Extracts the
idparameter from the request at the endpoint/api/event/:id(whereidis the world ID). - Queries the database for all events in the specified world.
- Includes RSVP information and usernames for each event.
- Returns a JSON response with the list of events and RSVP details.
joinEvent
Allows a user to RSVP (join) an event.
Flow:
- Extracts the
idparameter from the request at the endpoint/api/event/join/:id(whereidis the event ID). - Uses the authenticated user's ID from the request.
- Creates an RSVP record in the database.
- Returns a success response.
leaveEvent
Allows a user to remove their RSVP (leave) an event.
Flow:
- Extracts the
idparameter from the request at the endpoint/api/event/:id(whereidis the event ID). - Uses the authenticated user's ID from the request.
- Deletes the RSVP record from the database.
- Returns a success response.
Routes
The following routes are defined in event.routes.ts and connect HTTP requests to the corresponding controller functions:
| Method | Path | Description | Controller Function | Middleware |
|---|---|---|---|---|
| POST | /api/event/:id | Create a new event in a world | createEvent | requireAuth |
| POST | /api/event/join/:id | RSVP (join) an event | joinEvent | requireAuth |
| GET | /api/event/:id | Get all events for a world | getWorldEvents | requireAuth |
| DELETE | /api/event/:id | Remove RSVP (leave) an event | leaveEvent | requireAuth |
Error Handling
- All errors are caught and returned as a
500 Internal Server Errorwith the error details in the response JSON.
Example Requests & Responses
Create Event
Request:
POST /api/event/:id
Content-Type: application/json
{
"name": "Boss Raid",
"description": "Defeat the world boss together!",
"scheduledAt": "2025-09-01T18:00:00.000Z"
}
Successful Response:
{
"success": true
}
Get World Events
Request:
GET /api/event/:id
Successful Response:
{
"events": [
{
"id": "event1",
"name": "Boss Raid",
"description": "Defeat the world boss together!",
"scheduledAt": "2025-09-01T18:00:00.000Z",
"RSVPs": [{ "user": { "username": "player1" } }]
}
// ...more events
]
}
Join Event
Request:
POST /api/event/join/:id
Successful Response:
{
"success": true
}
Leave Event
Request:
DELETE /api/event/:id
Successful Response:
{
"success": true
}
Usage Instructions
- Create an Event:
UsePOST /api/event/:idwith event details in the request body to create a new event in a world. - View Events:
UseGET /api/event/:idto retrieve all events for a world. - Join an Event:
UsePOST /api/event/join/:idto RSVP to an event. - Leave an Event:
UseDELETE /api/event/:idto remove your RSVP from an event.
Dependencies
- Express: Handles HTTP requests and responses.
- Prisma: ORM for