""" Models for API responses common to all routers in the application. """ from typing import Generic, TypeVar from pydantic import BaseModel, Field, model_serializer T = TypeVar("T") class BaseCollectionResponse(BaseModel, Generic[T]): """Base model for collection responses. Collections are typically lists of items found in the database. When no items are found, the `data` field will be an empty list. """ data: list[T] = Field(..., description="List of data items.") more: bool = Field( ..., description="Indicates if more items are available beyond this response." ) @property def count(self) -> int: return len(self.data) @model_serializer(mode="plain") def add_count(self): d = self.__dict__.copy() d["count"] = self.count return d class Config: json_schema_extra = { "example": { "data": [{"id": 1, "name": "Item 1"}], "more": False, "count": 1, } } class BaseErrorResponse(BaseModel): """Base model for generic error responses.""" detail: str = Field(..., description="A detailed error message.") class Config: json_schema_extra = {"example": {"detail": "Not found."}}