cosmo/backend/app/api/event.py

75 lines
3.1 KiB
Python

"""
Celestial Events API routes
"""
import logging
from typing import List, Optional
from datetime import datetime
from fastapi import APIRouter, Depends, HTTPException, status, Query
from sqlalchemy.ext.asyncio import AsyncSession
from app.database import get_db
from app.models.schemas.social import CelestialEventCreate, CelestialEventResponse
from app.services.event_service import event_service
from app.api.deps import get_current_active_user # Assuming events can be public or require login
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/events", tags=["events"])
@router.post("", response_model=CelestialEventResponse, status_code=status.HTTP_201_CREATED)
async def create_celestial_event(
event_data: CelestialEventCreate,
current_user: dict = Depends(get_current_active_user), # Admin users for creating events?
db: AsyncSession = Depends(get_db)
):
"""Create a new celestial event (admin/system only)"""
# Further authorization checks could be added here (e.g., if current_user has 'admin' role)
try:
event = await event_service.create_event(event_data, db)
return event
except Exception as e:
logger.error(f"Error creating event: {e}")
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e))
@router.get("", response_model=List[CelestialEventResponse])
async def get_celestial_events(
db: AsyncSession = Depends(get_db),
body_id: Optional[str] = Query(None, description="Filter events by celestial body ID"),
start_time: Optional[datetime] = Query(None, description="Filter events starting from this time (UTC)"),
end_time: Optional[datetime] = Query(None, description="Filter events ending by this time (UTC)"),
limit: int = Query(100, ge=1, le=500),
offset: int = Query(0, ge=0)
):
"""Get a list of celestial events."""
try:
events = await event_service.get_events(db, body_id, start_time, end_time, limit, offset)
return events
except Exception as e:
logger.error(f"Error retrieving events: {e}")
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e))
@router.get("/{event_id}", response_model=CelestialEventResponse)
async def get_celestial_event(
event_id: int,
db: AsyncSession = Depends(get_db)
):
"""Get a specific celestial event by ID."""
event = await event_service.get_event(event_id, db)
if not event:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Celestial event not found")
return event
@router.delete("/{event_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_celestial_event(
event_id: int,
current_user: dict = Depends(get_current_active_user), # Admin users for deleting events?
db: AsyncSession = Depends(get_db)
):
"""Delete a celestial event by ID (admin/system only)"""
# Further authorization checks could be added here
deleted = await event_service.delete_event(event_id, db)
if not deleted:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Celestial event not found")
return None