-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
56 lines (41 loc) · 2.21 KB
/
Copy pathapp.py
File metadata and controls
56 lines (41 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from flask import Flask, request, jsonify
import db
app = Flask(__name__)
db.init_db()
@app.get('/workouts')
def get_workouts():
return jsonify(db.get_all_workouts())
@app.route("/workouts", methods=["POST"])
def create_workout():
data = request.get_json(silent=True)
if data is None:
return jsonify({"error": "Invalid request body or empty body. Must be JSON."}), 400
if data.get("sport") is None or data.get("sport") == "":
return jsonify({"error": "The key sport is missing or empty. Must be swim, run, badminton, cycle, strength, or rest."}), 400
allowed_sports = ["swim", "run", "badminton", "cycle", "strength", "rest"]
sport = data["sport"]
if sport not in allowed_sports:
return jsonify({"error": "The key sport is invalid. Must be swim, run, badminton, cycle, strength, or rest."}), 400
optional_distance = data.get("distance_km")
optional_duration = data.get("duration_min")
if optional_duration is None and (sport in ["run", "swim", "cycle", "badminton", "strength"]):
return jsonify({"error": "The key duration_min is missing."}), 400
if optional_distance is None and (sport in ["run", "swim", "cycle"]):
return jsonify({"error": "The key distance_km is missing."}), 400
if optional_duration is not None and not isinstance(optional_duration, (int, float)):
return jsonify({"error": "The key duration_min must be a number."}), 400
if optional_distance is not None and not isinstance(optional_distance, (int, float)):
return jsonify({"error": "The key distance_km must be a number."}), 400
if optional_duration is not None and optional_duration < 0:
return jsonify({"error": "The key duration_min must be a positive number or 0."}), 400
if optional_distance is not None and optional_distance < 0:
return jsonify({"error": "The key distance_km must be a positive number or 0."}), 400
return jsonify(db.add_workout(data))
@app.get('/workouts/<int:workout_id>')
def get_workout_by_id(workout_id):
workout = db.get_workout_by_id(workout_id)
if workout is None:
return jsonify({"error": "Workout not found."}), 404
return jsonify(workout)
if __name__ == "__main__":
app.run(debug=True)