<commit_before>'use strict';

// modules
const express = require('express');

// constants
const router = express.Router();

// import models
const User = require('../models/user.js');
const Workout = require('../models/workout.js');
const Exercise = require('../models/exercise.js');

// import helper functions
const getPerformedSets = require('../resources/helperFunctions.js').getPerformedSets;
const getPerformedExercises = require('../resources/helperFunctions.js').getPerformedExercises;
const updateTempModelSets = require('../resources/helperFunctions.js').updateTempModelSets;
const updateTempModelExercises = require('../resources/helperFunctions.js').updateTempModelExercises;

// GET request to show the details of a logged workout
router.get('/detail/:id', (req, res) => {
  Workout.getWorkoutAndExercises(req.params.id, (err, workout) => {
    if (err) throw err;

    res.render('workoutDetail', {workout: workout[0], exercises: workout[0].exercises, sets: workout[0].sets});
  });
});

// GET request to display interface for logging a freeform workout
router.get('/log', (req, res) => {
  if (req.user) {
    Exercise.getOwnExercises(req.user, (err, exercises) => {
      res.render('log', {exercises: exercises});
    });

  } else {
    res.redirect('/');
  }
});

// GET request to display interface for logging a specific workout
router.get('/log/:id', (req, res) => {
  if (req.user) {
    res.render('log');

  } else {
    res.redirect('/');
  }
});

// POST request upon workout completion
router.post('/complete', (req, res) => {
  if (req.user) {
    let tempWorkout = new Workout({
      name: req.body.workoutName,
      date: new Date(),
      creator: req.user._id,
      exercises: [],
      sets: []
    });

    getPerformedSets(req.user)
    .then(performedSets => {

      updateTempModelSets(tempWorkout, performedSets)
      .then(updatedTempWorkout => {

        getPerformedExercises(req.user)
        .then(performedExercises => {

          updateTempModelExercises(updatedTempWorkout, performedExercises)
          .then(newUpdatedTempWorkout => {

            Exercise.archiveSets(performedExercises, (err, result) => {
              if (err) throw err;

              Workout.createWorkout(newUpdatedTempWorkout, (err, result) => {
                if (err) throw err;

                req.flash('success', 'Workout completed!');
                res.redirect('/user')
              });
            });
          });
        });
      });
    });

  } else {
    res.redirect('/');
  }
});

// POST request to delete a workout
router.post('/delete/:id', (req, res) => {
  Workout.getWorkoutByWorkoutId(req.params.id, (err, workout) => {
    if (err) throw err;

    workout.remove((err, result) => {
      if (err) throw err;

      res.redirect('/user');
    });
  })
});

module.exports = router;<commit_msg>Protected Workout route<commit_after>'use strict';

// modules
const express = require('express');

// constants
const router = express.Router();

// import models
const User = require('../models/user.js');
const Workout = require('../models/workout.js');
const Exercise = require('../models/exercise.js');

// import helper functions
const getPerformedSets = require('../resources/helperFunctions.js').getPerformedSets;
const getPerformedExercises = require('../resources/helperFunctions.js').getPerformedExercises;
const updateTempModelSets = require('../resources/helperFunctions.js').updateTempModelSets;
const updateTempModelExercises = require('../resources/helperFunctions.js').updateTempModelExercises;

// GET request to show the details of a logged workout
router.get('/detail/:id', (req, res) => {
  userHasResource(req.user, req.params.id).then((bool) => {
    if (bool) {
      Workout.getWorkoutAndExercises(req.params.id, (err, workout) => {
        if (err) throw err;

        res.render('workoutDetail', {workout: workout[0], exercises: workout[0].exercises, sets: workout[0].sets});
      });

    } else {
      res.redirect('/');
    }
  });
});

// GET request to display interface for logging a freeform workout
router.get('/log', (req, res) => {
  if (req.user) {
    Exercise.getOwnExercises(req.user, (err, exercises) => {
      res.render('log', {exercises: exercises});
    });

  } else {
    res.redirect('/');
  }
});

// POST request upon workout completion
router.post('/complete', (req, res) => {
  if (req.user) {
    let tempWorkout = new Workout({
      name: req.body.workoutName,
      date: new Date(),
      creator: req.user._id,
      exercises: [],
      sets: []
    });

    getPerformedSets(req.user)
    .then(performedSets => {

      updateTempModelSets(tempWorkout, performedSets)
      .then(updatedTempWorkout => {

        getPerformedExercises(req.user)
        .then(performedExercises => {

          updateTempModelExercises(updatedTempWorkout, performedExercises)
          .then(newUpdatedTempWorkout => {

            Exercise.archiveSets(performedExercises, (err, result) => {
              if (err) throw err;

              Workout.createWorkout(newUpdatedTempWorkout, (err, result) => {
                if (err) throw err;

                req.flash('success', 'Workout completed!');
                res.redirect('/user')
              });
            });
          });
        });
      });
    });

  } else {
    res.redirect('/');
  }
});

// POST request to delete a workout
router.post('/delete/:id', (req, res) => {
  userHasResource(req.user, req.params.id).then((bool) => {
    if (bool) {

      Workout.getWorkoutByWorkoutId(req.params.id, (err, workout) => {
        if (err) throw err;

        workout.remove((err, result) => {
          if (err) throw err;

          res.redirect('/user');
        });
      });

    } else {
      res.redirect('/');
    }
  });
});

module.exports = router;
{'fraction_non_alphanumeric': 0.1620044619873005, 'fraction_numerical': 0.0010296893770379268, 'mean_word_length': 2.831689677843524, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1046501', 'n_tokens_mistral': 1786, 'n_tokens_neox': 1755, 'n_words': 359}