init: 从 /root/zikai 根目录迁入
把原本散落在 /root/zikai 的 FastAPI 服务整理到 server/ 子目录, 开启独立 git 与 venv。运行时数据(config.yaml / keys / uploads) 按 .gitignore 留在工作目录但不入仓。
This commit is contained in:
5
app/dao/__init__.py
Normal file
5
app/dao/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
||||
"""DAO 层:数据库访问的唯一入口。"""
|
||||
|
||||
from .uploaded_file_dao import UploadedFileDAO
|
||||
|
||||
__all__ = ["UploadedFileDAO"]
|
||||
39
app/dao/uploaded_file_dao.py
Normal file
39
app/dao/uploaded_file_dao.py
Normal file
@@ -0,0 +1,39 @@
|
||||
"""UploadedFile 的 DAO。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..models.uploaded_file import UploadedFile
|
||||
|
||||
|
||||
class UploadedFileDAO:
|
||||
def __init__(self, db: Session) -> None:
|
||||
self.db = db
|
||||
|
||||
def create(self, file: UploadedFile) -> UploadedFile:
|
||||
self.db.add(file)
|
||||
self.db.commit()
|
||||
self.db.refresh(file)
|
||||
return file
|
||||
|
||||
def get_by_id(self, file_id: int) -> UploadedFile | None:
|
||||
return self.db.get(UploadedFile, file_id)
|
||||
|
||||
def list(self, limit: int = 100, offset: int = 0) -> list[UploadedFile]:
|
||||
stmt = (
|
||||
select(UploadedFile)
|
||||
.order_by(UploadedFile.uploaded_at.desc())
|
||||
.limit(limit)
|
||||
.offset(offset)
|
||||
)
|
||||
return list(self.db.scalars(stmt).all())
|
||||
|
||||
def delete(self, file_id: int) -> bool:
|
||||
file = self.get_by_id(file_id)
|
||||
if file is None:
|
||||
return False
|
||||
self.db.delete(file)
|
||||
self.db.commit()
|
||||
return True
|
||||
Reference in New Issue
Block a user