Note
The note.controller.ts file manages notes within worlds, including creation, retrieval, and deletion. These endpoints allow users to add, view, and remove notes in a specific world.
Note: All note routes use the
requireAuthmiddleware to ensure the user is authenticated. TheDELETE /api/note/:idroute also uses therequireRolemiddleware to restrict access to users with the roles: OWNER, ADMIN, or SUB_ADMIN. For more details, see the requireAuth and requireRole documentation.
Key Functions
createNote
Creates a new note in a specified world.
Flow:
- Extracts the
idparameter from the request at the endpoint/api/note/:id(whereidis the world ID). - Reads
title,content, andtagfrom the request body. - Uses the authenticated user's ID from the request.
- Creates the note in the database, associating it with the world and author.
- Returns a success response.
getWorldNotes
Retrieves all notes for a specific world, including author usernames.
Flow:
- Extracts the
idparameter from the request at the endpoint/api/note/:id(whereidis the world ID). - Queries the database for all notes in the specified world.
- Includes author information for each note.
- Returns a JSON response with the list of notes.
deleteNote
Deletes a specific note.
Flow:
- Extracts the
idparameter from the request at the endpoint/api/note/:id(whereidis the note ID). - Checks the user's role (must be OWNER, ADMIN, or SUB_ADMIN).
- Deletes the note from the database.
- Returns a success response.
Routes
The following routes are defined in note.routes.ts and connect HTTP requests to the corresponding controller functions:
| Method | Path | Description | Controller Function | Middleware |
|---|---|---|---|---|
| POST | /api/note/:id | Create a note in a world | createNote | requireAuth |
| GET | /api/note/:id | Get all notes for a world | getWorldNotes | requireAuth |
| DELETE | /api/note/:id | Delete a note | deleteNote | requireAuth, requireRole(["OWNER", "ADMIN", "SUB_ADMIN"]) |
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 Note
Request:
POST /api/note/:id
Content-Type: application/json
{
"title": "Strategy",
"content": "Focus on the east gate first.",
"tag": "battle"
}
Successful Response:
{
"success": true
}
Get World Notes
Request:
GET /api/note/:id
Successful Response:
{
"notes": [
{
"id": "note1",
"title": "Strategy",
"content": "Focus on the east gate first.",
"tag": "battle",
"author": { "username": "player1" }
}
// ...more notes
]
}
Delete Note
Request:
DELETE /api/note/:id
Successful Response:
{
"success": true
}
Usage Instructions
- Create a Note:
UsePOST /api/note/:idwith note details in the request body to create a new note in a world. - View Notes:
UseGET /api/note/:idto retrieve all notes for a world. - Delete a Note:
UseDELETE /api/note/:idto remove a note (requires OWNER, ADMIN, or SUB_ADMIN role).
Dependencies
- Express: Handles HTTP requests and responses.
- Prisma: ORM for database