180 lines
5.8 KiB
Python
180 lines
5.8 KiB
Python
# Copyright (c) 2025, JINGROW and contributors
|
|
# For license information, please see license.txt
|
|
|
|
"""
|
|
提供对云端数据库的增删改查操作
|
|
"""
|
|
|
|
from fastapi import APIRouter, HTTPException, Request
|
|
from fastapi.responses import JSONResponse
|
|
from typing import Dict, Any, Optional
|
|
import json
|
|
import jingrow
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/api/data/{pagetype}")
|
|
async def get_records(
|
|
request: Request,
|
|
pagetype: str,
|
|
fields: Optional[str] = None,
|
|
filters: Optional[str] = None,
|
|
order_by: str = "modified desc",
|
|
limit_start: int = 0,
|
|
limit_page_length: int = 20
|
|
):
|
|
"""获取记录列表"""
|
|
try:
|
|
try:
|
|
fields_list = json.loads(fields) if fields else []
|
|
except Exception:
|
|
raise
|
|
try:
|
|
filters_list = json.loads(filters) if filters else []
|
|
except Exception:
|
|
raise
|
|
|
|
if limit_page_length == 0:
|
|
limit = None
|
|
else:
|
|
limit = limit_start + limit_page_length
|
|
|
|
try:
|
|
data = jingrow.get_list(
|
|
pagetype=pagetype,
|
|
filters=filters_list,
|
|
fields=fields_list,
|
|
limit=limit
|
|
)
|
|
except Exception:
|
|
raise
|
|
|
|
if not isinstance(data, list):
|
|
raise HTTPException(status_code=400, detail='获取记录列表失败')
|
|
|
|
if limit_page_length > 0:
|
|
if limit_start > 0:
|
|
data = data[limit_start:]
|
|
data = data[:limit_page_length]
|
|
|
|
estimated_total = limit_start + len(data)
|
|
if limit_page_length > 0 and len(data) < limit_page_length:
|
|
estimated_total = limit_start + len(data)
|
|
elif limit_page_length > 0:
|
|
estimated_total = limit_start + limit_page_length + 1
|
|
|
|
return JSONResponse(content={
|
|
"data": data,
|
|
"total": estimated_total
|
|
})
|
|
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
@router.get("/api/data/{pagetype}/{name}")
|
|
async def get_record_api(request: Request, pagetype: str, name: str):
|
|
"""获取单个记录"""
|
|
try:
|
|
data = jingrow.get_pg(pagetype, name)
|
|
if data is None:
|
|
raise HTTPException(status_code=404, detail='记录不存在')
|
|
return JSONResponse(content={"data": data})
|
|
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
@router.post("/api/data/{pagetype}")
|
|
async def create_record_api(request: Request, pagetype: str, data: Dict[str, Any]):
|
|
"""创建记录"""
|
|
try:
|
|
created_data = jingrow.create_pg(pagetype, data) or {}
|
|
if not isinstance(created_data, dict):
|
|
raise HTTPException(status_code=400, detail='创建记录失败')
|
|
|
|
return JSONResponse(content={
|
|
"message": "Created successfully",
|
|
"data": created_data
|
|
})
|
|
|
|
except HTTPException:
|
|
raise
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
@router.put("/api/data/{pagetype}/{name}")
|
|
async def update_record_api(request: Request, pagetype: str, name: str, data: Dict[str, Any]):
|
|
"""更新记录"""
|
|
try:
|
|
updated = jingrow.update_pg(pagetype, name, data)
|
|
if updated is False:
|
|
raise HTTPException(status_code=400, detail='更新记录失败')
|
|
return JSONResponse(content={
|
|
"message": "Updated successfully",
|
|
"data": (updated or {}) if isinstance(updated, dict) else {}
|
|
})
|
|
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
@router.delete("/api/data/{pagetype}/{name}")
|
|
async def delete_record_api(request: Request, pagetype: str, name: str):
|
|
"""删除记录"""
|
|
try:
|
|
ok = jingrow.delete_pg(pagetype, name)
|
|
if not ok:
|
|
raise HTTPException(status_code=400, detail='删除记录失败')
|
|
|
|
return JSONResponse(content={
|
|
"message": "Deleted successfully"
|
|
})
|
|
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
@router.post("/api/action/jingrow.client.delete")
|
|
async def batch_delete_records(request: Request, data: Dict[str, Any]):
|
|
"""批量删除记录"""
|
|
try:
|
|
pagetype = data.get('pagetype')
|
|
name = data.get('name')
|
|
|
|
if not pagetype or not name:
|
|
raise HTTPException(status_code=400, detail="缺少必要参数")
|
|
|
|
return await delete_record_api(request, pagetype, name)
|
|
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
@router.post("/api/action/jingrow.client.get_count")
|
|
async def get_record_count(request: Request, pagetype: str):
|
|
"""获取记录总数(估算)"""
|
|
try:
|
|
data = jingrow.get_list(pagetype=pagetype, limit=100)
|
|
if not isinstance(data, list):
|
|
raise HTTPException(status_code=400, detail='获取记录总数失败')
|
|
if len(data) < 100:
|
|
estimated_count = len(data)
|
|
else:
|
|
estimated_count = len(data) + 1
|
|
return JSONResponse(content={"message": estimated_count})
|
|
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
@router.get("/api/single/{pagetype}")
|
|
async def get_single(request: Request, pagetype: str):
|
|
"""获取single类型pagetype记录"""
|
|
try:
|
|
result = jingrow.get_single(pagetype)
|
|
if isinstance(result, dict) and result.get('success'):
|
|
return JSONResponse(content={"data": result.get('config', {})})
|
|
raise HTTPException(status_code=404, detail=(result or {}).get('error', '配置不存在') if isinstance(result, dict) else '配置不存在')
|
|
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|