package mantra.rest

RestController

ABSTRACT public abstract class

Base abstract controller for REST resources providing standard method signatures with 405 Method Not Allowed defaults.

🏗️ Constructor Summary

Constructor Signature
def __init__(self, resource_name: String)

⚙️ Method Summary

Modifier & Return Type Method Signature Description
ApiResponse abstract def get_all(req: Request) Handles GET /resource to list entities with optional pagination.
ApiResponse abstract def get_by_id(req: Request, id: Int) Handles GET /resource/{id} to retrieve a single entity by primary identifier.
ApiResponse abstract def create(req: Request) Handles POST /resource to persist a new entity from incoming JSON payload.
ApiResponse abstract def update(req: Request, id: Int) Handles PUT /resource/{id} to update an existing entity.
ApiResponse abstract def delete(req: Request, id: Int) Handles DELETE /resource/{id} to remove an entity.

🔍 Detailed Method Specifications

abstract def get_all(req: Request) -> ApiResponse
Handles GET /resource to list entities with optional pagination.
abstract def get_by_id(req: Request, id: Int) -> ApiResponse
Handles GET /resource/{id} to retrieve a single entity by primary identifier.
abstract def create(req: Request) -> ApiResponse
Handles POST /resource to persist a new entity from incoming JSON payload.
abstract def update(req: Request, id: Int) -> ApiResponse
Handles PUT /resource/{id} to update an existing entity.
abstract def delete(req: Request, id: Int) -> ApiResponse
Handles DELETE /resource/{id} to remove an entity.

💡 Example Usage

rest_controller_example.mantra
class UserController extends RestController:
    def __init__(self, repo: UserRepository):
        super("users")
        self.repo = repo

    override pub def get_all(self, req: Request) -> ApiResponse:
        let users = self.repo.all()
        return ApiResponse.ok(users.to_json())

    override pub def get_by_id(self, req: Request, id: Int) -> ApiResponse:
        let u = self.repo.find(id)
        if u.id == -1:
            return ApiResponse.not_found("User not found")
        return ApiResponse.ok(u.to_json())