Add edit check-in functionality

This commit is contained in:
Jacob Hinkle 2026-06-29 10:52:15 -04:00
parent 1a2509ab34
commit 87a5da6f03
3 changed files with 77 additions and 11 deletions

View File

@ -33,6 +33,7 @@ async def new_checkin_page(request: Request, user: User = Depends(get_current_us
return templates.TemplateResponse(request, "checkin_new.html", { return templates.TemplateResponse(request, "checkin_new.html", {
"user": user, "user": user,
"today": date.today().isoformat(), "today": date.today().isoformat(),
"action": "/checkins/new",
}) })
@ -63,3 +64,63 @@ async def new_checkin(
await session.commit() await session.commit()
return RedirectResponse(url="/checkins", status_code=303) 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)

View File

@ -1,46 +1,49 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block title %}Morning Check-in{% endblock %} {% block title %}{% if checkin %}Edit{% else %}New{% endif %} Morning Check-in{% endblock %}
{% block content %} {% block content %}
<h1>Morning Check-in</h1> <h1>{% if checkin %}Edit{% else %}New{% endif %} Morning Check-in</h1>
<p><small>All fields are optional — fill in whatever you have each morning.</small></p> <p><small>All fields are optional — fill in whatever you have each morning.</small></p>
<form method="post" action="/checkins/new"> <form method="post" action="{{ action }}">
<label> <label>
Date Date
<input type="date" name="date" value="{{ today }}" required> <input type="date" name="date" value="{{ checkin.date if checkin else today }}" required>
<small>Morning of this check-in</small> <small>Morning of this check-in</small>
</label> </label>
<label> <label>
Feeling Feeling
<input type="text" name="feeling" placeholder="e.g., Good, Tired, Sore"> <input type="text" name="feeling" value="{{ checkin.feeling or '' }}" placeholder="e.g., Good, Tired, Sore">
<small>How are you feeling this morning?</small> <small>How are you feeling this morning?</small>
</label> </label>
<div class="grid"> <div class="grid">
<label> <label>
Weight (lb) Weight (lb)
<input type="number" name="weight_lb" step="0.1"> <input type="number" name="weight_lb" step="0.1" value="{{ checkin.weight_lb or '' }}">
<small>Morning weight</small> <small>Morning weight</small>
</label> </label>
<label> <label>
Calories Calories
<input type="number" name="calories"> <input type="number" name="calories" value="{{ checkin.calories or '' }}">
<small>Yesterday's total</small> <small>Yesterday's total</small>
</label> </label>
<label> <label>
Steps Steps
<input type="number" name="steps"> <input type="number" name="steps" value="{{ checkin.steps or '' }}">
<small>Yesterday's total</small> <small>Yesterday's total</small>
</label> </label>
<label> <label>
Sleep (hours) Sleep (hours)
<input type="number" name="sleep_hours" step="0.5"> <input type="number" name="sleep_hours" step="0.5" value="{{ checkin.sleep_hours or '' }}">
<small>Last night</small> <small>Last night</small>
</label> </label>
</div> </div>
<label> <label>
Notes Notes
<textarea name="notes" rows="3" placeholder="Anything else to note?"></textarea> <textarea name="notes" rows="3" placeholder="Anything else to note?">{{ checkin.notes or '' }}</textarea>
<small>Optional notes about yesterday or today</small> <small>Optional notes about yesterday or today</small>
</label> </label>
<button type="submit">Save Check-in</button> <button type="submit">{% if checkin %}Update{% else %}Save{% endif %} Check-in</button>
</form> </form>
{% if checkin %}
<a href="/checkins">Back to Check-ins</a>
{% endif %}
{% endblock %} {% endblock %}

View File

@ -15,6 +15,7 @@
<th>Calories <small>(yesterday)</small></th> <th>Calories <small>(yesterday)</small></th>
<th>Steps <small>(yesterday)</small></th> <th>Steps <small>(yesterday)</small></th>
<th>Sleep</th> <th>Sleep</th>
<th></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -26,6 +27,7 @@
<td>{{ c.calories or '—' }}</td> <td>{{ c.calories or '—' }}</td>
<td>{{ c.steps or '—' }}</td> <td>{{ c.steps or '—' }}</td>
<td>{{ c.sleep_hours or '—' }}</td> <td>{{ c.sleep_hours or '—' }}</td>
<td><a href="/checkins/{{ c.id }}/edit">Edit</a></td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>