43 lines
1.5 KiB
Python
43 lines
1.5 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from sqlalchemy import text
|
|
from app.api.db import get_db
|
|
from pydantic import BaseModel
|
|
from app.settings import SUBMIT_RECORD_TABLE
|
|
from datetime import datetime
|
|
import pytz
|
|
|
|
router = APIRouter()
|
|
|
|
class SubmitRecordRequest(BaseModel):
|
|
user_email: str
|
|
operation_type: str
|
|
storage_path: str
|
|
|
|
@router.post("/submit-record")
|
|
async def submit_record(request: SubmitRecordRequest, db: Session = Depends(get_db)):
|
|
try:
|
|
if request.operation_type not in ['upload', 'delete']:
|
|
raise HTTPException(status_code=400, detail="操作类型必须是 'upload' 或 'delete'")
|
|
|
|
china_tz = pytz.timezone('Asia/Shanghai')
|
|
operation_time = datetime.now(china_tz)
|
|
|
|
query = text(f"""
|
|
INSERT INTO {SUBMIT_RECORD_TABLE} (user_email, operation_time, operation_type, storage_path)
|
|
VALUES (:user_email, :operation_time, :operation_type, :storage_path)
|
|
""")
|
|
|
|
db.execute(query, {
|
|
"user_email": request.user_email,
|
|
"operation_time": operation_time,
|
|
"operation_type": request.operation_type,
|
|
"storage_path": request.storage_path
|
|
})
|
|
db.commit()
|
|
|
|
return {"status": "success", "message": "操作记录已保存"}
|
|
|
|
except Exception as e:
|
|
db.rollback()
|
|
raise HTTPException(status_code=500, detail=f"服务器错误:{str(e)}") |