From 87a5da6f03f041e85c8cd9bd25140fa418e293de Mon Sep 17 00:00:00 2001 From: Jacob Hinkle Date: Mon, 29 Jun 2026 10:52:15 -0400 Subject: [PATCH] Add edit check-in functionality --- app/routers/checkins.py | 61 ++++++++++++++++++++++++++++++++++ app/templates/checkin_new.html | 25 ++++++++------ app/templates/checkins.html | 2 ++ 3 files changed, 77 insertions(+), 11 deletions(-) diff --git a/app/routers/checkins.py b/app/routers/checkins.py index c9fc010..dfeb20a 100644 --- a/app/routers/checkins.py +++ b/app/routers/checkins.py @@ -33,6 +33,7 @@ async def new_checkin_page(request: Request, user: User = Depends(get_current_us return templates.TemplateResponse(request, "checkin_new.html", { "user": user, "today": date.today().isoformat(), + "action": "/checkins/new", }) @@ -63,3 +64,63 @@ async def new_checkin( await session.commit() return RedirectResponse(url="/checkins", status_code=303) + + +@router.get("/checkins/{checkin_id}/edit", response_class=HTMLResponse) +async def edit_checkin_page( + request: Request, + checkin_id: int, + user: User = Depends(get_current_user), +): + async with async_session() as session: + result = await session.execute( + select(Checkin).where( + Checkin.id == checkin_id, + Checkin.user_id == user.id, + ) + ) + checkin = result.scalar_one_or_none() + if not checkin: + return templates.TemplateResponse(request, "404.html", status_code=404) + + return templates.TemplateResponse(request, "checkin_new.html", { + "user": user, + "checkin": checkin, + "action": f"/checkins/{checkin_id}/edit", + }) + + +@router.post("/checkins/{checkin_id}/edit") +async def edit_checkin( + request: Request, + checkin_id: int, + user: User = Depends(get_current_user), + date: str = Form(), + feeling: str = Form(default=""), + weight_lb: float = Form(default=None), + calories: int = Form(default=None), + steps: int = Form(default=None), + sleep_hours: float = Form(default=None), + notes: str = Form(default=""), +): + async with async_session() as session: + result = await session.execute( + select(Checkin).where( + Checkin.id == checkin_id, + Checkin.user_id == user.id, + ) + ) + checkin = result.scalar_one_or_none() + if not checkin: + return templates.TemplateResponse(request, "404.html", status_code=404) + + checkin.date = date + checkin.feeling = feeling + checkin.weight_lb = weight_lb + checkin.calories = calories + checkin.steps = steps + checkin.sleep_hours = sleep_hours + checkin.notes = notes + await session.commit() + + return RedirectResponse(url="/checkins", status_code=303) diff --git a/app/templates/checkin_new.html b/app/templates/checkin_new.html index aa2ea70..a91349f 100644 --- a/app/templates/checkin_new.html +++ b/app/templates/checkin_new.html @@ -1,46 +1,49 @@ {% extends "base.html" %} -{% block title %}Morning Check-in{% endblock %} +{% block title %}{% if checkin %}Edit{% else %}New{% endif %} Morning Check-in{% endblock %} {% block content %} -

Morning Check-in

+

{% if checkin %}Edit{% else %}New{% endif %} Morning Check-in

All fields are optional — fill in whatever you have each morning.

-
+
- +
+{% if checkin %} +Back to Check-ins +{% endif %} {% endblock %} diff --git a/app/templates/checkins.html b/app/templates/checkins.html index 7794248..8a20a6b 100644 --- a/app/templates/checkins.html +++ b/app/templates/checkins.html @@ -15,6 +15,7 @@ Calories (yesterday) Steps (yesterday) Sleep + @@ -26,6 +27,7 @@ {{ c.calories or '—' }} {{ c.steps or '—' }} {{ c.sleep_hours or '—' }} + Edit {% endfor %}