Spaces:
Runtime error
Runtime error
Update main.py
Browse files945 unmodified lines94694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031 unmodified line1005100610071008100910101011101210131014101510161017945 unmodified lines@app.get("/map-issues")def get_map_issues(community_id: str):"""Fetch all map issues for a given community."""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.dataexcept 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(...),title: str = Form(...),description: str = Form(None),latitude: float = Form(...),longitude: float = Form(...),image: Optional[UploadFile] = File(None)):"""Create a new map issue (with optional image upload)."""image_url = None# ✅ Handle image upload if providedif image:MAX_FILE_SIZE = 5 * 1024 * 1024 # 5 MB limitif 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)}")try:# ✅ Insert issue recorddata = {"user_id": user_id,"community_id": community_id,"title": title,1 unmodified line"latitude": latitude,"longitude": longitude,"image_url": image_url,"status": "active",}res = supabase.table("map_issues").insert(data).execute()return {"message": "Map issue created successfully.", "data": res.data[0]}except Exception as e:raise HTTPException(status_code=500, detail=f"Failed to create map issue: {str(e)}")@app.patch("/admin/reply-map-issue")def reply_to_map_issue(issue_id: str, reply: str, admin_id: str):"""945 unmodified lines9469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919921 unmodified line994995996997998999100010011002100310041005100610071008945 unmodified linesclass MapIssue(BaseModel):user_id: Optional[str]community_id: Optional[str]title: strdescription: Optional[str] = Nonelatitude: floatlongitude: floatimage_url: Optional[str] = None@app.get("/map-issues")async def get_map_issues(community: str):"""Return all map issues for a given community name."""try:res = supabase.table("communities").select("id").eq("name", community).execute()if not res.data:return [] # ✅ always return a listcommunity_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 Noneexcept Exception as e:raise HTTPException(status_code=500, detail=f"Error fetching map issues: {str(e)}")@app.post("/map-issues")async def add_map_issue(user_id: Optional[str] = Form(None),community_id: Optional[str] = Form(None),title: str = Form(...),description: Optional[str] = Form(None),latitude: float = Form(...),longitude: float = Form(...),image_url: Optional[str] = Form(None)):"""Insert a new map issue marker."""try: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"}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 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