feat: implement project relational data platform
This commit is contained in:
91
app/api/data_platform.py
Normal file
91
app/api/data_platform.py
Normal file
@@ -0,0 +1,91 @@
|
||||
"""Project database catalog and registered-table CRUD APIs."""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Query
|
||||
|
||||
from app.auth import CurrentUser
|
||||
from app.data_platform.record_service import (
|
||||
create_record,
|
||||
delete_record,
|
||||
list_databases,
|
||||
list_records,
|
||||
list_tables,
|
||||
update_record,
|
||||
)
|
||||
from app.data_platform.schema import ensure_all_project_databases
|
||||
|
||||
router = APIRouter(prefix="/data-platform")
|
||||
|
||||
|
||||
@router.get("/databases")
|
||||
async def databases(_user: CurrentUser):
|
||||
return await list_databases()
|
||||
|
||||
|
||||
@router.post("/databases/initialize")
|
||||
async def initialize_databases(user: CurrentUser):
|
||||
if "admin" not in user.get("roles", []):
|
||||
raise HTTPException(403, "只有系统管理员可以初始化项目数据库")
|
||||
await ensure_all_project_databases()
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
@router.get("/databases/{project_id}/tables")
|
||||
async def tables(project_id: str, _user: CurrentUser):
|
||||
return await list_tables(project_id)
|
||||
|
||||
|
||||
@router.get("/databases/{project_id}/tables/{table_code}/records")
|
||||
async def records(
|
||||
project_id: str,
|
||||
table_code: str,
|
||||
_user: CurrentUser,
|
||||
page: int = Query(default=1, ge=1),
|
||||
page_size: int = Query(default=50, ge=1, le=200),
|
||||
search: str | None = None,
|
||||
sort_field: str | None = None,
|
||||
sort_order: str = Query(default="desc", pattern="^(asc|desc)$"),
|
||||
):
|
||||
return await list_records(
|
||||
project_id,
|
||||
table_code,
|
||||
page=page,
|
||||
page_size=page_size,
|
||||
search=search,
|
||||
sort_field=sort_field,
|
||||
sort_order=sort_order,
|
||||
)
|
||||
|
||||
|
||||
@router.post("/databases/{project_id}/tables/{table_code}/records")
|
||||
async def add_record(
|
||||
project_id: str,
|
||||
table_code: str,
|
||||
body: dict[str, Any],
|
||||
user: CurrentUser,
|
||||
):
|
||||
return await create_record(project_id, table_code, body, user["username"])
|
||||
|
||||
|
||||
@router.patch("/databases/{project_id}/tables/{table_code}/records/{record_id}")
|
||||
async def edit_record(
|
||||
project_id: str,
|
||||
table_code: str,
|
||||
record_id: str,
|
||||
body: dict[str, Any],
|
||||
user: CurrentUser,
|
||||
):
|
||||
return await update_record(project_id, table_code, record_id, body, user["username"])
|
||||
|
||||
|
||||
@router.delete("/databases/{project_id}/tables/{table_code}/records/{record_id}")
|
||||
async def remove_record(
|
||||
project_id: str,
|
||||
table_code: str,
|
||||
record_id: str,
|
||||
user: CurrentUser,
|
||||
):
|
||||
return await delete_record(project_id, table_code, record_id, user["username"])
|
||||
|
||||
Reference in New Issue
Block a user