Files
F-r-ldraledigplannerare/backend/app/repositories_impl/sqlite/user_repository.py
T
jacobh2 68d982bb94 Make repository list() reject unrecognised filter names
Repository.list(**filters) let each implementation pick out the filter
names it understood and silently ignore the rest, so a typo or an
unsupported filter returned every row in the table instead of an error.

This was hit for real: LeaveWishRepository.list(parent_personal_data_uuid
=...) returns every wish in the database, because that implementation
only recognises workspace_id and child_id. A guard written on top of it
would have passed while checking nothing.

All ten implementations now validate against their supported set and
raise ValueError naming the offending key. ValueError rather than a
domain error because passing a filter that does not exist is a
programming bug to fix, not a runtime condition to handle -- it must not
become an HTTP 4xx.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RXMhc9nT6sNz1SL4j8dznL
2026-09-14 18:54:34 +02:00

80 lines
2.8 KiB
Python

"""SQLite implementation of UserRepository."""
from datetime import datetime
from uuid import UUID
import aiosqlite
from app.entities.user import User
from app.repositories.filter_validator import validate_filters
from app.repositories.user_repository import UserRepository
from app.utils.errors import NotFoundError
class SQLiteUserRepository(UserRepository):
def __init__(self, connection: aiosqlite.Connection) -> None:
self._conn = connection
@staticmethod
def _row_to_entity(row: aiosqlite.Row) -> User:
return User(
id=UUID(row["id"]),
personal_data_uuid=UUID(row["personal_data_uuid"]),
email=row["email"],
hashed_password=row["hashed_password"],
created_at=datetime.fromisoformat(row["created_at"]),
)
async def create(self, item: User) -> User:
now = datetime.now()
item.created_at = item.created_at or now
await self._conn.execute(
"""
INSERT INTO "user" (id, personal_data_uuid, email, hashed_password, created_at)
VALUES (?, ?, ?, ?, ?)
""",
(
str(item.id),
str(item.personal_data_uuid),
item.email,
item.hashed_password,
item.created_at.isoformat(),
),
)
await self._conn.commit()
return item
async def get_by_id(self, id: UUID) -> User | None:
cursor = await self._conn.execute('SELECT * FROM "user" WHERE id = ?', (str(id),))
row = await cursor.fetchone()
return self._row_to_entity(row) if row else None
async def get_by_email(self, email: str) -> User | None:
cursor = await self._conn.execute('SELECT * FROM "user" WHERE email = ?', (email,))
row = await cursor.fetchone()
return self._row_to_entity(row) if row else None
async def update(self, id: UUID, item: User) -> User:
cursor = await self._conn.execute(
'UPDATE "user" SET email = ?, hashed_password = ? WHERE id = ?',
(item.email, item.hashed_password, str(id)),
)
await self._conn.commit()
if cursor.rowcount == 0:
raise NotFoundError(f"User {id} not found")
refreshed = await self.get_by_id(id)
assert refreshed is not None
return refreshed
async def delete(self, id: UUID) -> None:
cursor = await self._conn.execute('DELETE FROM "user" WHERE id = ?', (str(id),))
await self._conn.commit()
if cursor.rowcount == 0:
raise NotFoundError(f"User {id} not found")
async def list(self, **filters: object) -> list[User]:
validate_filters(filters, set())
cursor = await self._conn.execute('SELECT * FROM "user"')
rows = await cursor.fetchall()
return [self._row_to_entity(r) for r in rows]