File size: 1,651 Bytes
a31fd7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import logging
from typing import Dict, Any, Optional
from fastapi import APIRouter, HTTPException, Query
from pydantic import BaseModel, Field
from services.supabase_service import save_shelter, get_shelters_in_bbox

logger = logging.getLogger("api_shelters")
router = APIRouter()

class ShelterCreateSchema(BaseModel):
    name: str = Field(..., example="Refugio Escuela Miguel Otero Silva")
    capacity: int = Field(..., ge=0)
    occupancy: int = Field(0, ge=0)
    latitude: float = Field(...)
    longitude: float = Field(...)
    resources: Optional[Dict[str, Any]] = Field(default_factory=dict)

@router.get("/bbox")
def get_shelters_in_view(
    min_lat: float = Query(...),
    min_lng: float = Query(...),
    max_lat: float = Query(...),
    max_lng: float = Query(...)
):
    """
    Fetches shelters within the visible map bounding box.
    """
    try:
        shelters = get_shelters_in_bbox(min_lat, min_lng, max_lat, max_lng)
        return shelters
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@router.post("/create")
def create_shelter(shelter: ShelterCreateSchema):
    """
    Creates a new shelter.
    """
    try:
        saved = save_shelter(
            name=shelter.name,
            capacity=shelter.capacity,
            occupancy=shelter.occupancy,
            latitude=shelter.latitude,
            longitude=shelter.longitude,
            resources=shelter.resources or {}
        )
        return {"success": True, "shelter": saved}
    except Exception as e:
        logger.error(f"Error creating shelter: {e}")
        raise HTTPException(status_code=500, detail=str(e))