Skip to content

Commit b869aab

Browse files
committed
feat: add sort option for get admins
1 parent 0881c4d commit b869aab

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

app/db/crud/admin.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import datetime, timezone
2+
from enum import Enum
23

34
from sqlalchemy import func, select
45
from sqlalchemy.ext.asyncio import AsyncSession
@@ -15,6 +16,19 @@ async def load_admin_attrs(admin: Admin):
1516
pass
1617

1718

19+
AdminsSortingOptions = Enum(
20+
"AdminsSortingOptions",
21+
{
22+
"username": Admin.username.asc(),
23+
"created_at": Admin.created_at.asc(),
24+
"used_traffic": Admin.used_traffic.asc(),
25+
"-username": Admin.username.desc(),
26+
"-created_at": Admin.created_at.desc(),
27+
"-used_traffic": Admin.used_traffic.desc(),
28+
},
29+
)
30+
31+
1832
async def get_admin(db: AsyncSession, username: str) -> Admin:
1933
"""
2034
Retrieves an admin by username.
@@ -154,7 +168,11 @@ async def get_admin_by_discord_id(db: AsyncSession, discord_id: int) -> Admin:
154168

155169

156170
async def get_admins(
157-
db: AsyncSession, offset: int | None = None, limit: int | None = None, username: str | None = None
171+
db: AsyncSession,
172+
offset: int | None = None,
173+
limit: int | None = None,
174+
username: str | None = None,
175+
sort: list[AdminsSortingOptions] | None = None,
158176
) -> list[Admin]:
159177
"""
160178
Retrieves a list of admins with optional filters and pagination.
@@ -164,13 +182,18 @@ async def get_admins(
164182
offset (Optional[int]): The number of records to skip (for pagination).
165183
limit (Optional[int]): The maximum number of records to return.
166184
username (Optional[str]): The username to filter by.
185+
sort (Optional[list[AdminsSortingOptions]]): Sort options for ordering results.
167186
168187
Returns:
169188
List[Admin]: A list of admin objects.
170189
"""
171190
query = select(Admin)
172191
if username:
173192
query = query.where(Admin.username.ilike(f"%{username}%"))
193+
194+
if sort:
195+
query = query.order_by(*sort)
196+
174197
if offset:
175198
query = query.offset(offset)
176199
if limit:

app/operation/admin.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from app import notification
66
from app.db import AsyncSession
77
from app.db.crud.admin import (
8+
AdminsSortingOptions,
89
create_admin,
910
get_admins,
1011
get_admins_count,
@@ -75,9 +76,24 @@ async def remove_admin(self, db: AsyncSession, username: str, current_admin: Adm
7576
asyncio.create_task(notification.remove_admin(username, current_admin.username))
7677

7778
async def get_admins(
78-
self, db: AsyncSession, username: str | None = None, offset: int | None = None, limit: int | None = None
79+
self,
80+
db: AsyncSession,
81+
username: str | None = None,
82+
offset: int | None = None,
83+
limit: int | None = None,
84+
sort: str | None = None,
7985
) -> list[DBAdmin]:
80-
return await get_admins(db, offset, limit, username)
86+
sort_list = []
87+
if sort is not None:
88+
opts = sort.strip(",").split(",")
89+
for opt in opts:
90+
try:
91+
enum_member = AdminsSortingOptions[opt]
92+
sort_list.append(enum_member.value)
93+
except KeyError:
94+
await self.raise_error(message=f'"{opt}" is not a valid sort option', code=400)
95+
96+
return await get_admins(db, offset, limit, username, sort_list if sort_list else None)
8197

8298
async def get_admins_count(self, db: AsyncSession) -> int:
8399
return await get_admins_count(db)

app/routers/admin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,12 @@ async def get_admins(
121121
username: str | None = None,
122122
offset: int | None = None,
123123
limit: int | None = None,
124+
sort: str | None = None,
124125
db: AsyncSession = Depends(get_db),
125126
_: AdminDetails = Depends(check_sudo_admin),
126127
):
127128
"""Fetch a list of admins with optional filters for pagination and username."""
128-
return await admin_operator.get_admins(db, username=username, offset=offset, limit=limit)
129+
return await admin_operator.get_admins(db, username=username, offset=offset, limit=limit, sort=sort)
129130

130131

131132
@router.post("/{username}/users/disable", responses={404: responses._404})

tests/api/test_a_admin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ def test_get_admins(access_token):
8181
username = "testadmincreate"
8282
response = client.get(
8383
url="/api/admins",
84+
params={"sort": "-created_at"},
8485
headers={"Authorization": f"Bearer {access_token}"},
8586
)
8687
assert response.status_code == status.HTTP_200_OK

0 commit comments

Comments
 (0)