Adds Underwriter's carousel and admin features
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -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 Underwriter
|
||||
from app.schemas import UnderwriterCreate, UnderwriterUpdate, UnderwriterResponse
|
||||
from app.user_models import User
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/", response_model=list[UnderwriterResponse])
|
||||
async def list_underwriters(
|
||||
active: Optional[bool] = Query(None, description="Filter by active status"),
|
||||
session: AsyncSession = Depends(get_session),
|
||||
):
|
||||
query = select(Underwriter).order_by(Underwriter.display_order)
|
||||
if active is not None:
|
||||
query = query.where(Underwriter.active == active)
|
||||
result = await session.execute(query)
|
||||
return list(result.scalars().all())
|
||||
|
||||
|
||||
@router.get("/{underwriter_id}", response_model=UnderwriterResponse)
|
||||
async def get_underwriter(underwriter_id: int, session: AsyncSession = Depends(get_session)):
|
||||
result = await session.execute(select(Underwriter).where(Underwriter.id == underwriter_id))
|
||||
uw = result.scalar_one_or_none()
|
||||
if not uw:
|
||||
return {"error": "Underwriter not found"}
|
||||
return uw
|
||||
|
||||
|
||||
@router.post("/", response_model=UnderwriterResponse, status_code=201)
|
||||
async def create_underwriter(
|
||||
payload: UnderwriterCreate,
|
||||
session: AsyncSession = Depends(get_session),
|
||||
_: User = Depends(get_current_admin_user),
|
||||
):
|
||||
uw = Underwriter(**payload.model_dump(), active=True)
|
||||
session.add(uw)
|
||||
await session.commit()
|
||||
await session.refresh(uw)
|
||||
return uw
|
||||
|
||||
|
||||
@router.put("/{underwriter_id}", response_model=UnderwriterResponse)
|
||||
async def update_underwriter(
|
||||
underwriter_id: int,
|
||||
payload: UnderwriterUpdate,
|
||||
session: AsyncSession = Depends(get_session),
|
||||
_: User = Depends(get_current_admin_user),
|
||||
):
|
||||
result = await session.execute(select(Underwriter).where(Underwriter.id == underwriter_id))
|
||||
uw = result.scalar_one_or_none()
|
||||
if not uw:
|
||||
return {"error": "Underwriter not found"}
|
||||
for key, value in payload.model_dump(exclude_unset=True).items():
|
||||
setattr(uw, key, value)
|
||||
await session.commit()
|
||||
await session.refresh(uw)
|
||||
return uw
|
||||
|
||||
|
||||
@router.delete("/{underwriter_id}", status_code=204)
|
||||
async def delete_underwriter(
|
||||
underwriter_id: int,
|
||||
session: AsyncSession = Depends(get_session),
|
||||
_: User = Depends(get_current_admin_user),
|
||||
):
|
||||
result = await session.execute(select(Underwriter).where(Underwriter.id == underwriter_id))
|
||||
uw = result.scalar_one_or_none()
|
||||
if not uw:
|
||||
return {"error": "Underwriter not found"}
|
||||
await session.delete(uw)
|
||||
await session.commit()
|
||||
+3
-2
@@ -6,9 +6,9 @@ from sqlalchemy import func, select
|
||||
|
||||
from app.config import settings
|
||||
from app.database import async_session, engine
|
||||
from app.models import Base, Show, StationConfig, HistoryEntry, TeamMember, CommunityHighlight
|
||||
from app.models import Base, Show, StationConfig, HistoryEntry, TeamMember, CommunityHighlight, Underwriter
|
||||
from app.user_models import User
|
||||
from app.api import events, auth, station_config, history, team, community, shows, storage
|
||||
from app.api import events, auth, station_config, history, team, community, shows, storage, underwriters
|
||||
|
||||
|
||||
async def _ensure_station_config(session):
|
||||
@@ -93,6 +93,7 @@ app.include_router(team.router, prefix="/api/team", tags=["team"])
|
||||
app.include_router(community.router, prefix="/api/community", tags=["community"])
|
||||
app.include_router(shows.router, prefix="/api/shows", tags=["shows"])
|
||||
app.include_router(storage.router, prefix="/api/storage", tags=["storage"])
|
||||
app.include_router(underwriters.router, prefix="/api/underwriters", tags=["underwriters"])
|
||||
|
||||
# Dev-only admin router (disabled in production)
|
||||
if settings.ENV != "production":
|
||||
|
||||
@@ -129,6 +129,18 @@ class CommunityHighlight(Base):
|
||||
active = Column(Boolean, default=True)
|
||||
|
||||
|
||||
class Underwriter(Base):
|
||||
__tablename__ = "underwriters"
|
||||
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
name = Column(String(100), nullable=False)
|
||||
logo_url = Column(String(500), nullable=True)
|
||||
description = Column(Text, nullable=False)
|
||||
website_url = Column(String(500), nullable=True)
|
||||
display_order = Column(Integer, nullable=False, default=0)
|
||||
active = Column(Boolean, default=True)
|
||||
|
||||
|
||||
class StorageBlob(Base):
|
||||
__tablename__ = "storage_blobs"
|
||||
|
||||
|
||||
@@ -184,6 +184,37 @@ class CommunityHighlightResponse(BaseModel):
|
||||
model_config = {"from_attributes": True}
|
||||
|
||||
|
||||
# ── Underwriter ────────────────────────────────────────────
|
||||
|
||||
class UnderwriterCreate(BaseModel):
|
||||
name: str
|
||||
logo_url: Optional[str] = None
|
||||
description: str
|
||||
website_url: Optional[str] = None
|
||||
display_order: int = 0
|
||||
|
||||
|
||||
class UnderwriterUpdate(BaseModel):
|
||||
name: Optional[str] = None
|
||||
logo_url: Optional[str] = None
|
||||
description: Optional[str] = None
|
||||
website_url: Optional[str] = None
|
||||
display_order: Optional[int] = None
|
||||
active: Optional[bool] = None
|
||||
|
||||
|
||||
class UnderwriterResponse(BaseModel):
|
||||
id: int
|
||||
name: str
|
||||
logo_url: Optional[str]
|
||||
description: str
|
||||
website_url: Optional[str]
|
||||
display_order: int
|
||||
active: bool
|
||||
|
||||
model_config = {"from_attributes": True}
|
||||
|
||||
|
||||
# ── ShowSchedule ──────────────────────────────────────────
|
||||
|
||||
class ShowScheduleResponse(BaseModel):
|
||||
|
||||
Reference in New Issue
Block a user