{"size": 1505, "ext": "js", "max_stars_repo_path": "routes/exercises.js", "max_stars_repo_name": "christiamenrique/HackathonForLifeSupport", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "routes/exercises.js", "max_issues_repo_name": "christiamenrique/HackathonForLifeSupport", "max_issues_repo_issues_event_min_datetime": "2021-05-10T17:57:56.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-10T22:39:29.000Z", "max_forks_repo_path": "routes/exercises.js", "max_forks_repo_name": "christiamenrique/HackathonForLifeSupport", "max_forks_repo_forks_event_min_datetime": "2019-10-21T18:36:13.000Z", "max_forks_repo_forks_event_max_datetime": "2019-10-21T18:36:13.000Z"}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": 1, "avg_line_length": 25.9482758621, "max_line_length": 86, "alphanum_fraction": 0.542192691}

/////////////////////////////////////////////
//// API endpoints for managing exercises //
///////////////////////////////////////////
const router = require('express').Router();
let Exercise = require('../models/exercise.model');

// 1. get all exercise logs on record
router.get('/', (req, res) => {
    Exercise.find({}, (err, data) => {
        if(err) {
            res.send(err)
        } else {
            res.json(data)
        }
    })
  })

// 2. add a new exercise log
 router.post("/add", (req, res) => { 
    Exercise.create(req.body).then(data=>res.json(data)).catch(err=>console.log(err));
  });

// 3. retrieve a specfic exercise log
router.get("/:id", (req, res) => {
    Exercise.findOne().then(data=>res.json(data)).catch(err=>console.log(err));
  });


// 4. delete a specfic exercise log
router.delete("/:id",async (req, res) => {
    const exercise = await Exercise.deleteOne({_id: req.params.id})
    res.send(exercise)
})

// 5. retrieve a specific exercise log and update it 
// with information sent by client on req body
router.post("/update/:id", (req, res) => {
  let id = req.param.id
  console.log(id)
    Exercise.updateOne(
      {
        _id: req.params.id
      },
      {
        $set: {
          name: req.body.name,
          description: req.body.description,
          duration: req.body.duration,
          date: req.body.date
        }
      },{new: true}
    ).then(data=> res.json(data).catch(err=> console.error(err)))}
    );
  


module.exports = router;