{"size": 897, "ext": "js", "max_stars_repo_path": "routes/Habit.js", "max_stars_repo_name": "aribshaikh/habitual-public", "max_stars_repo_stars_event_min_datetime": "2022-02-18T03:22:30.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-18T03:22:30.000Z", "max_issues_repo_path": "routes/Habit.js", "max_issues_repo_name": "aribshaikh/habitual-public", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "routes/Habit.js", "max_forks_repo_name": "aribshaikh/habitual-public", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 24.9166666667, "max_line_length": 58, "alphanum_fraction": 0.607580825}
const router = require("express").Router();
let Habit = require("../models/HabitModel");

router.route("/").get(async (req, res) => {
  await Habit.find()
    .then((posts) => res.json(posts))
    .catch((err) => res.status(400).json("Error:" + err));
});

router.route("/:id").delete(async (req, res) => {
  console.log(req.body.id)
  console.log(req.id)
  const habit = await Habit.findByIdAndRemove(req.body.id)
		if (!habit) {
			res.status(404).send()
		} else {   
			res.send({message: "Habit Deleted Successfully",habit})
		}
});

router.route("/add").post(async (req, res) => {
  const newHabit = new Habit({
    name: req.body.name,
    habit1: req.body.habit1,
    habit2: req.body.habit2,
    habit3: req.body.habit3,
  });

  await newHabit
    .save()
    .then(() => res.json("Habit added!"))
    .catch((err) => res.status(400).json("Error:" + err));
});

module.exports = router;
