diff --git a/app.py b/app.py index 52faf1c..503d05b 100644 --- a/app.py +++ b/app.py @@ -28,7 +28,17 @@ class User(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(255), nullable=False) + bike_id = db.Column(db.Integer, db.ForeignKey( + 'bikes.id', ondelete='CASCADE'), nullable=False) workouts = db.relationship('Workout', backref='user', lazy=True) + bike = db.relationship('Bike', backref='user', lazy=True) + + +class Bike(db.Model): + __tablename__ = 'bikes' + id = db.Column(db.Integer, primary_key=True) + display_name = db.Column(db.String(255), nullable=False) + code_name = db.Column(db.String(255), nullable=False) class Workout(db.Model): @@ -48,10 +58,13 @@ class CadenceReading(db.Model): 'workouts.id', ondelete='CASCADE'), nullable=False) created_at = db.Column(db.DateTime, nullable=False) rpm = db.Column(db.Integer, nullable=False) + distance = db.Column(db.Numeric, nullable=False) + speed = db.Column(db.Numeric, nullable=False) + calories = db.Column(db.Numeric, nullable=False) + power = db.Column(db.Integer, nullable=False) -@app.route('/', methods=['GET']) -def get_workouts(): +def render_users_and_workouts(): users = User.query.all() users_data = [] for user in users: @@ -59,11 +72,17 @@ def get_workouts(): user_data = { 'id': user.id, 'name': user.name, + 'bike_id': user.bike_id, 'workouts_count': len(workouts), 'workouts': workouts } users_data.append(user_data) - return render_template('users_and_workouts.html', users=users_data) + return render_template('users_and_workouts.html', users=users_data, bikes=Bike.query.all()) + + +@app.route('/', methods=['GET']) +def get_workouts(): + return render_users_and_workouts() @app.route("/user//new_workout") @@ -85,14 +104,14 @@ def users(): # create a new user data = request.form name = data['name'] + bike_id = data['bike_id'] # create a new user and add it to the database - new_user = User(name=name) + new_user = User(name=name, bike_id=bike_id) db.session.add(new_user) db.session.commit() - users = User.query.all() - return render_template('users.html', users=users) + return render_users_and_workouts() @app.route('/user/', methods=['DELETE']) @@ -101,8 +120,7 @@ def delete_user(user_id): if user: db.session.delete(user) db.session.commit() - users = User.query.all() - return render_template('users.html', users=users) + return render_users_and_workouts() else: return jsonify({'error': 'User not found.'}), 404 @@ -115,7 +133,7 @@ def workouts(user_id): elif request.method == 'POST': data = request.json - rpm_readings = data['workout'] + data = data['workout'] # create a new workout workout = Workout(user_id=user_id) @@ -123,9 +141,9 @@ def workouts(user_id): db.session.commit() # add cadence readings to the workout - for reading in rpm_readings: + for d in data: cadence_reading = CadenceReading( - workout_id=workout.id, created_at=reading['timestamp'], rpm=reading['rpm']) + workout_id=workout.id, created_at=d['timestamp'], rpm=d['rpm'], distance=d['distance'], speed=d['speed'], calories=d['calories'], power=d['power']) db.session.add(cadence_reading) db.session.commit() @@ -186,6 +204,19 @@ def workouts_for_user(user_id): return render_template('workouts_list.html', workouts=workouts_data) +@app.route('/user//bike', methods=['GET']) +def update_users_bike(user_id): + bike_id = request.args.get('bike_id') + user = User.query.get(user_id) + bike = Bike.query.get(bike_id) + if user and bike: + user.bike_id = bike_id + db.session.commit() + return render_users_and_workouts() + else: + return jsonify({'error': 'User or bike not found.'}), 404 + + def get_workouts_for_user(user_id): workouts_data = [] workouts = Workout.query.filter_by(user_id=user_id).order_by( diff --git a/database/001_create_tables.sql b/database/001_create_tables.sql index be85ffd..1510077 100644 --- a/database/001_create_tables.sql +++ b/database/001_create_tables.sql @@ -1,5 +1,9 @@ CREATE TABLE - users (id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL); + users ( + id SERIAL PRIMARY KEY, + name VARCHAR(255) NOT NULL, + bike_id INTEGER NOT NULL REFERENCES bikes (id) ON DELETE CASCADE DEFAULT '1', + ); CREATE TABLE workouts ( @@ -14,5 +18,26 @@ CREATE TABLE workout_id INTEGER NOT NULL REFERENCES workouts (id) ON DELETE CASCADE, created_at TIMESTAMP NOT NULL, rpm INTEGER NOT NULL, + distance NUMERIC NOT NULL DEFAULT '0', + power NUMERIC NOT NULL DEFAULT '0', + speed NUMERIC NOT NULL DEFAULT '0', + calories NUMERIC NOT NULL DEFAULT '0', CONSTRAINT unique_cadence_reading_per_workout_time UNIQUE (workout_id, created_at) - ); \ No newline at end of file + ); + +CREATE TABLE + bikes ( + id SERIAL PRIMARY KEY, + display_name VARCHAR(255) NOT NULL, + code_name VARCHAR(255) NOT NULL + ); + +INSERT INTO + bikes (code_name, display_name) +VALUES + ('ad6', 'AirDyne6'), + ('aab', 'Assault Air Bike'), + ('echo', 'Echo Bike'), + ('bikeergDamper10', 'BikeErg D10'), + ('bikeergDamper5', 'BikeErg D5'), + ('bikeergDamper1', 'BikeErg D1'); \ No newline at end of file diff --git a/templates/new_workout.html b/templates/new_workout.html index b8e5e67..0c4b6c6 100644 --- a/templates/new_workout.html +++ b/templates/new_workout.html @@ -23,7 +23,7 @@
- {{ user.name }} + {{ user.name }} - {{ user.bike.display_name }}
@@ -324,8 +324,8 @@ // Add the new data point to the data array data.push(newData); - let watts = generators.aab.rpm.power(rpm) - let speed = generators.aab.rpm.speed(rpm) + let watts = generators['{{ user.bike.code_name }}'].rpm.power(rpm) + let speed = generators['{{ user.bike.code_name }}'].rpm.speed(rpm) if (previousReadingTime) { distance += calculateDistance(previousReadingTime, new Date(), speed) calories += calculateCalories(previousReadingTime, new Date(), watts) diff --git a/templates/users.html b/templates/users.html index acf6185..dc61e76 100644 --- a/templates/users.html +++ b/templates/users.html @@ -2,7 +2,8 @@ Name - Workouts + Bike + @@ -19,19 +20,39 @@ -
- {{ u.name }} +
+

{{ u.name }}

+

({{ u.workouts_count }})

- + +
+ +
+ + + +
+
+ +
-
{{ u.workouts_count }}
+
+
+ hx-target="#users-container" class="cursor-pointer pr-2">
-
+
-
-
- - +
+
+
+ + +
+
+ +
+ +
+ + + +
+
+
+ +
\ No newline at end of file diff --git a/templates/users_and_workouts.html b/templates/users_and_workouts.html index 9773c34..bdb9bdb 100644 --- a/templates/users_and_workouts.html +++ b/templates/users_and_workouts.html @@ -33,19 +33,19 @@
-
-
-
+
+
+
- {% with users=users %} + {% with users=users, bikes=bikes %} {% include 'users.html' %} {% endwith %}
-
+