Browse Source

Create route to update workouts.

Lee Morgan 1 year ago
parent
commit
0071e2825f
4 changed files with 72 additions and 3 deletions
  1. 46 2
      controllers/workout.js
  2. 18 0
      documentation/data.js
  3. 5 0
      models/Workout.js
  4. 3 1
      routes/workout.js

+ 46 - 2
controllers/workout.js

@@ -21,13 +21,22 @@ const addNoteRoute = async (req, res, next)=>{
     try{
         const workout = await Workout.findOne({_id: req.params.workoutId});
         confirmOwnership(workout, res.locals.user);
-        const exercise = findExercise(workout, req.body.exercise);
+        const exercise = findExercise(workout.exercises, req.body.exercise);
         exercise.notes = req.body.note;
         await workout.save();
         res.json(responseWorkout(workout));
     }catch(e){next(e)}
 }
 
+const updateWorkoutRoute = async (req, res, next)=>{
+    try{
+        const workout = await Workout.findOne({_id: req.params.workoutId});
+        workout.exercises = updateExercises(workout, req.body);
+        await workout.save();
+        res.json(responseWorkout(workout));
+    }catch(e){next(e)}
+}
+
 /*
  Create a new Workout object
  @param {Object} - Body data from the request
@@ -68,6 +77,40 @@ const findExercise = (workout, exerciseId)=>{
     return null;
 }
 
+/*
+ Update the order of exercises
+ @param {Workout} workout - Workout object
+ @param {Object} data - Request data containing exercise information
+ @return {[Object]} - Array of updated exercises
+ */
+const updateExercises = (exercises, data)=>{
+    const newExercises = [];
+    const existingIndices = [];
+    for(let i = 0; i < data.length; i++){
+        if(data[i].id){
+            const j = exercises.findIndex(e => e._id.toString() === data[i].id);
+            newExercises.push(exercises[j]);
+            existingIndices.push(j);
+        }else if(data[i].new){
+            newExercises.push({
+                name: data[i].new,
+                notes: "",
+                type: data[i].type,
+                archived: false
+            });
+        }
+    }
+
+    for(let i = 0; i < exercises.length; i++){
+        if(existingIndices.includes(i)) continue;
+
+        exercises[i].archived = true;
+        newExercises.push(exercises[i]);
+    }
+
+    return newExercises;
+}
+
 /*
  Create a new workout object for sending to the frontend
  @param {Workout} workout - Workout object
@@ -84,5 +127,6 @@ const responseWorkout = (workout)=>{
 export {
     createRoute,
     getRoute,
-    addNoteRoute
+    addNoteRoute,
+    updateWorkoutRoute
 }

+ 18 - 0
documentation/data.js

@@ -215,6 +215,24 @@ window.data = [
             desc: "Workout object"
         }]
     },
+    {
+        type: "route",
+        id: "updateWorkout",
+        title: "Update",
+        url: "PUT /workout/:workoutId",
+        auth: true,
+        description: "Update a workout. Specifically the exercises in the workout",
+        requestBody: [{
+            name: "existingExercises",
+            type: "[Object]",
+            desc: "List of exercises in order. Existing exercises should be {id: <ExerciseId>} and new exercises should be {new: <ExerciseName>}"
+        }],
+        responseBody: [{
+            name: "N/A",
+            type: "Workout",
+            desc: "Workout object"
+        }]
+    },
     {
         type: "object",
         title: "Session",

+ 5 - 0
models/Workout.js

@@ -21,6 +21,11 @@ const WorkoutSchema = new mongoose.Schema({
         type: {
             type: String,
             required: true
+        },
+        archived: {
+            type: Boolean,
+            required: true,
+            default: false
         }
     }]
 });

+ 3 - 1
routes/workout.js

@@ -1,7 +1,8 @@
 import {
     createRoute,
     getRoute,
-    addNoteRoute
+    addNoteRoute,
+    updateWorkoutRoute
 } from "../controllers/workout.js";
 
 import {userAuth} from "../auth.js";
@@ -10,4 +11,5 @@ export default (app)=>{
     app.post("/workout", userAuth, createRoute);
     app.get("/workout", userAuth, getRoute);
     app.put("/workout/:workoutId/note", userAuth, addNoteRoute);
+    app.put("/workout/:workoutId", userAuth, updateWorkoutRoute);
 }