mirror of
https://github.com/fastapi-practices/fastapi-best-architecture.git
synced 2026-09-21 13:12:24 +00:00
Optimize routes to better align with RESTful (#673)
* Optimize routes to better align with RESTful * Add codes endpoint description * Update jinja templates * fix typo * fix sql
This commit is contained in:
@@ -12,6 +12,7 @@ from backend.common.security.rbac import DependsRBAC
|
||||
from backend.database.db import CurrentSession
|
||||
from backend.plugin.dict.schema.dict_data import (
|
||||
CreateDictDataParam,
|
||||
DeleteDictDataParam,
|
||||
GetDictDataDetail,
|
||||
GetDictDataWithRelation,
|
||||
UpdateDictDataParam,
|
||||
@@ -21,7 +22,7 @@ from backend.plugin.dict.service.dict_data_service import dict_data_service
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get('/{pk}', summary='获取字典详情', dependencies=[DependsJwtAuth])
|
||||
@router.get('/{pk}', summary='获取字典数据详情', dependencies=[DependsJwtAuth])
|
||||
async def get_dict_data(
|
||||
pk: Annotated[int, Path(description='字典数据 ID')],
|
||||
) -> ResponseSchemaModel[GetDictDataWithRelation]:
|
||||
@@ -31,13 +32,13 @@ async def get_dict_data(
|
||||
|
||||
@router.get(
|
||||
'',
|
||||
summary='分页获取所有字典',
|
||||
summary='分页获取所有字典数据',
|
||||
dependencies=[
|
||||
DependsJwtAuth,
|
||||
DependsPagination,
|
||||
],
|
||||
)
|
||||
async def get_pagination_dict_datas(
|
||||
async def get_dict_datas_paged(
|
||||
db: CurrentSession,
|
||||
label: Annotated[str | None, Query(description='字典数据标签')] = None,
|
||||
value: Annotated[str | None, Query(description='字典数据键值')] = None,
|
||||
@@ -50,9 +51,9 @@ async def get_pagination_dict_datas(
|
||||
|
||||
@router.post(
|
||||
'',
|
||||
summary='创建字典',
|
||||
summary='创建字典数据',
|
||||
dependencies=[
|
||||
Depends(RequestPermission('sys:dict:data:add')),
|
||||
Depends(RequestPermission('dict:data:add')),
|
||||
DependsRBAC,
|
||||
],
|
||||
)
|
||||
@@ -63,9 +64,9 @@ async def create_dict_data(obj: CreateDictDataParam) -> ResponseModel:
|
||||
|
||||
@router.put(
|
||||
'/{pk}',
|
||||
summary='更新字典',
|
||||
summary='更新字典数据',
|
||||
dependencies=[
|
||||
Depends(RequestPermission('sys:dict:data:edit')),
|
||||
Depends(RequestPermission('dict:data:edit')),
|
||||
DependsRBAC,
|
||||
],
|
||||
)
|
||||
@@ -80,14 +81,14 @@ async def update_dict_data(
|
||||
|
||||
@router.delete(
|
||||
'',
|
||||
summary='批量删除字典',
|
||||
summary='批量删除字典数据',
|
||||
dependencies=[
|
||||
Depends(RequestPermission('sys:dict:data:del')),
|
||||
Depends(RequestPermission('dict:data:del')),
|
||||
DependsRBAC,
|
||||
],
|
||||
)
|
||||
async def delete_dict_data(pk: Annotated[list[int], Query(description='字典数据 ID 列表')]) -> ResponseModel:
|
||||
count = await dict_data_service.delete(pk=pk)
|
||||
async def delete_dict_datas(obj: DeleteDictDataParam) -> ResponseModel:
|
||||
count = await dict_data_service.delete(obj=obj)
|
||||
if count > 0:
|
||||
return response_base.success()
|
||||
return response_base.fail()
|
||||
|
||||
@@ -10,7 +10,12 @@ from backend.common.security.jwt import DependsJwtAuth
|
||||
from backend.common.security.permission import RequestPermission
|
||||
from backend.common.security.rbac import DependsRBAC
|
||||
from backend.database.db import CurrentSession
|
||||
from backend.plugin.dict.schema.dict_type import CreateDictTypeParam, GetDictTypeDetail, UpdateDictTypeParam
|
||||
from backend.plugin.dict.schema.dict_type import (
|
||||
CreateDictTypeParam,
|
||||
DeleteDictTypeParam,
|
||||
GetDictTypeDetail,
|
||||
UpdateDictTypeParam,
|
||||
)
|
||||
from backend.plugin.dict.service.dict_type_service import dict_type_service
|
||||
|
||||
router = APIRouter()
|
||||
@@ -24,7 +29,7 @@ router = APIRouter()
|
||||
DependsPagination,
|
||||
],
|
||||
)
|
||||
async def get_pagination_dict_types(
|
||||
async def get_dict_types_paged(
|
||||
db: CurrentSession,
|
||||
name: Annotated[str | None, Query(description='字典类型名称')] = None,
|
||||
code: Annotated[str | None, Query(description='字典类型编码')] = None,
|
||||
@@ -39,7 +44,7 @@ async def get_pagination_dict_types(
|
||||
'',
|
||||
summary='创建字典类型',
|
||||
dependencies=[
|
||||
Depends(RequestPermission('sys:dict:type:add')),
|
||||
Depends(RequestPermission('dict:type:add')),
|
||||
DependsRBAC,
|
||||
],
|
||||
)
|
||||
@@ -52,7 +57,7 @@ async def create_dict_type(obj: CreateDictTypeParam) -> ResponseModel:
|
||||
'/{pk}',
|
||||
summary='更新字典类型',
|
||||
dependencies=[
|
||||
Depends(RequestPermission('sys:dict:type:edit')),
|
||||
Depends(RequestPermission('dict:type:edit')),
|
||||
DependsRBAC,
|
||||
],
|
||||
)
|
||||
@@ -69,12 +74,12 @@ async def update_dict_type(
|
||||
'',
|
||||
summary='批量删除字典类型',
|
||||
dependencies=[
|
||||
Depends(RequestPermission('sys:dict:type:del')),
|
||||
Depends(RequestPermission('dict:type:del')),
|
||||
DependsRBAC,
|
||||
],
|
||||
)
|
||||
async def delete_dict_type(pk: Annotated[list[int], Query(description='字典类型 ID 列表')]) -> ResponseModel:
|
||||
count = await dict_type_service.delete(pk=pk)
|
||||
async def delete_dict_types(obj: DeleteDictTypeParam) -> ResponseModel:
|
||||
count = await dict_type_service.delete(obj=obj)
|
||||
if count > 0:
|
||||
return response_base.success()
|
||||
return response_base.fail()
|
||||
|
||||
@@ -72,15 +72,15 @@ class CRUDDictData(CRUDPlus[DictData]):
|
||||
"""
|
||||
return await self.update_model(db, pk, obj)
|
||||
|
||||
async def delete(self, db: AsyncSession, pk: list[int]) -> int:
|
||||
async def delete(self, db: AsyncSession, pks: list[int]) -> int:
|
||||
"""
|
||||
删除字典数据
|
||||
批量删除字典数据
|
||||
|
||||
:param db: 数据库会话
|
||||
:param pk: 字典数据 ID 列表
|
||||
:param pks: 字典数据 ID 列表
|
||||
:return:
|
||||
"""
|
||||
return await self.delete_model_by_column(db, allow_multiple=True, id__in=pk)
|
||||
return await self.delete_model_by_column(db, allow_multiple=True, id__in=pks)
|
||||
|
||||
async def get_with_relation(self, db: AsyncSession, pk: int) -> DictData | None:
|
||||
"""
|
||||
|
||||
@@ -72,15 +72,15 @@ class CRUDDictType(CRUDPlus[DictType]):
|
||||
"""
|
||||
return await self.update_model(db, pk, obj)
|
||||
|
||||
async def delete(self, db: AsyncSession, pk: list[int]) -> int:
|
||||
async def delete(self, db: AsyncSession, pks: list[int]) -> int:
|
||||
"""
|
||||
删除字典类型
|
||||
批量删除字典类型
|
||||
|
||||
:param db: 数据库会话
|
||||
:param pk: 字典类型 ID 列表
|
||||
:param pks: 字典类型 ID 列表
|
||||
:return:
|
||||
"""
|
||||
return await self.delete_model_by_column(db, allow_multiple=True, id__in=pk)
|
||||
return await self.delete_model_by_column(db, allow_multiple=True, id__in=pks)
|
||||
|
||||
|
||||
dict_type_dao: CRUDDictType = CRUDDictType(DictType)
|
||||
|
||||
@@ -28,6 +28,12 @@ class UpdateDictDataParam(DictDataSchemaBase):
|
||||
"""更新字典数据参数"""
|
||||
|
||||
|
||||
class DeleteDictDataParam(SchemaBase):
|
||||
"""删除字典数据参数"""
|
||||
|
||||
pks: list[int] = Field(description='字典数据 ID 列表')
|
||||
|
||||
|
||||
class GetDictDataDetail(DictDataSchemaBase):
|
||||
"""字典数据详情"""
|
||||
|
||||
|
||||
@@ -25,6 +25,12 @@ class UpdateDictTypeParam(DictTypeSchemaBase):
|
||||
"""更新字典类型参数"""
|
||||
|
||||
|
||||
class DeleteDictTypeParam(SchemaBase):
|
||||
"""删除字典类型参数"""
|
||||
|
||||
pks: list[int] = Field(description='字典类型 ID 列表')
|
||||
|
||||
|
||||
class GetDictTypeDetail(DictTypeSchemaBase):
|
||||
"""字典类型详情"""
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ from backend.database.db import async_db_session
|
||||
from backend.plugin.dict.crud.crud_dict_data import dict_data_dao
|
||||
from backend.plugin.dict.crud.crud_dict_type import dict_type_dao
|
||||
from backend.plugin.dict.model import DictData
|
||||
from backend.plugin.dict.schema.dict_data import CreateDictDataParam, UpdateDictDataParam
|
||||
from backend.plugin.dict.schema.dict_data import CreateDictDataParam, DeleteDictDataParam, UpdateDictDataParam
|
||||
|
||||
|
||||
class DictDataService:
|
||||
@@ -79,15 +79,15 @@ class DictDataService:
|
||||
return count
|
||||
|
||||
@staticmethod
|
||||
async def delete(*, pk: list[int]) -> int:
|
||||
async def delete(*, obj: DeleteDictDataParam) -> int:
|
||||
"""
|
||||
删除字典数据
|
||||
批量删除字典数据
|
||||
|
||||
:param pk: 字典数据 ID 列表
|
||||
:param obj: 字典数据 ID 列表
|
||||
:return:
|
||||
"""
|
||||
async with async_db_session.begin() as db:
|
||||
count = await dict_data_dao.delete(db, pk)
|
||||
count = await dict_data_dao.delete(db, obj.pks)
|
||||
return count
|
||||
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ from sqlalchemy import Select
|
||||
from backend.common.exception import errors
|
||||
from backend.database.db import async_db_session
|
||||
from backend.plugin.dict.crud.crud_dict_type import dict_type_dao
|
||||
from backend.plugin.dict.schema.dict_type import CreateDictTypeParam, UpdateDictTypeParam
|
||||
from backend.plugin.dict.schema.dict_type import CreateDictTypeParam, DeleteDictTypeParam, UpdateDictTypeParam
|
||||
|
||||
|
||||
class DictTypeService:
|
||||
@@ -57,15 +57,15 @@ class DictTypeService:
|
||||
return count
|
||||
|
||||
@staticmethod
|
||||
async def delete(*, pk: list[int]) -> int:
|
||||
async def delete(*, obj: DeleteDictTypeParam) -> int:
|
||||
"""
|
||||
删除字典类型
|
||||
批量删除字典类型
|
||||
|
||||
:param pk: 字典类型 ID 列表
|
||||
:param obj: 字典类型 ID 列表
|
||||
:return:
|
||||
"""
|
||||
async with async_db_session.begin() as db:
|
||||
count = await dict_type_dao.delete(db, pk)
|
||||
count = await dict_type_dao.delete(db, obj.pks)
|
||||
return count
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user