{"size": 1690, "ext": "js", "max_stars_repo_path": "api/Courses.js", "max_stars_repo_name": "Moe82/TTP-Capstone-Back-End", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "api/Courses.js", "max_issues_repo_name": "Moe82/TTP-Capstone-Back-End", "max_issues_repo_issues_event_min_datetime": "2021-01-30T18:14:52.000Z", "max_issues_repo_issues_event_max_datetime": "2021-01-30T18:28:39.000Z", "max_forks_repo_path": "api/Courses.js", "max_forks_repo_name": "Moe82/TTP-Capstone-Back-End", "max_forks_repo_forks_event_min_datetime": "2021-03-02T04:25:47.000Z", "max_forks_repo_forks_event_max_datetime": "2021-03-02T04:25:47.000Z"}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": 1, "avg_line_length": 20.8641975309, "max_line_length": 64, "alphanum_fraction": 0.5366863905}
const express = require('express');
const router = express.Router();

const { Course } = require('../db/models');


// Routes for Courses
router.get('/:id', async (req, res, next) => {
  try {
    await Course.findAll({
      where:{
        teacherId: req.params.id
      }
    })
      .then(
        response => res.json(response)
      )

  } catch (error) {
    console.log(error)
  }
});

// router.get('/:id', async (req, res, next) => {
//   try {
//     await Course.findAll({
//       where: {
//         id: req.params.id
//       }
//     })
//       .then((response) => { res.status(200).json(response) })
//   } catch (err) {
//     console.log(err);
//   }
// })

router.put('/edit/:id', async (req, res, next) => {
  try {
    const data = await Course.findByPk(req.params.id);
    data.update({
      name: req.body.name
    })
    data.save();
  } catch (err) { console.log("err" + err) }
})



router.post('/', async (req, res, next) => {
  await Course.create({
    name: req.body.formValues.name,
    teacherId: req.body.teacherId
  })

    .then(course => res.status(200).json(course))
    .catch(error => {
      console.log("there is an error" + error);
      res.status(500).send("There is an error" + error);
    })
})

router.delete('/delete/:id', async (req, res, next) => {
  console.log("deleting", req.params.id)
  try {
    const data = await Course.findByPk(req.params.id)
    .then(course => {
      if (!course) {
        return res.status(404).json({
          message: "course not found"
        })
      } 
      course.destroy();
      res.status(200)
      .json(course)
    })
  } catch (err) {
    console.log(err);
  }
})

module.exports = router;