| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- import Workout from "../models/Workout.js";
- const createRoute = async (req, res, next)=>{
- try{
- const workout = createWorkout(req.body, res.locals.user._id);
- await workout.save();
- res.json(responseWorkout(workout));
- }catch(e){next(e)}
- }
- const getRoute = async (req, res, next)=>{
- try{
- const workouts = await Workout.find({user: res.locals.user._id});
- res.json(workouts.map(w => responseWorkout(w)));
- }catch(e){next(e)}
- }
- /*
- Create a new Workout object
- @param {Object} - Body data from the request
- @param {ObjectId} userId - ID of the user creating the workout
- @return {Workout} - Workout object
- */
- const createWorkout = (data, userId)=>{
- return new Workout({
- user: userId,
- name: data.name,
- exercises: data.exercises
- });
- }
- /*
- Create a new workout object for sending to the frontend
- @param {Workout} workout - Workout object
- @return {Object} - Modified Workout object
- */
- const responseWorkout = (workout)=>{
- return {
- id: workout._id,
- name: workout.name,
- exercises: workout.exercises
- };
- }
- export {
- createRoute,
- getRoute
- }
|