workout.js 912 B

123456789101112131415161718192021222324252627282930313233343536373839
  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. name: data.name,
  19. exercises: data.exercises
  20. });
  21. }
  22. /*
  23. Create a new workout object for sending to the frontend
  24. @param {Workout} workout - Workout object
  25. @return {Object} - Modified Workout object
  26. */
  27. const responseWorkout = (workout)=>{
  28. return {
  29. name: workout.name,
  30. exercises: workout.exercises
  31. };
  32. }
  33. export {
  34. createRoute
  35. }