
Create a simple Todo API with FastAPI and SQLite with CRUD functionality, health check, error handling, and API documentation.
71 lines
1.5 KiB
Python
71 lines
1.5 KiB
Python
from datetime import datetime
|
|
from typing import Optional
|
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
|
|
class TodoBase(BaseModel):
|
|
"""
|
|
Base schema for Todo items with common attributes.
|
|
"""
|
|
title: str = Field(
|
|
...,
|
|
min_length=1,
|
|
max_length=100,
|
|
description="The title of the todo item"
|
|
)
|
|
description: Optional[str] = Field(
|
|
None,
|
|
description="An optional description of the todo item"
|
|
)
|
|
completed: bool = Field(
|
|
False,
|
|
description="Whether the todo item is completed"
|
|
)
|
|
|
|
|
|
class TodoCreate(TodoBase):
|
|
"""
|
|
Schema for creating a new Todo item.
|
|
"""
|
|
pass
|
|
|
|
|
|
class TodoUpdate(BaseModel):
|
|
"""
|
|
Schema for updating an existing Todo item.
|
|
All fields are optional to allow partial updates.
|
|
"""
|
|
title: Optional[str] = Field(
|
|
None,
|
|
min_length=1,
|
|
max_length=100,
|
|
description="The title of the todo item"
|
|
)
|
|
description: Optional[str] = Field(
|
|
None,
|
|
description="An optional description of the todo item"
|
|
)
|
|
completed: Optional[bool] = Field(
|
|
None,
|
|
description="Whether the todo item is completed"
|
|
)
|
|
|
|
|
|
class TodoInDBBase(TodoBase):
|
|
"""
|
|
Schema for Todo items as stored in the database.
|
|
"""
|
|
id: int
|
|
created_at: datetime
|
|
updated_at: datetime
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
|
|
class Todo(TodoInDBBase):
|
|
"""
|
|
Schema for Todo items returned to clients.
|
|
"""
|
|
pass |