feat: add search_users and count_users to user repository
This commit is contained in:
parent
b22dabbbb0
commit
7e9eeb1339
3 changed files with 73 additions and 0 deletions
|
|
@ -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: ...
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue