mirror of
https://github.com/fastapi-practices/fastapi-best-architecture.git
synced 2026-09-21 05:02:49 +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:
@@ -4,7 +4,12 @@ from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, Depends, Path, Query
|
||||
|
||||
from backend.app.{{ app_name }}.schema.{{ table_name }} import Create{{ schema_name }}Param, Get{{ schema_name }}Detail, Update{{ schema_name }}Param
|
||||
from backend.app.{{ app_name }}.schema.{{ table_name }} import (
|
||||
Create{{ schema_name }}Param,
|
||||
Delete{{ schema_name }}Param,
|
||||
Get{{ schema_name }}Detail,
|
||||
Update{{ schema_name }}Param,
|
||||
)
|
||||
from backend.app.{{ app_name }}.service.{{ table_name }}_service import {{ table_name }}_service
|
||||
from backend.common.pagination import DependsPagination, PageData, paging_data
|
||||
from backend.common.response.response_schema import ResponseModel, ResponseSchemaModel, response_base
|
||||
@@ -30,7 +35,7 @@ async def get_{{ table_name }}(pk: Annotated[int, Path(description='{{ doc_comme
|
||||
DependsPagination,
|
||||
],
|
||||
)
|
||||
async def get_pagination_{{ table_name }}s(db: CurrentSession) -> ResponseSchemaModel[PageData[Get{{ schema_name }}Detail]]:
|
||||
async def get_{{ table_name }}s_paged(db: CurrentSession) -> ResponseSchemaModel[PageData[Get{{ schema_name }}Detail]]:
|
||||
{{ table_name }}_select = await {{ table_name }}_service.get_select()
|
||||
page_data = await paging_data(db, {{ table_name }}_select)
|
||||
return response_base.success(data=page_data)
|
||||
@@ -72,8 +77,8 @@ async def update_{{ table_name }}(pk: Annotated[int, Path(description='{{ doc_co
|
||||
DependsRBAC,
|
||||
],
|
||||
)
|
||||
async def delete_{{ table_name }}(pk: Annotated[list[int], Query(description='{{ doc_comment }} ID 列表')]) -> ResponseModel:
|
||||
count = await {{ table_name }}_service.delete(pk=pk)
|
||||
async def delete_{{ table_name }}s(obj: Delete{{ schema_name }}Param) -> ResponseModel:
|
||||
count = await {{ table_name }}_service.delete(obj=obj)
|
||||
if count > 0:
|
||||
return response_base.success()
|
||||
return response_base.fail()
|
||||
|
||||
@@ -55,15 +55,15 @@ class CRUD{{ class_name }}(CRUDPlus[{{ schema_name }}]):
|
||||
"""
|
||||
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:
|
||||
"""
|
||||
删除{{ doc_comment }}
|
||||
批量删除{{ doc_comment }}
|
||||
|
||||
:param db: 数据库会话
|
||||
:param pk: {{ doc_comment }} ID
|
||||
:param pks: {{ doc_comment }} 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)
|
||||
|
||||
|
||||
{{ instance_name }}_dao: CRUD{{ class_name }} = CRUD{{ class_name }}({{ class_name }})
|
||||
|
||||
@@ -23,6 +23,10 @@ class Update{{ schema_name }}Param({{ schema_name }}SchemaBase):
|
||||
"""更新{{ doc_comment }}参数"""
|
||||
|
||||
|
||||
class Delete{{ schema_name }}Param({{ schema_name }}SchemaBase):
|
||||
"""删除{{ doc_comment }}参数"""
|
||||
|
||||
|
||||
class Get{{ schema_name }}Detail({{ schema_name }}SchemaBase):
|
||||
"""{{ doc_comment }}详情"""
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ from sqlalchemy import Select
|
||||
|
||||
from backend.app.{{ app_name }}.crud.crud_{{ table_name }} import {{ table_name }}_dao
|
||||
from backend.app.{{ app_name }}.model import {{ class_name }}
|
||||
from backend.app.{{ app_name }}.schema.{{ table_name }} import Create{{ schema_name }}Param, Update{{ schema_name }}Param
|
||||
from backend.app.{{ app_name }}.schema.{{ table_name }} import Create{{ schema_name }}Param, Delete{{ schema_name }}Param, Update{{ schema_name }}Param
|
||||
from backend.common.exception import errors
|
||||
from backend.database.db import async_db_session
|
||||
|
||||
@@ -63,15 +63,15 @@ class {{ class_name }}Service:
|
||||
return count
|
||||
|
||||
@staticmethod
|
||||
async def delete(*, pk: list[int]) -> int:
|
||||
async def delete(*, obj: Delete{{ schema_name }}Param) -> int:
|
||||
"""
|
||||
删除{{ doc_comment }}
|
||||
|
||||
:param pk: {{ doc_comment }} ID 列表
|
||||
:param obj: {{ doc_comment }} ID 列表
|
||||
:return:
|
||||
"""
|
||||
async with async_db_session.begin() as db:
|
||||
count = await {{ table_name }}_dao.delete(db, pk)
|
||||
count = await {{ table_name }}_dao.delete(db, obj.pks)
|
||||
return count
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user