74 lines
3.0 KiB
Python
74 lines
3.0 KiB
Python
|
|
"""
|
||
|
|
Circumference Tracking Endpoints for Mitai Jinkendo
|
||
|
|
|
||
|
|
Handles body circumference measurements (8 measurement points).
|
||
|
|
"""
|
||
|
|
import uuid
|
||
|
|
from typing import Optional
|
||
|
|
|
||
|
|
from fastapi import APIRouter, Header, Depends
|
||
|
|
|
||
|
|
from db import get_db, get_cursor, r2d
|
||
|
|
from auth import require_auth
|
||
|
|
from models import CircumferenceEntry
|
||
|
|
from routers.profiles import get_pid
|
||
|
|
|
||
|
|
router = APIRouter(prefix="/api/circumferences", tags=["circumference"])
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("")
|
||
|
|
def list_circs(limit: int=100, x_profile_id: Optional[str]=Header(default=None), session: dict=Depends(require_auth)):
|
||
|
|
"""Get circumference entries for current profile."""
|
||
|
|
pid = get_pid(x_profile_id)
|
||
|
|
with get_db() as conn:
|
||
|
|
cur = get_cursor(conn)
|
||
|
|
cur.execute(
|
||
|
|
"SELECT * FROM circumference_log WHERE profile_id=%s ORDER BY date DESC LIMIT %s", (pid,limit))
|
||
|
|
return [r2d(r) for r in cur.fetchall()]
|
||
|
|
|
||
|
|
|
||
|
|
@router.post("")
|
||
|
|
def upsert_circ(e: CircumferenceEntry, x_profile_id: Optional[str]=Header(default=None), session: dict=Depends(require_auth)):
|
||
|
|
"""Create or update circumference entry (upsert by date)."""
|
||
|
|
pid = get_pid(x_profile_id)
|
||
|
|
with get_db() as conn:
|
||
|
|
cur = get_cursor(conn)
|
||
|
|
cur.execute("SELECT id FROM circumference_log WHERE profile_id=%s AND date=%s", (pid,e.date))
|
||
|
|
ex = cur.fetchone()
|
||
|
|
d = e.model_dump()
|
||
|
|
if ex:
|
||
|
|
eid = ex['id']
|
||
|
|
sets = ', '.join(f"{k}=%s" for k in d if k!='date')
|
||
|
|
cur.execute(f"UPDATE circumference_log SET {sets} WHERE id=%s",
|
||
|
|
[v for k,v in d.items() if k!='date']+[eid])
|
||
|
|
else:
|
||
|
|
eid = str(uuid.uuid4())
|
||
|
|
cur.execute("""INSERT INTO circumference_log
|
||
|
|
(id,profile_id,date,c_neck,c_chest,c_waist,c_belly,c_hip,c_thigh,c_calf,c_arm,notes,photo_id,created)
|
||
|
|
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,CURRENT_TIMESTAMP)""",
|
||
|
|
(eid,pid,d['date'],d['c_neck'],d['c_chest'],d['c_waist'],d['c_belly'],
|
||
|
|
d['c_hip'],d['c_thigh'],d['c_calf'],d['c_arm'],d['notes'],d['photo_id']))
|
||
|
|
return {"id":eid,"date":e.date}
|
||
|
|
|
||
|
|
|
||
|
|
@router.put("/{eid}")
|
||
|
|
def update_circ(eid: str, e: CircumferenceEntry, x_profile_id: Optional[str]=Header(default=None), session: dict=Depends(require_auth)):
|
||
|
|
"""Update existing circumference entry."""
|
||
|
|
pid = get_pid(x_profile_id)
|
||
|
|
with get_db() as conn:
|
||
|
|
d = e.model_dump()
|
||
|
|
cur = get_cursor(conn)
|
||
|
|
cur.execute(f"UPDATE circumference_log SET {', '.join(f'{k}=%s' for k in d)} WHERE id=%s AND profile_id=%s",
|
||
|
|
list(d.values())+[eid,pid])
|
||
|
|
return {"id":eid}
|
||
|
|
|
||
|
|
|
||
|
|
@router.delete("/{eid}")
|
||
|
|
def delete_circ(eid: str, x_profile_id: Optional[str]=Header(default=None), session: dict=Depends(require_auth)):
|
||
|
|
"""Delete circumference entry."""
|
||
|
|
pid = get_pid(x_profile_id)
|
||
|
|
with get_db() as conn:
|
||
|
|
cur = get_cursor(conn)
|
||
|
|
cur.execute("DELETE FROM circumference_log WHERE id=%s AND profile_id=%s", (eid,pid))
|
||
|
|
return {"ok":True}
|