workout.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import Workout from "../models/Workout.js";
  2. import {HttpError} from "../HttpError.js";
  3. const createRoute = async (req, res, next)=>{
  4. try{
  5. const workout = createWorkout(req.body, res.locals.user._id);
  6. await workout.save();
  7. res.json(responseWorkout(workout));
  8. }catch(e){next(e)}
  9. }
  10. const getRoute = async (req, res, next)=>{
  11. try{
  12. const workouts = await Workout.find({user: res.locals.user._id});
  13. res.json(workouts.map(w => responseWorkout(w)));
  14. }catch(e){next(e)}
  15. }
  16. const addNoteRoute = async (req, res, next)=>{
  17. try{
  18. const workout = await Workout.findOne({_id: req.params.workoutId});
  19. confirmOwnership(workout, res.locals.user);
  20. const exercise = findExercise(workout.exercises, req.body.exercise);
  21. exercise.notes = req.body.note;
  22. await workout.save();
  23. res.json(responseWorkout(workout));
  24. }catch(e){next(e)}
  25. }
  26. const updateWorkoutRoute = async (req, res, next)=>{
  27. try{
  28. const workout = await Workout.findOne({_id: req.params.workoutId});
  29. workout.exercises = updateExercises(workout, req.body);
  30. await workout.save();
  31. res.json(responseWorkout(workout));
  32. }catch(e){next(e)}
  33. }
  34. /*
  35. Create a new Workout object
  36. @param {Object} - Body data from the request
  37. @param {ObjectId} userId - ID of the user creating the workout
  38. @return {Workout} - Workout object
  39. */
  40. const createWorkout = (data, userId)=>{
  41. return new Workout({
  42. user: userId,
  43. name: data.name,
  44. exercises: data.exercises
  45. });
  46. }
  47. /*
  48. Throw error if user does not own workout
  49. @param {Workout} workout - Workout object
  50. @param {User} user - User object
  51. */
  52. const confirmOwnership = (workout, user)=>{
  53. if(workout.user.toString() !== user._id.toString()){
  54. throw new HttpError(403, "Unauthorized");
  55. }
  56. }
  57. /*
  58. Retrieve a single exercise from a workout
  59. @param {Workout} workout - Workout object
  60. @param {String} exerciseId - ID of the exercise to retrieve
  61. @return {Object} - Exercise object
  62. */
  63. const findExercise = (workout, exerciseId)=>{
  64. for(let i = 0; i < workout.exercises.length; i++){
  65. if(workout.exercises[i]._id.toString() === exerciseId){
  66. return workout.exercises[i];
  67. }
  68. }
  69. return null;
  70. }
  71. /*
  72. Update the order of exercises
  73. @param {Workout} workout - Workout object
  74. @param {Object} data - Request data containing exercise information
  75. @return {[Object]} - Array of updated exercises
  76. */
  77. const updateExercises = (exercises, data)=>{
  78. const newExercises = [];
  79. const existingIndices = [];
  80. for(let i = 0; i < data.length; i++){
  81. if(data[i].id){
  82. const j = exercises.findIndex(e => e._id.toString() === data[i].id);
  83. newExercises.push(exercises[j]);
  84. existingIndices.push(j);
  85. }else if(data[i].new){
  86. newExercises.push({
  87. name: data[i].new,
  88. notes: "",
  89. type: data[i].type,
  90. archived: false
  91. });
  92. }
  93. }
  94. for(let i = 0; i < exercises.length; i++){
  95. if(existingIndices.includes(i)) continue;
  96. exercises[i].archived = true;
  97. newExercises.push(exercises[i]);
  98. }
  99. return newExercises;
  100. }
  101. /*
  102. Create a new workout object for sending to the frontend
  103. @param {Workout} workout - Workout object
  104. @return {Object} - Modified Workout object
  105. */
  106. const responseWorkout = (workout)=>{
  107. return {
  108. id: workout._id,
  109. name: workout.name,
  110. exercises: workout.exercises
  111. };
  112. }
  113. export {
  114. createRoute,
  115. getRoute,
  116. addNoteRoute,
  117. updateWorkoutRoute
  118. }