feat: add search_users and count_users to user repository

This commit is contained in:
Johan Lundberg 2026-02-19 11:00:47 +01:00
parent b22dabbbb0
commit 7e9eeb1339
No known key found for this signature in database
GPG key ID: A6C152738D03C7D1
3 changed files with 73 additions and 0 deletions

View file

@ -20,6 +20,10 @@ class UserRepository(Protocol):
async def list_users(self, offset: int = 0, limit: int = 100) -> list[User]: ...
async def search_users(self, query: str, offset: int = 0, limit: int = 100) -> list[User]: ...
async def count_users(self, query: str | None = None) -> int: ...
async def delete(self, userid: str) -> bool: ...

View file

@ -136,6 +136,32 @@ class SQLiteUserRepository:
users.append(self._row_to_user(row, groups))
return users
async def search_users(self, query: str, offset: int = 0, limit: int = 100) -> list[User]:
pattern = f"%{query}%"
async with self._db.execute(
"SELECT * FROM users WHERE username LIKE ? OR email LIKE ? ORDER BY username LIMIT ? OFFSET ?",
(pattern, pattern, limit, offset),
) as cursor:
rows = await cursor.fetchall()
users = []
for row in rows:
groups = await self._get_groups(row["userid"])
users.append(self._row_to_user(row, groups))
return users
async def count_users(self, query: str | None = None) -> int:
if query:
pattern = f"%{query}%"
async with self._db.execute(
"SELECT COUNT(*) FROM users WHERE username LIKE ? OR email LIKE ?",
(pattern, pattern),
) as cursor:
row = await cursor.fetchone()
else:
async with self._db.execute("SELECT COUNT(*) FROM users") as cursor:
row = await cursor.fetchone()
return row[0] if row else 0
async def delete(self, userid: str) -> bool:
cursor = await self._db.execute("DELETE FROM users WHERE userid = ?", (userid,))
await self._db.commit()