Spaces:
Runtime error
Runtime error
Update main.py
Browse files945 unmodified lines94694794894995095195295395495595695795895095195996096195395495595695795895996096296396496596696796896997097197297396696796897497597697797097897998097398198297597697797897998098198298398498598698798898999099199299399499599699798398499910009859869879889899909919921 unmodified line9949959961008997998999101010111000100110021013100310041005100610071008945 unmodified lines
class MapIssue(BaseModel): user_id: Optional[str] community_id: Optional[str] title: str description: Optional[str] = None latitude: float longitude: float image_url: Optional[str] = None
@app.get("/map-issues")def get_map_issues(community_id: str): """Fetch all map issues for a given community."""async def get_map_issues(community: str): """Return all map issues for a given community name.""" try: res = ( supabase.table("map_issues") .select("*, profiles(first_name, last_name)") .eq("community_id", community_id) .order("created_at", desc=True) .execute() ) return res.data res = supabase.table("communities").select("id").eq("name", community).execute() if not res.data: return [] # ✅ always return a list community_id = res.data[0]["id"]
data = supabase.table("map_issues").select("*").eq("community_id", community_id).execute() return data.data or [] # ✅ ensure an array, not None except Exception as e: raise HTTPException(status_code=500, detail=f"Error fetching map issues: {str(e)}")
@app.post("/map-issues")async def create_map_issue( user_id: str = Form(...), community_id: str = Form(...),async def add_map_issue( user_id: Optional[str] = Form(None), community_id: Optional[str] = Form(None), title: str = Form(...), description: str = Form(None), description: Optional[str] = Form(None), latitude: float = Form(...), longitude: float = Form(...), image: Optional[UploadFile] = File(None) image_url: Optional[str] = Form(None)): """ Create a new map issue (with optional image upload). """ image_url = None
# ✅ Handle image upload if provided if image: MAX_FILE_SIZE = 5 * 1024 * 1024 # 5 MB limit if image.size > MAX_FILE_SIZE: raise HTTPException(status_code=413, detail="Image is too large. Limit is 5 MB.")
try: file_extension = image.filename.split('.')[-1] unique_filename = f"{uuid.uuid4()}.{file_extension}" supabase.storage.from_("map-issue-images").upload( file=await image.read(), path=unique_filename, file_options={"content-type": image.content_type} ) image_url = supabase.storage.from_("map-issue-images").get_public_url(unique_filename) except Exception as e: raise HTTPException(status_code=500, detail=f"Failed to upload image: {str(e)}")
"""Insert a new map issue marker.""" try: # ✅ Insert issue record data = { if not community_id: raise HTTPException(status_code=400, detail="Missing community_id")
issue = { "id": str(uuid.uuid4()), "user_id": user_id, "community_id": community_id, "title": title,1 unmodified line "latitude": latitude, "longitude": longitude, "image_url": image_url, "status": "active", "status": "active" }
res = supabase.table("map_issues").insert(data).execute() return {"message": "Map issue created successfully.", "data": res.data[0]} res = supabase.table("map_issues").insert(issue).execute() return {"success": True, "data": res.data[0]} if res.data else {"success": False} except Exception as e: raise HTTPException(status_code=500, detail=f"Failed to create map issue: {str(e)}") raise HTTPException(status_code=500, detail=f"Failed to add issue: {str(e)}")
@app.patch("/admin/reply-map-issue")def reply_to_map_issue(issue_id: str, reply: str, admin_id: str): """main.py+35-44