add settings flask endpoints

This commit is contained in:
matt committed 2026-10-16 03:43:27 -07:00
1 parent 63734c87bc
commit bd9eb7798f
1 file changed
+47
+47
View File
@@ -126,6 +126,53 @@ def update_visit_route(visit_id: int):
cost=cost, location=location, time=time) cost=cost, location=location, time=time)
return jsonify(result) 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/<string:name>", 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 # Run the app if this file is executed directly
if __name__ == "__main__": if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=False) app.run(host="0.0.0.0", port=5000, debug=False)