Files
mapmaker/app/routers/items.py

91 lines
2.8 KiB
Python

"""Map items router for CRUD operations."""
from typing import List
from uuid import UUID
from fastapi import APIRouter, Depends, status
from sqlalchemy.orm import Session
from app.database import get_db
from app.dependencies import get_current_user, get_optional_current_user
from app.models.user import User
from app.schemas.map_item import MapItemCreate, MapItemUpdate, MapItemResponse
from app.services import item_service
from app.services.item_service import geography_to_geojson
router = APIRouter(prefix="/api/maps/{map_id}/items", tags=["map-items"])
def format_item_response(item) -> dict:
"""Format map item for response with GeoJSON geometry."""
return {
"id": str(item.id),
"map_id": str(item.map_id),
"type": item.type,
"geometry": geography_to_geojson(item.geometry),
"properties": item.properties,
"created_at": item.created_at.isoformat(),
"updated_at": item.updated_at.isoformat(),
"created_by": str(item.created_by) if item.created_by else None,
"updated_by": str(item.updated_by) if item.updated_by else None,
}
@router.get("", response_model=List[dict])
async def list_map_items(
map_id: UUID,
current_user: User = Depends(get_optional_current_user),
db: Session = Depends(get_db)
):
"""Get all items for a map."""
items = item_service.get_map_items(db, map_id, current_user)
return [format_item_response(item) for item in items]
@router.post("", response_model=dict, status_code=status.HTTP_201_CREATED)
async def create_map_item(
map_id: UUID,
item_data: MapItemCreate,
current_user: User = Depends(get_current_user),
db: Session = Depends(get_db)
):
"""Create a new map item."""
item = item_service.create_map_item(db, map_id, item_data, current_user)
return format_item_response(item)
@router.get("/{item_id}", response_model=dict)
async def get_map_item(
map_id: UUID,
item_id: UUID,
current_user: User = Depends(get_optional_current_user),
db: Session = Depends(get_db)
):
"""Get a specific map item."""
item = item_service.get_map_item_by_id(db, item_id, current_user)
return format_item_response(item)
@router.patch("/{item_id}", response_model=dict)
async def update_map_item(
map_id: UUID,
item_id: UUID,
item_data: MapItemUpdate,
current_user: User = Depends(get_current_user),
db: Session = Depends(get_db)
):
"""Update a map item."""
item = item_service.update_map_item(db, item_id, item_data, current_user)
return format_item_response(item)
@router.delete("/{item_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_map_item(
map_id: UUID,
item_id: UUID,
current_user: User = Depends(get_current_user),
db: Session = Depends(get_db)
):
"""Delete a map item."""
item_service.delete_map_item(db, item_id, current_user)
return None