137 行
3.6 KiB
Python
137 行
3.6 KiB
Python
"""Profile route handlers."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from backend.routes.common import handle_request
|
|
from backend.services.profile_service import ProfileService
|
|
from backend.services.quick_profile_service import QuickProfileService
|
|
from core.audit_service import AuditService
|
|
from core.identity import RequestContext
|
|
from core.security import require_authenticated, require_write
|
|
from database.db_helper import Database, load_database_settings
|
|
from database.repositories.profile_repository import ProfileRepository
|
|
|
|
|
|
def list_profiles(
|
|
ctx: RequestContext,
|
|
query: dict[str, Any] | None = None,
|
|
service: ProfileService | None = None,
|
|
) -> dict:
|
|
profile_service = service or _create_profile_service()
|
|
params = query or {}
|
|
|
|
def action() -> dict:
|
|
require_authenticated(ctx)
|
|
result = profile_service.list_profiles(
|
|
ctx=ctx,
|
|
keyword=_optional_str(params.get("keyword")),
|
|
status=_optional_str(params.get("status")) or "active",
|
|
page=_int_or_default(params.get("page"), 1),
|
|
page_size=_int_or_default(params.get("page_size"), 30),
|
|
)
|
|
return result.to_dict()
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def create_profile(
|
|
ctx: RequestContext,
|
|
payload: dict[str, Any],
|
|
service: ProfileService | None = None,
|
|
) -> dict:
|
|
profile_service = service or _create_profile_service()
|
|
|
|
def action() -> dict:
|
|
require_write(ctx)
|
|
return profile_service.create_profile(payload, ctx).to_dict()
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def get_profile(
|
|
ctx: RequestContext,
|
|
profile_id: str,
|
|
service: ProfileService | None = None,
|
|
) -> dict:
|
|
profile_service = service or _create_profile_service()
|
|
|
|
def action() -> dict:
|
|
require_authenticated(ctx)
|
|
return profile_service.get_profile(profile_id).to_dict()
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def update_profile(
|
|
ctx: RequestContext,
|
|
profile_id: str,
|
|
payload: dict[str, Any],
|
|
service: ProfileService | None = None,
|
|
) -> dict:
|
|
profile_service = service or _create_profile_service()
|
|
|
|
def action() -> dict:
|
|
require_write(ctx)
|
|
return profile_service.update_profile(profile_id, payload, ctx).to_dict()
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def delete_profile(
|
|
ctx: RequestContext,
|
|
profile_id: str,
|
|
service: ProfileService | None = None,
|
|
) -> dict:
|
|
profile_service = service or _create_profile_service()
|
|
|
|
def action() -> dict:
|
|
require_write(ctx)
|
|
return profile_service.delete_profile(profile_id, ctx).to_dict()
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def archive_profile(
|
|
ctx: RequestContext,
|
|
profile_id: str,
|
|
service: ProfileService | None = None,
|
|
) -> dict:
|
|
return delete_profile(ctx, profile_id, service)
|
|
|
|
|
|
def create_quick_profile_draft(
|
|
ctx: RequestContext,
|
|
payload: dict[str, Any],
|
|
service: QuickProfileService | None = None,
|
|
) -> dict:
|
|
quick_service = service or QuickProfileService()
|
|
|
|
def action() -> dict:
|
|
require_authenticated(ctx)
|
|
return quick_service.create_draft(payload)
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def _create_profile_service() -> ProfileService:
|
|
database = Database(load_database_settings())
|
|
return ProfileService(
|
|
repository=ProfileRepository(database),
|
|
audit_service=AuditService(database),
|
|
)
|
|
|
|
|
|
def _optional_str(value: Any) -> str | None:
|
|
if value is None:
|
|
return None
|
|
text = str(value).strip()
|
|
return text or None
|
|
|
|
|
|
def _int_or_default(value: Any, default: int) -> int:
|
|
try:
|
|
return int(value)
|
|
except (TypeError, ValueError):
|
|
return default
|