data driven about us screen

This commit is contained in:
2026-06-21 05:24:40 +00:00
parent f52625b37b
commit 1b537b3dfd
34 changed files with 2119 additions and 44 deletions
+79
View File
@@ -0,0 +1,79 @@
from typing import Optional
from fastapi import APIRouter, Depends, Query
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.auth import get_current_admin_user
from app.database import get_session
from app.models import HistoryEntry
from app.schemas import HistoryEntryCreate, HistoryEntryUpdate, HistoryEntryResponse
from app.user_models import User
router = APIRouter()
@router.get("/", response_model=list[HistoryEntryResponse])
async def list_history_entries(
active: Optional[bool] = Query(None, description="Filter by active status"),
session: AsyncSession = Depends(get_session),
):
query = select(HistoryEntry).order_by(HistoryEntry.display_order)
if active is not None:
query = query.where(HistoryEntry.active == active)
result = await session.execute(query)
return list(result.scalars().all())
@router.get("/{entry_id}", response_model=HistoryEntryResponse)
async def get_history_entry(entry_id: int, session: AsyncSession = Depends(get_session)):
result = await session.execute(select(HistoryEntry).where(HistoryEntry.id == entry_id))
entry = result.scalar_one_or_none()
if not entry:
return {"error": "History entry not found"}
return entry
@router.post("/", response_model=HistoryEntryResponse, status_code=201)
async def create_history_entry(
payload: HistoryEntryCreate,
session: AsyncSession = Depends(get_session),
_: User = Depends(get_current_admin_user),
):
entry = HistoryEntry(**payload.model_dump(), active=True)
session.add(entry)
await session.commit()
await session.refresh(entry)
return entry
@router.put("/{entry_id}", response_model=HistoryEntryResponse)
async def update_history_entry(
entry_id: int,
payload: HistoryEntryUpdate,
session: AsyncSession = Depends(get_session),
_: User = Depends(get_current_admin_user),
):
result = await session.execute(select(HistoryEntry).where(HistoryEntry.id == entry_id))
entry = result.scalar_one_or_none()
if not entry:
return {"error": "History entry not found"}
for key, value in payload.model_dump(exclude_unset=True).items():
setattr(entry, key, value)
await session.commit()
await session.refresh(entry)
return entry
@router.delete("/{entry_id}", status_code=204)
async def delete_history_entry(
entry_id: int,
session: AsyncSession = Depends(get_session),
_: User = Depends(get_current_admin_user),
):
result = await session.execute(select(HistoryEntry).where(HistoryEntry.id == entry_id))
entry = result.scalar_one_or_none()
if not entry:
return {"error": "History entry not found"}
await session.delete(entry)
await session.commit()