27 lines
952 B
Python
27 lines
952 B
Python
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy import select
|
|
from app.models.section import ContentSection
|
|
|
|
|
|
async def get_section(db: AsyncSession, key: str) -> dict | None:
|
|
result = await db.execute(select(ContentSection).where(ContentSection.key == key))
|
|
row = result.scalar_one_or_none()
|
|
return row.data if row else None
|
|
|
|
|
|
async def upsert_section(db: AsyncSession, key: str, data: dict) -> ContentSection:
|
|
result = await db.execute(select(ContentSection).where(ContentSection.key == key))
|
|
row = result.scalar_one_or_none()
|
|
if row:
|
|
row.data = data
|
|
else:
|
|
row = ContentSection(key=key, data=data)
|
|
db.add(row)
|
|
await db.flush()
|
|
return row
|
|
|
|
|
|
async def list_sections(db: AsyncSession) -> list[dict]:
|
|
result = await db.execute(select(ContentSection).order_by(ContentSection.key))
|
|
return [{"key": r.key, "updated_at": r.updated_at.isoformat()} for r in result.scalars()]
|