diff --git a/app/routes/checkin.py b/app/routes/checkin.py index 0928f6a..73a6115 100644 --- a/app/routes/checkin.py +++ b/app/routes/checkin.py @@ -99,6 +99,72 @@ def create(): return redirect(url_for("checkin.index")) +@bp.route("/checkin//edit", methods=["GET"]) +@login_required +def edit_form(checkin_id): + user = get_current_user() + checkin = query_one( + "SELECT * FROM checkins WHERE id = %s AND user_id = %s", + (checkin_id, user["id"]), + ) + if not checkin: + return "", 404 + return render_template("partials/checkin_edit_row.html", c=checkin, user=user) + + +@bp.route("/checkin//view", methods=["GET"]) +@login_required +def view_row(checkin_id): + user = get_current_user() + checkin = query_one( + "SELECT * FROM checkins WHERE id = %s AND user_id = %s", + (checkin_id, user["id"]), + ) + if not checkin: + return "", 404 + return render_template("partials/checkin_row.html", c=checkin, user=user) + + +@bp.route("/checkin/", methods=["PUT"]) +@login_required +def update(checkin_id): + user = get_current_user() + weight_kg = request.form.get("weight_kg") + notes = request.form.get("notes", "").strip() + + checkin = query_one( + "SELECT * FROM checkins WHERE id = %s AND user_id = %s", + (checkin_id, user["id"]), + ) + if not checkin: + return "", 404 + + if not weight_kg: + flash("Weight is required.", "error") + return render_template("partials/checkin_edit_row.html", c=checkin, user=user) + + try: + weight_kg = float(weight_kg) + except ValueError: + flash("Invalid weight value.", "error") + return render_template("partials/checkin_edit_row.html", c=checkin, user=user) + + bmi = calculate_bmi(weight_kg, user.get("height_cm")) + + execute( + """UPDATE checkins SET weight_kg = %s, bmi = %s, notes = %s + WHERE id = %s AND user_id = %s""", + (weight_kg, bmi, notes or None, checkin_id, user["id"]), + ) + + check_milestones(user["id"], user) + + updated = query_one( + "SELECT * FROM checkins WHERE id = %s", (checkin_id,) + ) + return render_template("partials/checkin_row.html", c=updated, user=user) + + @bp.route("/checkin/", methods=["DELETE"]) @login_required def delete(checkin_id): diff --git a/app/templates/partials/checkin_edit_row.html b/app/templates/partials/checkin_edit_row.html new file mode 100644 index 0000000..5e7763b --- /dev/null +++ b/app/templates/partials/checkin_edit_row.html @@ -0,0 +1,19 @@ + + {{ c.checked_in_at | sydney }} + + + + auto + + + + +
+ + +
+ + \ No newline at end of file diff --git a/app/templates/partials/checkin_row.html b/app/templates/partials/checkin_row.html index de60576..bebbd4c 100644 --- a/app/templates/partials/checkin_row.html +++ b/app/templates/partials/checkin_row.html @@ -4,7 +4,11 @@ {{ '%.1f' % (c.bmi | float) if c.bmi else '—' }} {{ c.notes or '—' }} - +
+ + +
\ No newline at end of file