workout.js 859 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import Workout from "../models/Workout.js";
  2. const createRoute = async (req, res, next)=>{
  3. try{
  4. const workout = createWorkout(req.body, res.locals.user._id);
  5. await workout.save();
  6. res.json(responseWorkout(workout));
  7. }catch(e){next(e)}
  8. }
  9. /*
  10. Create a new Workout object
  11. @param {Object} - Body data from the request
  12. @param {ObjectId} userId - ID of the user creating the workout
  13. @return {Workout} - Workout object
  14. */
  15. const createWorkout = (data, userId)=>{
  16. return new Workout({
  17. user: userId,
  18. exercises: data.exercises
  19. });
  20. }
  21. /*
  22. Create a new workout object for sending to the frontend
  23. @param {Workout} workout - Workout object
  24. @return {Object} - Modified Workout object
  25. */
  26. const responseWorkout = (workout)=>{
  27. return {
  28. exercises: workout.exercises
  29. };
  30. }
  31. export {
  32. createRoute
  33. }