58 lines
2.1 KiB
Python
58 lines
2.1 KiB
Python
"""任务模型定义模块。
|
|
|
|
定义任务相关的 Pydantic 数据模型,用于请求校验与响应序列化。
|
|
"""
|
|
|
|
from datetime import datetime, timezone
|
|
from typing import Optional
|
|
from pydantic import BaseModel, Field
|
|
|
|
|
|
class TodoItem(BaseModel):
|
|
"""任务项模型,对应数据库中的一条任务记录。"""
|
|
|
|
id: int = Field(..., description="任务唯一标识")
|
|
title: str = Field(..., min_length=1, max_length=200, description="任务标题")
|
|
description: str = Field(default="", max_length=1000, description="任务描述")
|
|
completed: bool = Field(default=False, description="是否完成")
|
|
created_at: datetime = Field(
|
|
default_factory=lambda: datetime.now(timezone.utc),
|
|
description="创建时间",
|
|
)
|
|
updated_at: datetime = Field(
|
|
default_factory=lambda: datetime.now(timezone.utc),
|
|
description="更新时间",
|
|
)
|
|
|
|
|
|
class TodoCreate(BaseModel):
|
|
"""创建任务请求模型。"""
|
|
|
|
title: str = Field(..., min_length=1, max_length=200, description="任务标题")
|
|
description: str = Field(default="", max_length=1000, description="任务描述")
|
|
|
|
|
|
class TodoUpdate(BaseModel):
|
|
"""更新任务请求模型,所有字段可选。"""
|
|
|
|
title: Optional[str] = Field(None, min_length=1, max_length=200, description="任务标题")
|
|
description: Optional[str] = Field(None, max_length=1000, description="任务描述")
|
|
completed: Optional[bool] = Field(None, description="是否完成")
|
|
|
|
|
|
class TodoResponse(BaseModel):
|
|
"""任务响应模型,包含标准返回结构。"""
|
|
|
|
code: int = Field(default=200, description="状态码")
|
|
message: str = Field(default="success", description="提示信息")
|
|
data: Optional[TodoItem] = Field(None, description="任务数据")
|
|
|
|
|
|
class TodoListResponse(BaseModel):
|
|
"""任务列表响应模型。"""
|
|
|
|
code: int = Field(default=200, description="状态码")
|
|
message: str = Field(default="success", description="提示信息")
|
|
data: list[TodoItem] = Field(default_factory=list, description="任务列表")
|
|
total: int = Field(default=0, description="任务总数")
|