Skip to content

Commit a756475

Browse files
perf: Workspace api for ui
1 parent 26664e1 commit a756475

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

backend/apps/system/api/workspace.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,47 @@
11
from typing import Optional
22
from fastapi import APIRouter, Query
3-
from sqlmodel import or_, select
3+
from sqlmodel import exists, or_, select
44
from apps.system.models.system_model import UserWsModel, WorkspaceBase, WorkspaceEditor, WorkspaceModel
55
from apps.system.models.user import UserModel
6-
from apps.system.schemas.system_schema import UserWsBase, UserWsDTO, WorkspaceUser
6+
from apps.system.schemas.system_schema import UserWsBase, UserWsDTO, UserWsOption, WorkspaceUser
77
from common.core.deps import CurrentUser, SessionDep, Trans
88
from common.core.pagination import Paginator
99
from common.core.schemas import PaginatedResponse, PaginationParams
1010
from common.utils.time import get_timestamp
1111

1212
router = APIRouter(tags=["system/workspace"], prefix="/system/workspace")
1313

14+
@router.get("/uws/option/pager/{pageNum}/{pageSize}", response_model=PaginatedResponse[UserWsOption])
15+
async def option_pager(
16+
session: SessionDep,
17+
current_user: CurrentUser,
18+
pageNum: int,
19+
pageSize: int,
20+
oid: int = Query(description="空间ID"),
21+
keyword: Optional[str] = Query(None, description="搜索关键字(可选)"),
22+
):
23+
if not current_user.isAdmin:
24+
raise RuntimeError('only for admin')
25+
if not oid:
26+
raise RuntimeError('oid miss error')
27+
pagination = PaginationParams(page=pageNum, size=pageSize)
28+
paginator = Paginator(session)
29+
stmt = select(UserModel.id, UserModel.account, UserModel.name).where(
30+
~exists().where(UserWsModel.uid == UserModel.id, UserWsModel.oid == oid)
31+
).order_by(UserModel.create_time)
32+
33+
if keyword:
34+
keyword_pattern = f"%{keyword}%"
35+
stmt = stmt.where(
36+
or_(
37+
UserModel.account.ilike(keyword_pattern),
38+
UserModel.name.ilike(keyword_pattern),
39+
)
40+
)
41+
return await paginator.get_paginated_response(
42+
stmt=stmt,
43+
pagination=pagination,
44+
)
1445

1546
@router.get("/uws/pager/{pageNum}/{pageSize}", response_model=PaginatedResponse[WorkspaceUser])
1647
async def pager(

backend/apps/system/schemas/system_schema.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,7 @@ class WorkspaceUser(UserEditor):
9393
create_time: int
9494

9595
class UserWs(BaseCreatorDTO):
96-
name: str
96+
name: str
97+
98+
class UserWsOption(UserWs):
99+
account: str

0 commit comments

Comments
 (0)