Spaces:
Sleeping
Sleeping
File size: 1,782 Bytes
59a1470 | 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 53 54 55 56 57 58 59 60 61 62 63 | from datetime import datetime, timezone
from typing import Any, List, Optional
from fastapi import APIRouter, Header, HTTPException
from pydantic import BaseModel
from services.auth import auth as _auth
router = APIRouter()
class PillResult(BaseModel):
name: str
shape: Optional[str] = None
color: Optional[str] = None
confidence: Optional[float] = None
bbox: Optional[List[float]] = None
class SaveRecordRequest(BaseModel):
image_url: Optional[str] = None
pills: List[PillResult] = []
dur_info: List[dict[str, Any]] = []
meal_last: Optional[str] = None
meal_next: Optional[str] = None
advice: Optional[str] = None
has_plan: bool = False
@router.post("/save-record")
async def save_record(
data: SaveRecordRequest,
authorization: str = Header(...),
) -> dict[str, Any]:
# has_plan์ medication_records ์ปฌ๋ผ ์์ โ ํ๋ก ํธ ํ์ ์ ์ฉ
db, user_id = _auth(authorization)
taken_at = datetime.now(tz=timezone.utc).isoformat()
detected_drugs = [pill.model_dump(exclude_none=True) for pill in data.pills]
result = (
db.table("medication_records")
.insert(
{
"user_id": user_id,
"taken_at": taken_at,
"detected_drugs": detected_drugs,
"dur_info": data.dur_info,
"llm_advice": data.advice or "",
"image_url": data.image_url,
"meal_timing_input": (
f"meal_last={data.meal_last}, meal_next={data.meal_next}"
),
}
)
.execute()
)
if not result.data:
raise HTTPException(status_code=500, detail="๊ธฐ๋ก ์ ์ฅ์ ์คํจํ์ต๋๋ค.")
return {"id": result.data[0]["id"]}
|