Spaces:
Sleeping
Sleeping
sync: μ΅μ λ°±μλ μ½λ λκΈ°ν (detect-only, plans, save, push μλν¬μΈνΈ μΆκ°)
59a1470 Download api/plans.py from BOWOWEEEEE/pill-advisor-backend: direct link, hf CLI and curl.
- Browser
- Download file 5.17 kB
-
https://huggingface.co/spaces/BOWOWEEEEE/pill-advisor-backend/resolve/main/api/plans.py
- Command line
-
hf download hf://spaces/BOWOWEEEEE/pill-advisor-backend/api/plans.py
-
curl -L -o plans.py https://huggingface.co/spaces/BOWOWEEEEE/pill-advisor-backend/resolve/main/api/plans.py
5.17 kB
| from datetime import date | |
| from typing import Any, List, Optional | |
| import supabase as sb | |
| from fastapi import APIRouter, Header, HTTPException | |
| from pydantic import BaseModel | |
| from services.auth import auth as _auth, get_supabase | |
| router = APIRouter() | |
| class PlanRequest(BaseModel): | |
| pill_name: str | |
| dose: Optional[str] = None | |
| timing: Optional[str] = None | |
| times: List[str] = [] | |
| pill_type: str = "rx" | |
| active: bool = True | |
| class PlanUpdateRequest(BaseModel): | |
| pill_name: Optional[str] = None | |
| dose: Optional[str] = None | |
| timing: Optional[str] = None | |
| times: Optional[List[str]] = None | |
| pill_type: Optional[str] = None | |
| active: Optional[bool] = None | |
| async def get_plans(authorization: str = Header(...)) -> list[dict[str, Any]]: | |
| """ | |
| μ¬μ©μμ λ³΅μ½ κ³ν λͺ©λ‘μ μ΅μ μμΌλ‘ λ°ννλ€. | |
| κ° planμ μ€λ μλ£ μ¬λΆ(is_done_today)λ₯Ό ν¬ν¨νλ€. | |
| """ | |
| db, user_id = _auth(authorization) | |
| result = ( | |
| db.table("medication_plans") | |
| .select("*") | |
| .eq("user_id", user_id) | |
| .order("created_at", desc=True) | |
| .execute() | |
| ) | |
| plans = result.data or [] | |
| # μ€λ μλ£λ plan_id μ§ν© μ‘°ν | |
| today = str(date.today()) | |
| done_result = ( | |
| db.table("plan_completions") | |
| .select("plan_id") | |
| .eq("user_id", user_id) | |
| .eq("completed_date", today) | |
| .execute() | |
| ) | |
| done_ids = {row["plan_id"] for row in (done_result.data or [])} | |
| for plan in plans: | |
| plan["is_done_today"] = plan["id"] in done_ids | |
| return plans | |
| async def create_plan( | |
| data: PlanRequest, | |
| authorization: str = Header(...), | |
| ) -> dict[str, Any]: | |
| """λ³΅μ½ κ³νμ μΆκ°νκ³ μμ±λ λ μ½λλ₯Ό λ°ννλ€.""" | |
| db, user_id = _auth(authorization) | |
| result = ( | |
| db.table("medication_plans") | |
| .insert( | |
| { | |
| "user_id": user_id, | |
| "pill_name": data.pill_name, | |
| "dose": data.dose, | |
| "timing": data.timing, | |
| "times": data.times, | |
| "pill_type": data.pill_type, | |
| "active": data.active, | |
| } | |
| ) | |
| .execute() | |
| ) | |
| return result.data[0] if result.data else {} | |
| async def update_plan( | |
| plan_id: str, | |
| data: PlanUpdateRequest, | |
| authorization: str = Header(...), | |
| ) -> dict[str, Any]: | |
| """λ³ΈμΈ μμ μ λ³΅μ½ κ³ν νλλ₯Ό μμ νλ€.""" | |
| db, user_id = _auth(authorization) | |
| _assert_owns_plan(db, user_id, plan_id) | |
| updates = data.model_dump(exclude_none=True) | |
| if not updates: | |
| raise HTTPException(status_code=400, detail="μμ ν νλλ₯Ό νλ μ΄μ μ λ ₯νμΈμ.") | |
| result = ( | |
| db.table("medication_plans") | |
| .update(updates) | |
| .eq("id", plan_id) | |
| .eq("user_id", user_id) | |
| .execute() | |
| ) | |
| if not result.data: | |
| raise HTTPException(status_code=500, detail="μ μ₯ μ€ν¨") | |
| return result.data[0] | |
| async def delete_plan( | |
| plan_id: str, | |
| authorization: str = Header(...), | |
| ) -> dict[str, Any]: | |
| """λ³ΈμΈ μμ μ λ³΅μ½ κ³νμ μμ νλ€.""" | |
| db, user_id = _auth(authorization) | |
| result = ( | |
| db.table("medication_plans") | |
| .delete() | |
| .eq("id", plan_id) | |
| .eq("user_id", user_id) | |
| .execute() | |
| ) | |
| deleted = bool(result.data) | |
| if not deleted: | |
| raise HTTPException(status_code=404, detail="ν΄λΉ λ³΅μ½ κ³νμ μ°Ύμ μ μμ΅λλ€.") | |
| return {"deleted": True, "id": plan_id} | |
| def _assert_owns_plan(db: sb.Client, user_id: str, plan_id: str) -> None: | |
| """plan_idκ° user_id μμ μΈμ§ νμΈνλ€. μλλ©΄ HTTP 404.""" | |
| owned = ( | |
| db.table("medication_plans") | |
| .select("id") | |
| .eq("id", plan_id) | |
| .eq("user_id", user_id) | |
| .execute() | |
| ) | |
| if not owned.data: | |
| raise HTTPException(status_code=404, detail="ν΄λΉ λ³΅μ½ κ³νμ μ°Ύμ μ μμ΅λλ€.") | |
| async def mark_plan_done( | |
| plan_id: str, | |
| authorization: str = Header(...), | |
| ) -> dict[str, Any]: | |
| """μ€λ λ μ§λ‘ λ³΅μ½ μλ£λ₯Ό κΈ°λ‘νλ€. μ΄λ―Έ μλ£λ κ²½μ° λ¬΄μ(λ©±λ±).""" | |
| db, user_id = _auth(authorization) | |
| _assert_owns_plan(db, user_id, plan_id) | |
| today = str(date.today()) | |
| db.table("plan_completions").upsert( | |
| {"user_id": user_id, "plan_id": plan_id, "completed_date": today}, | |
| on_conflict="user_id,plan_id,completed_date", | |
| ).execute() | |
| return {"plan_id": plan_id, "is_done_today": True} | |
| async def unmark_plan_done( | |
| plan_id: str, | |
| authorization: str = Header(...), | |
| ) -> dict[str, Any]: | |
| """μ€λ λ μ§μ λ³΅μ½ μλ£ κΈ°λ‘μ μ·¨μνλ€.""" | |
| db, user_id = _auth(authorization) | |
| today = str(date.today()) | |
| db.table("plan_completions").delete().eq("user_id", user_id).eq( | |
| "plan_id", plan_id | |
| ).eq("completed_date", today).execute() | |
| return {"plan_id": plan_id, "is_done_today": False} | |