diff --git a/App/controllers/routine.py b/App/controllers/routine.py new file mode 100644 index 000000000..ca9a6b826 --- /dev/null +++ b/App/controllers/routine.py @@ -0,0 +1,38 @@ +# from App.models import Routine, Workout +# from App.database import db + + +# def create_routine(name, description, user_id): +# routine = Routine(name=name, description=description, user_id=user_id) +# db.session.add(routine) +# db.session.commit() +# return routine + +# def get_all_routines(): +# return Routine.query.all() + +# def get_routine(id): +# return Routine.query.get(id) + +# def get_user_routines(user_id): +# return Routine.query.filter_by(user_id=user_id).all() + +# def update_routine(id, name=None, description=None): +# routine = Routine.query.get(id) +# if name is not None: +# routine.name = name +# if description is not None: +# routine.description = description +# db.session.commit() +# return routine + +# def add_workout_to_routine(routine_id, workout_id): +# routine = Routine.query.get(routine_id) +# workout = Workout.query.get(workout_id) +# routine.workouts.append(workout) +# db.session.commit() + +# def delete_routine(id): +# routine = Routine.query.get(id) +# db.session.delete(routine) +# db.session.commit() diff --git a/App/controllers/user.py b/App/controllers/user.py index 4165eb421..b89253783 100644 --- a/App/controllers/user.py +++ b/App/controllers/user.py @@ -1,4 +1,4 @@ -from App.models import User +from App.models import User, Workout, Routine from App.database import db def create_user(username, password): @@ -30,4 +30,39 @@ def update_user(id, username): db.session.add(user) return db.session.commit() return None - \ No newline at end of file + +def create_routine(name, description, user_id): + routine = Routine(name=name, description=description, user_id=user_id) + db.session.add(routine) + db.session.commit() + return routine + +def get_all_routines(): + return Routine.query.all() + +def get_routine(id): + return Routine.query.get(id) + +def get_user_routines(user_id): + return Routine.query.filter_by(user_id=user_id).all() + +def update_routine(id, name=None, description=None): + routine = Routine.query.get(id) + if name is not None: + routine.name = name + if description is not None: + routine.description = description + db.session.commit() + return routine + +def add_workout_to_routine(routine_id, workout_id): + routine = Routine.query.get(routine_id) + workout = Workout.query.get(workout_id) + routine.workouts.append(workout) + db.session.commit() + +def delete_routine(id): + routine = Routine.query.get(id) + db.session.delete(routine) + db.session.commit() + diff --git a/App/models/user.py b/App/models/user.py index 8efe083ab..ff15702df 100644 --- a/App/models/user.py +++ b/App/models/user.py @@ -24,3 +24,78 @@ def check_password(self, password): """Check hashed password.""" return check_password_hash(self.password, password) +class Workout(db.Model): + id = db.Column(db.Integer, primary_key=True) + exercise_name = db.Column(db.String, nullable=False) + exercise_image1 = db.Column(db.String, nullable=True) + exercise_image2 = db.Column(db.String, nullable=True) + muscle_group = db.Column(db.String, nullable=False) + equipment = db.Column(db.String, nullable=True) + rating = db.Column(db.Float, nullable=True) + description = db.Column(db.String, nullable=True) + + def __init__(self, exercise_name, exercise_image1, exercise_image2, muscle_group, equipment, rating, description): + self.exercise_name = exercise_name + self.exercise_image1 = exercise_image1 + self.exercise_image2 = exercise_image2 + self.muscle_group = muscle_group + self.equipment = equipment + self.rating = rating + self.description = description + + def get_json(self): + return { + 'id': self.id, + 'exercise_name': self.exercise_name, + 'exercise_image1': self.exercise_image1, + 'exercise_image2': self.exercise_image2, + 'muscle_group': self.muscle_group, + 'equipment': self.equipment, + 'rating': self.rating, + 'description': self.description + } + + def save(self): + db.session.add(self) + db.session.commit() + + def delete(self): + db.session.delete(self) + db.session.commit() + +exercise_routine = db.Table('exercise_routine', + db.Column('routine_id', db.Integer, db.ForeignKey('routine.id'), primary_key=True), + db.Column('exercise_id', db.Integer, db.ForeignKey('exercise.id'), primary_key=True) +) + +class Routine(db.Model): + id = db.Column(db.Integer, primary_key=True) + name = db.Column(db.String, nullable=False) + description = db.Column(db.String) + user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) + user = db.relationship('User', backref=db.backref('routines', lazy=True)) + exercises = db.relationship('Exercise', secondary=exercise_routine, backref=db.backref('routines', lazy=True)) + + def __init__(self, name, description, user_id): + self.name = name + self.description = description + self.user_id = user_id + + def get_json(self): + return { + 'id': self.id, + 'name': self.name, + 'description': self.description, + 'user_id': self.user_id, + 'exercises': [exercise.to_dict() for exercise in self.exercises] + } + + def add_exercise(self, exercise): + if exercise not in self.exercises: + self.exercises.append(exercise) + db.session.commit() + + def remove_exercise(self, exercise): + if exercise in self.exercises: + self.exercises.remove(exercise) + db.session.commit() \ No newline at end of file diff --git a/App/templates/index.html b/App/templates/index.html index 96ec19670..12b78e73b 100644 --- a/App/templates/index.html +++ b/App/templates/index.html @@ -9,5 +9,27 @@

Flask MVC

{% if is_authenticated %}

Welcome {{current_user.username}}

{% endif %} -

This is a boileplate flask application which follows the MVC pattern for structuring the project.

+

This is a boilerplate flask application which follows the MVC pattern for structuring the project.

+ +

Workouts

+ {% if workouts %} +
+ {% for workout in workouts %} +
+
+
+ Exercise Image 1 + Exercise Image 2 +
+
+

{{ workout.exercise_name }} - {{ workout.description }}

+
+
+
+ {% endfor %} +
+ {% else %} +

No workouts found.

+ {% endif %} + {% endblock %} \ No newline at end of file diff --git a/App/templates/layout.html b/App/templates/layout.html index ae01fe223..45935eb3b 100644 --- a/App/templates/layout.html +++ b/App/templates/layout.html @@ -20,6 +20,7 @@