125 lines
3.3 KiB
Python
125 lines
3.3 KiB
Python
"""
|
|
Static Data Management API routes
|
|
Handles static celestial data like stars, constellations, galaxies
|
|
"""
|
|
from fastapi import APIRouter, HTTPException, Depends, status
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from pydantic import BaseModel
|
|
from typing import Optional, Dict, Any
|
|
|
|
from app.database import get_db
|
|
from app.services.db_service import static_data_service
|
|
|
|
router = APIRouter(prefix="/celestial/static", tags=["celestial-static"])
|
|
|
|
|
|
# Pydantic models
|
|
class StaticDataCreate(BaseModel):
|
|
category: str
|
|
name: str
|
|
name_zh: Optional[str] = None
|
|
data: Dict[str, Any]
|
|
|
|
|
|
class StaticDataUpdate(BaseModel):
|
|
category: Optional[str] = None
|
|
name: Optional[str] = None
|
|
name_zh: Optional[str] = None
|
|
data: Optional[Dict[str, Any]] = None
|
|
|
|
|
|
@router.get("/list")
|
|
async def list_static_data(db: AsyncSession = Depends(get_db)):
|
|
"""Get all static data items"""
|
|
items = await static_data_service.get_all_items(db)
|
|
result = []
|
|
for item in items:
|
|
result.append({
|
|
"id": item.id,
|
|
"category": item.category,
|
|
"name": item.name,
|
|
"name_zh": item.name_zh,
|
|
"data": item.data
|
|
})
|
|
return {"items": result}
|
|
|
|
|
|
@router.post("", status_code=status.HTTP_201_CREATED)
|
|
async def create_static_data(
|
|
item_data: StaticDataCreate,
|
|
db: AsyncSession = Depends(get_db)
|
|
):
|
|
"""Create new static data"""
|
|
new_item = await static_data_service.create_static(item_data.dict(), db)
|
|
return new_item
|
|
|
|
|
|
@router.put("/{item_id}")
|
|
async def update_static_data(
|
|
item_id: int,
|
|
item_data: StaticDataUpdate,
|
|
db: AsyncSession = Depends(get_db)
|
|
):
|
|
"""Update static data"""
|
|
update_data = {k: v for k, v in item_data.dict().items() if v is not None}
|
|
updated = await static_data_service.update_static(item_id, update_data, db)
|
|
if not updated:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"Static data {item_id} not found"
|
|
)
|
|
return updated
|
|
|
|
|
|
@router.delete("/{item_id}")
|
|
async def delete_static_data(
|
|
item_id: int,
|
|
db: AsyncSession = Depends(get_db)
|
|
):
|
|
"""Delete static data"""
|
|
deleted = await static_data_service.delete_static(item_id, db)
|
|
if not deleted:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"Static data {item_id} not found"
|
|
)
|
|
return {"message": "Deleted successfully"}
|
|
|
|
|
|
@router.get("/categories")
|
|
async def get_static_categories(db: AsyncSession = Depends(get_db)):
|
|
"""
|
|
Get all available static data categories
|
|
"""
|
|
categories = await static_data_service.get_all_categories(db)
|
|
return {"categories": categories}
|
|
|
|
|
|
@router.get("/{category}")
|
|
async def get_static_data(
|
|
category: str,
|
|
db: AsyncSession = Depends(get_db)
|
|
):
|
|
"""
|
|
Get all static data items for a specific category
|
|
(e.g., 'star', 'constellation', 'galaxy')
|
|
"""
|
|
items = await static_data_service.get_by_category(category, db)
|
|
|
|
if not items:
|
|
raise HTTPException(
|
|
status_code=404,
|
|
detail=f"No data found for category '{category}'"
|
|
)
|
|
|
|
result = []
|
|
for item in items:
|
|
result.append({
|
|
"id": item.id,
|
|
"name": item.name,
|
|
"name_zh": item.name_zh,
|
|
"data": item.data
|
|
})
|
|
|
|
return {"category": category, "items": result}
|