Return number of workouts for users and average/max rpm for workouts
This commit is contained in:
14
app.py
14
app.py
@@ -26,6 +26,7 @@ class User(db.Model):
|
||||
__tablename__ = 'users'
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(255), nullable=False)
|
||||
workouts = db.relationship('Workout', backref='user', lazy=True)
|
||||
|
||||
|
||||
class Workout(db.Model):
|
||||
@@ -57,7 +58,8 @@ def users():
|
||||
if request.method == 'GET':
|
||||
# get a list of all users in the database
|
||||
users = User.query.all()
|
||||
users_list = [{'id': user.id, 'name': user.name} for user in users]
|
||||
users_list = [{'id': user.id, 'name': user.name,
|
||||
'workouts': len(user.workouts)} for user in users]
|
||||
return jsonify(users_list), 200
|
||||
|
||||
elif request.method == 'POST':
|
||||
@@ -105,11 +107,19 @@ def create_workout(user_id):
|
||||
duration_str = str(duration)
|
||||
else:
|
||||
duration_str = str(duration).split('.')[0]
|
||||
|
||||
# calculate average and maximum rpm
|
||||
rpms = [reading.rpm for reading in cadence_readings]
|
||||
avg_rpm = sum(rpms) / len(rpms)
|
||||
max_rpm = max(rpms)
|
||||
|
||||
workouts_data.append({
|
||||
'id': workout.id,
|
||||
'started_at': start_time.strftime('%a %b %d %Y %H:%M:%S'),
|
||||
'finished_at': end_time.strftime('%a %b %d %Y %H:%M:%S'),
|
||||
'duration': duration_str
|
||||
'duration': duration_str,
|
||||
'avg_rpm': int(avg_rpm),
|
||||
'max_rpm': max_rpm
|
||||
})
|
||||
return jsonify({'workouts': workouts_data}), 200
|
||||
|
||||
|
||||
Reference in New Issue
Block a user