<file_sep>const router = require('express').Router();
const Workout = require('../../models/workout.js');


router.post("/workouts", async ({ body }, res) => {
    try {
        let newWorkout = await Workout.create(body)
        if (!newWorkout) {
            res.status(400).json(newWorkout);
        }
        res.status(200).json(newWorkout);
    } catch (error) {
        res.status(400).json(error);
    }
});

router.get('/workouts', async ({ body }, res) => {
    try {
        let allWorkouts = await Workout.aggregate([
            {
                $addFields: { totalDuration: { $sum: '$exercises.duration' } }
            }
        ]);

        if (!allWorkouts) {
            res.status(500).json({ message: 'No workouts are in the database' })
        }
        res.status(200).json(allWorkouts);
    } catch (error) {
        res.status(400).json(error)
    }
})
router.put('/workouts/:id', async ({ body, params }, res) => {
    try {
        let workoutData = await Workout.findOneAndUpdate({ _id: params.id }, {
            $push: { exercises: body }
        },
            { new: true });
        if (!workoutData) {
            res.status(404).json({ message: 'Could not find the workout.' })
        }
        res.status(200).json(workoutData);


    } catch (error) {
        res.status(500).json(error)
    }
})

router.get('/workouts/range', async (req, res) => {
    try {
        const lastSevenWorkouts = await Workout.aggregate([
            { 
                $addFields: { totalDuration: {$sum: '$exercises.duration'}}
            }
        ]).sort({ date: -1}).limit(7)
        if (!lastSevenWorkouts) {
            res.status(400).json({ message: 'Your database does not have any workouts' });
        }
        res.status(200).json(lastSevenWorkouts);
    } catch (error) {
        res.status(500).json(error)
    }

})

module.exports = router;<file_sep># Workout Tracker

## Description
Workout Tracker is an app to track user's daily resistance and cardio workouts. The user can also view their last 7 workouts in their dashboard through graphs to help visualize progress.
## Table of Contents


- [Workout Tracker](#workout-tracker)
  - [Description](#description)
  - [Table of Contents](#table-of-contents)
  - [Installation](#installation)
  - [Usage](#usage)
  - [License](#license)
  - [Tests](#tests)
  - [Contributing](#contributing)
  - [Questions](#questions)
  - [Deployed Application](#deployed-application)


## Installation
***
Simply used the deployed version or run npm i to create a locally hosted version!


## Usage
***
Due to the nature of the app, the deployed version will not seperate your data from another user's data. For a personalized experience, please download and host a local version.


## License
***
Unlicensed


## Tests
***
None at this time


## Contributing
***
If you would like to contribute please email me


## Questions
If you have further questions, suggestions, or business inquiries please contact me via email.  

## Deployed Application  
[Link To Deployed App](https://evening-mountain-82600.herokuapp.com/)  

![home screem](images/home.png)<file_sep>const mongoose = require('mongoose');

const {Schema} = mongoose;

const workoutSchema = new Schema({
    exercises: [{
        name: {
            type: String,
            trim: true,
            required: "Enter the name of the new exercise.",
        },
        type: {
            type: String,
            required: 'Enter what type of workout this is.'
        },
        weight: {
            type: Number,
            default: null,
        },
        sets: {
            type: Number,
            default: null,
        },
        reps: {
            type: Number,
            default: null,
        },
        duration: {
            type: Number,
            default: null,
            required: 'Enter a duration for the exercise.'
        },
        distance: {
            type: Number,
            default: null,
        }
    }],
    day: {
        type: Date,
        default: new Date(Date.now()).toISOString()
    }
})

const Workout = mongoose.model('Workout', workoutSchema);

module.exports = Workout;