feat:清除gitignore文件
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
105
schemas/blog.py
105
schemas/blog.py
@@ -1,105 +0,0 @@
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
class BlogQuery(BaseModel):
|
||||
# 类别
|
||||
category: Optional[str] = None
|
||||
# 标题
|
||||
title: Optional[str] = None
|
||||
# 年份
|
||||
year: Optional[int] = None
|
||||
|
||||
|
||||
class BlogBase(BaseModel):
|
||||
title: str
|
||||
topValue: int
|
||||
isGreat: bool
|
||||
category: str
|
||||
content: Optional[str] = None
|
||||
|
||||
|
||||
class BlogCreate(BlogBase):
|
||||
pass
|
||||
|
||||
|
||||
class BlogUpdate(BlogBase):
|
||||
pass
|
||||
|
||||
|
||||
class BlogResponse(BlogBase):
|
||||
id: Optional[int] = None
|
||||
summary: Optional[str] = None
|
||||
wordCount: Optional[int] = None
|
||||
readDuration: Optional[float] = None
|
||||
visitCount: Optional[int] = None
|
||||
createTime: Optional[datetime] = None
|
||||
updateTime: Optional[datetime] = None
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
json_encoders = {
|
||||
# 自定义 datetime 类型的序列化格式
|
||||
datetime: lambda dt: dt.strftime('%Y-%m-%d %H:%M:%S')
|
||||
}
|
||||
|
||||
|
||||
class BlogCategoryResponse(BaseModel):
|
||||
name: str
|
||||
count: int
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class BlogStatsResponse(BaseModel):
|
||||
blogCount: int
|
||||
categoryCount: int
|
||||
wordCount: int
|
||||
|
||||
|
||||
class BlogVisitResponse(BaseModel):
|
||||
ip: str
|
||||
os: str
|
||||
browser: str
|
||||
uri: str
|
||||
title: Optional[str] = None
|
||||
visitTime: datetime
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
json_encoders = {
|
||||
datetime: lambda dt: dt.strftime('%Y-%m-%d %H:%M:%S')
|
||||
}
|
||||
|
||||
|
||||
class BlogLatestResponse(BaseModel):
|
||||
id: int
|
||||
title: str
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class BlogCommentCreate(BaseModel):
|
||||
blogId: int
|
||||
parentId: int
|
||||
name: str
|
||||
website: Optional[str] = None
|
||||
content: str
|
||||
|
||||
|
||||
class BlogCommentResponse(BlogCommentCreate):
|
||||
id: int
|
||||
ipAddress: str
|
||||
userAgent: str
|
||||
createTime: datetime
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
json_encoders = {
|
||||
# 自定义 datetime 类型的序列化格式
|
||||
datetime: lambda dt: dt.strftime('%Y-%m-%d %H:%M:%S')
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
from sqlalchemy import select, func
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from schemas.pagination import PageResult
|
||||
|
||||
|
||||
def paginate_query(db: Session, query, current_page: int = 1, page_size: int = 10) -> PageResult:
|
||||
"""通用分页查询函数"""
|
||||
# 计算总记录数
|
||||
total = db.execute(select(func.count()).select_from(query.subquery())).scalar_one_or_none() or 0
|
||||
|
||||
# 计算总页数
|
||||
total_pages = (total + page_size - 1) // page_size if page_size != 0 else 0
|
||||
|
||||
# 执行分页查询
|
||||
results = db.execute(query.offset((current_page - 1) * page_size).limit(page_size)).all()
|
||||
|
||||
# 转换为字典列表
|
||||
records = [row._asdict() if hasattr(row, "_asdict") else dict(row) for row in results]
|
||||
|
||||
return PageResult(
|
||||
current=current_page,
|
||||
size=page_size,
|
||||
total=total,
|
||||
pages=total_pages,
|
||||
records=records
|
||||
)
|
||||
@@ -1,42 +0,0 @@
|
||||
from typing import List, TypeVar, Generic
|
||||
|
||||
from pydantic import BaseModel, field_validator
|
||||
|
||||
|
||||
class PaginationParams(BaseModel):
|
||||
currentPage: int = 1 # 当前页码,从 1 开始
|
||||
pageSize: int = 10 # 每页数据量
|
||||
|
||||
class Config:
|
||||
validate_assignment = True
|
||||
|
||||
@field_validator("pageSize")
|
||||
def limit_page_size(cls, v):
|
||||
if v > 100:
|
||||
raise ValueError("pageSize 最大为 100")
|
||||
return v
|
||||
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
class PageResult(BaseModel, Generic[T]):
|
||||
"""通用分页结果模型"""
|
||||
current: int = 1 # 当前页码(从 1 开始)
|
||||
size: int = 10 # 每页大小
|
||||
total: int = 0 # 总记录数
|
||||
pages: int = 0 # 总页数
|
||||
records: List[T] = [] # 数据列表
|
||||
|
||||
@field_validator("size")
|
||||
def validate_page_size(cls, v):
|
||||
if v < 1:
|
||||
raise ValueError("page_size 必须至少为 1")
|
||||
if v > 1000:
|
||||
raise ValueError("page_size 最大为 1000")
|
||||
return v
|
||||
|
||||
@property
|
||||
def offset(self) -> int:
|
||||
"""计算偏移量"""
|
||||
return (self.page - 1) * self.size
|
||||
@@ -1,6 +0,0 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class SessionResponse(BaseModel):
|
||||
access_token: str
|
||||
token_type: str
|
||||
Reference in New Issue
Block a user