mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-22 05:02:57 +00:00
refactor: 大规模代码重构与项目配置优化
- 调整前端路由前缀为 /web 并新增配置项 - 重构环境变量配置,替换旧环境文件为 dev/prod/test 版本 - 优化后端接口路由与参数校验,替换 dataclass 查询参数为 Pydantic 模型 - 整理后端导入顺序,延迟加载循环依赖模块 - 统一前端 API 调用命名,重构模块命名 - 新增多种响应类型支持,优化代码模板 - 简化后端路由装饰器写法,合并配置参数
This commit is contained in:
@@ -4,31 +4,19 @@ from fastapi import APIRouter, Body, Depends, Path, Query
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from app.common.response import ResponseSchema, StreamResponse, SuccessResponse
|
||||
from app.core.base_params import PaginationQueryParam
|
||||
from app.core.base_schema import AuthSchema, PageResultSchema
|
||||
from app.core.base_schema import AuthSchema, PageResultSchema, PaginationQueryParam
|
||||
from app.core.dependencies import AuthPermission
|
||||
from app.core.logger import logger
|
||||
from app.core.router_class import OperationLogRoute
|
||||
from app.utils.common_util import bytes2file_response
|
||||
|
||||
from .schema import (
|
||||
GenCreateTableSqlBody,
|
||||
GenDBTableSchema,
|
||||
GenSyncPreviewSchema,
|
||||
GenTableOutSchema,
|
||||
GenTableQueryParam,
|
||||
GenTableSchema,
|
||||
)
|
||||
from .schema import GenCreateTableSqlBody, GenDBTableSchema, GenSyncPreviewSchema, GenTableOutSchema, GenTableQueryParam, GenTableSchema
|
||||
from .service import GenTableService
|
||||
|
||||
GenRouter = APIRouter(route_class=OperationLogRoute, prefix="/gencode", tags=["代码生成"])
|
||||
|
||||
|
||||
@GenRouter.get(
|
||||
"/list",
|
||||
summary="查询代码生成业务表列表",
|
||||
response_model=ResponseSchema[list[GenTableOutSchema]],
|
||||
)
|
||||
@GenRouter.get("/list", summary="查询代码生成业务表列表", response_model=ResponseSchema[list[GenTableOutSchema]])
|
||||
async def gen_table_list_controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_generator:gencode:query"]))],
|
||||
page: Annotated[PaginationQueryParam, Query(description="分页参数")],
|
||||
@@ -46,11 +34,7 @@ async def gen_table_list_controller(
|
||||
return SuccessResponse(data=result_dict, msg="获取代码生成业务表列表成功")
|
||||
|
||||
|
||||
@GenRouter.get(
|
||||
"/db/list",
|
||||
summary="查询数据库表列表",
|
||||
response_model=ResponseSchema[PageResultSchema[GenDBTableSchema]],
|
||||
)
|
||||
@GenRouter.get("/db/list", summary="查询数据库表列表", response_model=ResponseSchema[PageResultSchema[GenDBTableSchema]])
|
||||
async def get_gen_db_table_list_controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_generator:dblist:query"]))],
|
||||
page: Annotated[PaginationQueryParam, Query(description="分页参数")],
|
||||
@@ -64,11 +48,7 @@ async def get_gen_db_table_list_controller(
|
||||
return SuccessResponse(data=result_dict, msg="获取数据库表列表成功")
|
||||
|
||||
|
||||
@GenRouter.post(
|
||||
"/import",
|
||||
summary="导入表结构",
|
||||
response_model=ResponseSchema[bool],
|
||||
)
|
||||
@GenRouter.post("/import", summary="导入表结构", response_model=ResponseSchema[bool])
|
||||
async def import_gen_table_controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_generator:gencode:import"]))],
|
||||
table_names: Annotated[list[str], Body(description="表名列表")],
|
||||
@@ -79,11 +59,7 @@ async def import_gen_table_controller(
|
||||
return SuccessResponse(msg="导入表结构成功", data=result)
|
||||
|
||||
|
||||
@GenRouter.get(
|
||||
"/detail/{table_id}",
|
||||
summary="获取业务表详细信息",
|
||||
response_model=ResponseSchema[GenTableOutSchema],
|
||||
)
|
||||
@GenRouter.get("/detail/{table_id}", summary="获取业务表详细信息", response_model=ResponseSchema[GenTableOutSchema])
|
||||
async def gen_table_detail_controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_generator:gencode:query"]))],
|
||||
table_id: Annotated[int, Path(description="业务表ID")],
|
||||
@@ -92,11 +68,7 @@ async def gen_table_detail_controller(
|
||||
return SuccessResponse(data=result, msg="获取业务表详细信息成功")
|
||||
|
||||
|
||||
@GenRouter.post(
|
||||
"/create",
|
||||
summary="创建表结构",
|
||||
response_model=ResponseSchema[bool],
|
||||
)
|
||||
@GenRouter.post("/create", summary="创建表结构", response_model=ResponseSchema[bool])
|
||||
async def create_table_controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_generator:gencode:create"]))],
|
||||
body: Annotated[GenCreateTableSqlBody, Body(description="创建表结构参数")],
|
||||
@@ -105,11 +77,7 @@ async def create_table_controller(
|
||||
return SuccessResponse(msg="创建表结构成功", data=result)
|
||||
|
||||
|
||||
@GenRouter.put(
|
||||
"/update/{table_id}",
|
||||
summary="编辑业务表信息",
|
||||
response_model=ResponseSchema[GenTableOutSchema],
|
||||
)
|
||||
@GenRouter.put("/update/{table_id}", summary="编辑业务表信息", response_model=ResponseSchema[GenTableOutSchema])
|
||||
async def update_gen_table_controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_generator:gencode:update"]))],
|
||||
table_id: Annotated[int, Path(description="业务表ID")],
|
||||
@@ -119,11 +87,7 @@ async def update_gen_table_controller(
|
||||
return SuccessResponse(data=result_dict, msg="编辑业务表信息成功")
|
||||
|
||||
|
||||
@GenRouter.delete(
|
||||
"/delete",
|
||||
summary="删除业务表信息",
|
||||
response_model=ResponseSchema[None],
|
||||
)
|
||||
@GenRouter.delete("/delete", summary="删除业务表信息", response_model=ResponseSchema[None])
|
||||
async def delete_gen_table_controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_generator:gencode:delete"]))],
|
||||
ids: Annotated[list[int], Body(description="业务表ID列表")],
|
||||
@@ -132,10 +96,7 @@ async def delete_gen_table_controller(
|
||||
return SuccessResponse(msg="删除业务表信息成功", data=result)
|
||||
|
||||
|
||||
@GenRouter.patch(
|
||||
"/batch/output",
|
||||
summary="批量生成代码",
|
||||
)
|
||||
@GenRouter.patch("/batch/output", summary="批量生成代码")
|
||||
async def batch_gen_code_controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_generator:gencode:operate"]))],
|
||||
table_names: Annotated[list[str], Body(description="表名列表")],
|
||||
@@ -152,11 +113,7 @@ async def batch_gen_code_controller(
|
||||
)
|
||||
|
||||
|
||||
@GenRouter.post(
|
||||
"/output/{table_name}",
|
||||
summary="生成代码到指定路径",
|
||||
response_model=ResponseSchema[bool],
|
||||
)
|
||||
@GenRouter.post("/output/{table_name}", summary="生成代码到指定路径", response_model=ResponseSchema[bool])
|
||||
async def gen_code_local_controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_generator:gencode:code"]))],
|
||||
table_name: Annotated[str, Path(description="表名")],
|
||||
@@ -165,11 +122,7 @@ async def gen_code_local_controller(
|
||||
return SuccessResponse(msg="生成代码到指定路径成功", data=result)
|
||||
|
||||
|
||||
@GenRouter.get(
|
||||
"/preview/{table_id}",
|
||||
summary="预览代码",
|
||||
response_model=ResponseSchema[GenTableOutSchema],
|
||||
)
|
||||
@GenRouter.get("/preview/{table_id}", summary="预览代码", response_model=ResponseSchema[GenTableOutSchema])
|
||||
async def preview_code_controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_generator:gencode:query"]))],
|
||||
table_id: Annotated[int, Path(description="业务表ID")],
|
||||
@@ -178,11 +131,7 @@ async def preview_code_controller(
|
||||
return SuccessResponse(data=result, msg="预览代码成功")
|
||||
|
||||
|
||||
@GenRouter.post(
|
||||
"/sync_db/{table_name}",
|
||||
summary="同步数据库",
|
||||
response_model=ResponseSchema[None],
|
||||
)
|
||||
@GenRouter.post("/sync_db/{table_name}", summary="同步数据库", response_model=ResponseSchema[None])
|
||||
async def sync_db_controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_generator:db:sync"]))],
|
||||
table_name: Annotated[str, Path(description="表名")],
|
||||
@@ -191,11 +140,7 @@ async def sync_db_controller(
|
||||
return SuccessResponse(msg="同步数据库成功", data=result)
|
||||
|
||||
|
||||
@GenRouter.get(
|
||||
"/sync_db/preview/{table_name}",
|
||||
summary="同步数据库差异预览",
|
||||
response_model=ResponseSchema[GenSyncPreviewSchema],
|
||||
)
|
||||
@GenRouter.get("/sync_db/preview/{table_name}", summary="同步数据库差异预览", response_model=ResponseSchema[GenSyncPreviewSchema])
|
||||
async def sync_db_preview_controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_generator:db:sync"]))],
|
||||
table_name: Annotated[str, Path(description="表名")],
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
|
||||
from fastapi import Query
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
|
||||
|
||||
from app.common.enums import QueueEnum
|
||||
from app.core.base_params import BaseQueryParam, TenantByQueryParam, UserByQueryParam
|
||||
from app.core.base_schema import BaseSchema, TenantBySchema, UserBySchema
|
||||
from app.core.base_schema import BaseQueryParam, BaseSchema, TenantByQueryParam, TenantBySchema, UserByQueryParam, UserBySchema
|
||||
|
||||
|
||||
class GenDBTableSchema(BaseModel):
|
||||
@@ -300,19 +297,20 @@ class GenSyncPreviewSchema(BaseModel):
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class GenTableQueryParam(BaseQueryParam, UserByQueryParam, TenantByQueryParam):
|
||||
"""代码生成业务表查询参数
|
||||
- 支持按`table_name`、`table_comment`进行模糊检索(由CRUD层实现like)。
|
||||
- 空值将被忽略,不参与过滤。
|
||||
"""
|
||||
|
||||
table_name: str | None = Query(None, description="表名称")
|
||||
table_comment: str | None = Query(None, description="表注释")
|
||||
status: int | None = Query(None, ge=0, le=1, description="状态(0:启动 1:停用)")
|
||||
table_name: str | None = Field(None, description="表名称")
|
||||
table_comment: str | None = Field(None, description="表注释")
|
||||
status: int | None = Field(None, ge=0, le=1, description="状态(0:启动 1:停用)")
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
@model_validator(mode="after")
|
||||
def validate_query_params(self) -> "GenTableQueryParam":
|
||||
self.table_name = (QueueEnum.like.value, self.table_name) if self.table_name else None
|
||||
self.table_comment = (QueueEnum.like.value, self.table_comment) if self.table_comment else None
|
||||
if isinstance(self.status, int):
|
||||
self.status = (QueueEnum.eq.value, self.status)
|
||||
return self
|
||||
|
||||
Reference in New Issue
Block a user