From bd9eb7798f7031716da2aff6a54b85bc42f65295 Mon Sep 17 00:00:00 2001 From: matt Date: Fri, 16 Oct 2026 03:43:27 -0700 Subject: [PATCH] add settings flask endpoints --- api/app.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/api/app.py b/api/app.py index 807502f..9b77e8b 100644 --- a/api/app.py +++ b/api/app.py @@ -126,6 +126,53 @@ def update_visit_route(visit_id: int): cost=cost, location=location, time=time) return jsonify(result) +# ----------------------------- Settings ----------------------------- +@app.route("/settings", methods=["GET", "POST"]) +def settings_collection(): + """Handle collection of settings. + + * GET: Return all settings. + * POST: Add or replace a setting. Expects JSON with ``name`` and ``value``. + """ + if request.method == "GET": + # Return all settings + return jsonify(view_settings()) + + if request.method == "POST": + data = request.get_json() + if not data: + return jsonify({"error": "Missing JSON body"}), 400 + name = data.get("name") + value = data.get("value") + if not name or value is None: + return jsonify({"error": "name and value required"}), 400 + result = add_setting(name, value) + return jsonify(result), 201 + + +@app.route("/settings/", methods=["GET", "PUT"]) +def setting_detail(name: str): + """Retrieve or update a single setting. + + * GET: Return the setting with the given name. + * PUT: Update the setting value. Expects JSON with ``value``. + """ + if request.method == "GET": + rows = view_settings([name]) + if not rows: + return jsonify({"error": "Setting not found"}), 404 + return jsonify(rows[0]) + + if request.method == "PUT": + data = request.get_json() + if not data: + return jsonify({"error": "Missing JSON body"}), 400 + value = data.get("value") + if value is None: + return jsonify({"error": "value required"}), 400 + result = change_setting(name, value) + return jsonify(result) + # Run the app if this file is executed directly if __name__ == "__main__": app.run(host="0.0.0.0", port=5000, debug=False)