World
The world.controller.ts file manages worlds, including creation, joining, retrieving user worlds, admin data, and updating world details. These endpoints allow users to interact with worlds and manage their membership and settings.
Note: All world routes use the
requireAuthmiddleware to ensure the user is authenticated. Some routes also use therequireRolemiddleware to restrict access to users with specific roles (OWNER, ADMIN, SUB_ADMIN). For more details, see the requireAuth and requireRole documentation.
Key Functions
createWorld
Creates a new world and assigns the creator as OWNER.
Flow:
- Reads
nameanddescriptionfrom the request body. - Generates a unique join code.
- Creates the world in the database with the join code.
- Assigns the authenticated user as OWNER via a membership record.
- Initializes boss progress for the world.
- Returns the new world's ID.
joinWorld
Allows a user to join a world using a join code.
Flow:
- Reads
codefrom the request body. - Finds the world associated with the join code.
- Checks if the user is already a member.
- Adds the user as a MEMBER if not already joined.
- Returns the world's ID.
getUserWorlds
Retrieves all worlds the authenticated user is a member of.
Flow:
- Uses the authenticated user's ID.
- Finds all world memberships for the user.
- Includes world details and membership count.
- Returns the list of user world memberships.
getAdminData
Retrieves admin data for a specific world.
Flow:
- Extracts the world ID from the request parameters.
- Finds the world and includes join code, memberships (with usernames), bosses, notes, and events.
- Returns the admin data.
updateWorldDetails
Updates the name, description, and join code for a world.
Flow:
- Extracts the world ID from the request parameters.
- Reads
name,description, andcodefrom the request body. - Updates the world and join code in the database.
- Returns a success response.
Routes
The following routes are defined in world.routes.ts and connect HTTP requests to the corresponding controller functions:
| Method | Path | Description | Controller Function | Middleware |
|---|---|---|---|---|
| POST | /api/world/create | Create a new world | createWorld | requireAuth |
| POST | /api/world/join | Join a world using a join code | joinWorld | requireAuth |
| GET | /api/world/ | Get worlds for authenticated user | getUserWorlds | requireAuth |
| GET | /api/world/adminData/:id | Get admin data for a world | getAdminData | requireAuth, requireRole(["OWNER", "ADMIN", "SUB_ADMIN"]) |
| PATCH | /api/world/:id | Update world details | updateWorldDetails | requireAuth, requireRole("OWNER") |
Error Handling
- Returns
400 Bad Requestfor missing or invalid data (e.g., join code not found, already a member). - Returns
500 Internal Server Errorfor unexpected errors.
Example Requests & Responses
Create World
Request:
POST /api/world/create
Content-Type: application/json
{
"name": "Adventure Realm",
"description": "A world for epic quests."
}
Successful Response:
{
"worldId": "world123"
}
Join World
Request:
POST /api/world/join
Content-Type: application/json
{
"code": "abc123"
}
Successful Response:
{
"worldId": "world123"
}
Get User Worlds
Request:
GET /api/world/
Cookie: token=<JWT_TOKEN>
Successful Response:
{
"userWorldMemberships": [
{
"role": "OWNER",
"world": {
"id": "world123",
"name": "Adventure Realm",
"description": "A world for epic quests.",
"_count": { "memberships": 5 }
}
}
// ...more memberships
]
}
Get Admin Data
Request:
GET /api/world/adminData/world123
Cookie: token=<JWT_TOKEN>
Successful Response:
{
"adminData": {
"id": "world123",
"name": "Adventure Realm",
"joinCode": { "code": "abc123" },
"memberships": [
{ "user": { "username": "player1" }, "role": "OWNER" }
// ...more members
],
"bosses": [
/* ... */
],
"notes": [
/* ... */
],
"events": [
/* ... */
]
}
}
Update World Details
Request:
PATCH /api/world/world123
Content-Type: application/json
{
"name": "Adventure Realm Updated",
"description": "A world for epic quests and battles.",
"code": "def456"
}
Successful Response:
{
"success": true
}
Usage Instructions
- Create a World:
UsePOST /api/world/createto create a new world. - Join a World:
UsePOST /api/world/joinwith a join code to join an existing world. - View Your Worlds:
UseGET /api/world/to see all worlds you are a member of. - Get Admin Data:
UseGET /api/world/adminData/:idfor detailed world info (requires OWNER, ADMIN, or SUB_ADMIN role). - Update World Details:
UsePATCH /api/world/:idto update world info (requires OWNER role).
Dependencies
- Express: Handles HTTP requests and responses.
- Prisma: ORM for database operations.
- Crypto: For join code