Skip to content

Commit 6446909

Browse files
committed
Add business pagination in the code generator
1 parent 4eb76ad commit 6446909

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

backend/plugin/code_generator/api/v1/business.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
# -*- coding: utf-8 -*-
33
from typing import Annotated
44

5-
from fastapi import APIRouter, Depends, Path
5+
from fastapi import APIRouter, Depends, Path, Query
66

7+
from backend.common.pagination import DependsPagination, PageData, paging_data
78
from backend.common.response.response_schema import ResponseModel, ResponseSchemaModel, response_base
89
from backend.common.security.jwt import DependsJwtAuth
910
from backend.common.security.permission import RequestPermission
1011
from backend.common.security.rbac import DependsRBAC
12+
from backend.database.db import CurrentSession
1113
from backend.plugin.code_generator.schema.business import (
1214
CreateGenBusinessParam,
1315
GetGenBusinessDetail,
@@ -28,10 +30,21 @@ async def get_business(
2830
return response_base.success(data=data)
2931

3032

31-
@router.get('', summary='获取所有代码生成业务', dependencies=[DependsJwtAuth])
32-
async def get_all_businesses() -> ResponseSchemaModel[list[GetGenBusinessDetail]]:
33-
data = await gen_business_service.get_all()
34-
return response_base.success(data=data)
33+
@router.get(
34+
'',
35+
summary='分页获取所有代码生成业务',
36+
dependencies=[
37+
DependsJwtAuth,
38+
DependsPagination,
39+
],
40+
)
41+
async def get_businesses_paged(
42+
db: CurrentSession,
43+
table_name: Annotated[str | None, Query(description='代码生成业务表名称')] = None,
44+
) -> ResponseSchemaModel[PageData[GetGenBusinessDetail]]:
45+
business_select = await gen_business_service.get_select(table_name=table_name)
46+
page_data = await paging_data(db, business_select)
47+
return response_base.success(data=page_data)
3548

3649

3750
@router.get('/{pk}/models', summary='获取代码生成业务所有模型', dependencies=[DependsJwtAuth])

backend/plugin/code_generator/crud/crud_business.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
3-
from typing import Sequence
43

4+
from sqlalchemy import Select
55
from sqlalchemy.ext.asyncio import AsyncSession
66
from sqlalchemy_crud_plus import CRUDPlus
77

@@ -32,14 +32,19 @@ async def get_by_name(self, db: AsyncSession, name: str) -> GenBusiness | None:
3232
"""
3333
return await self.select_model_by_column(db, table_name=name)
3434

35-
async def get_all(self, db: AsyncSession) -> Sequence[GenBusiness]:
35+
async def get_list(self, table_name: str | None) -> Select:
3636
"""
3737
获取所有代码生成业务
3838
39-
:param db: 数据库会话
39+
:param table_name: 业务表名
4040
:return:
4141
"""
42-
return await self.select_models(db)
42+
filters = {}
43+
44+
if table_name is not None:
45+
filters['table_name__like'] = f'%{table_name}%'
46+
47+
return await self.select_order('id', 'desc', **filters)
4348

4449
async def create(self, db: AsyncSession, obj: CreateGenBusinessParam) -> None:
4550
"""

backend/plugin/code_generator/service/business_service.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
3-
from typing import Sequence
3+
4+
from sqlalchemy import Select
45

56
from backend.common.exception import errors
67
from backend.database.db import async_db_session
@@ -27,10 +28,14 @@ async def get(*, pk: int) -> GenBusiness:
2728
return business
2829

2930
@staticmethod
30-
async def get_all() -> Sequence[GenBusiness]:
31-
"""获取所有业务"""
32-
async with async_db_session() as db:
33-
return await gen_business_dao.get_all(db)
31+
async def get_select(*, table_name: str) -> Select:
32+
"""
33+
获取代码生成业务列表查询条件
34+
35+
:param table_name: 业务表名
36+
:return:
37+
"""
38+
return await gen_business_dao.get_list(table_name=table_name)
3439

3540
@staticmethod
3641
async def create(*, obj: CreateGenBusinessParam) -> None:

0 commit comments

Comments
 (0)