Class ToDoHandler


public class ToDoHandler extends RouteBasedHandler
RESTful API handler for managing ToDo items with full CRUD operations.

This handler extends RouteBasedHandler to provide a complete REST API for ToDo list management. It maintains an in-memory store of ToDo items and supports all standard CRUD operations (Create, Read, Update, Delete) along with proper CORS headers for cross-origin requests.

Supported operations:

  • GET /api/todos - Retrieve all ToDo items
  • POST /api/todos - Create a new ToDo item
  • PUT /api/todos/:id - Update an existing ToDo item by ID
  • DELETE /api/todos/:id - Delete a ToDo item by ID
  • OPTIONS /api/todos - CORS preflight for collection endpoint
  • OPTIONS /api/todos/:id - CORS preflight for item endpoint

Data model: Each ToDo item contains:

  • id: UUID - Unique identifier (auto-generated)
  • title: String - Description of the task
  • completed: boolean - Completion status

CORS support: All endpoints include Access-Control-Allow-Origin: * headers, enabling requests from any origin. This is suitable for development but should be restricted in production environments.

Storage: This implementation uses in-memory storage (HashMap), meaning all data is lost when the server restarts. For production use, consider integrating with a persistent database.

Example usage:

// Create a new ToDo
POST /api/todos
Content-Type: application/json

{"title": "Buy groceries", "completed": false}

// Response: 201 Created
{"id": "123e4567-e89b-12d3-a456-426614174000", "title": "Buy groceries", "completed": false}

// Get all ToDos
GET /api/todos

// Response: 200 OK
[{"id": "123e4567-e89b-12d3-a456-426614174000", "title": "Buy groceries", "completed": false}]

Thread-safety: This handler is NOT thread-safe due to the use of HashMap without synchronization. Concurrent modifications may lead to race conditions. For production use, consider using ConcurrentHashMap or external synchronization.

See Also:
  • Constructor Details

    • ToDoHandler

      public ToDoHandler()
      Constructs a ToDoHandler and initializes the routing configuration.

      This constructor performs two primary operations:

      1. Initializes an empty in-memory store for ToDo items
      2. Registers six route mappings for the REST API endpoints

      Registered routes:

      • GET /api/todos → sendAllToDos(HTTPRequest)
      • POST /api/todos → addTodo(HTTPRequest)
      • PUT /api/todos/:id → updateTodo(HTTPRequest)
      • DELETE /api/todos/:id → deleteTodo(HTTPRequest)
      • OPTIONS /api/todos → handleOptions(HTTPRequest)
      • OPTIONS /api/todos/:id → handleOptions(HTTPRequest)

      Route keys combine HTTP method and path (e.g., "GET /api/todos") and map to method references that handle the corresponding requests. The :id parameter in routes is resolved by the parent RouteBasedHandler.