{"size": 2001, "ext": "js", "max_stars_repo_path": "routers/reviewsRouter.js", "max_stars_repo_name": "Nick-Ballenger/philanthropio-backend", "max_stars_repo_stars_event_min_datetime": "2019-04-19T17:56:36.000Z", "max_stars_repo_stars_event_max_datetime": "2019-05-23T20:04:38.000Z", "max_issues_repo_path": "routers/reviewsRouter.js", "max_issues_repo_name": "Nick-Ballenger/philanthropio-backend", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "routers/reviewsRouter.js", "max_forks_repo_name": "Nick-Ballenger/philanthropio-backend", "max_forks_repo_forks_event_min_datetime": "2019-04-28T01:29:18.000Z", "max_forks_repo_forks_event_max_datetime": "2019-06-27T18:00:44.000Z"}
{"max_stars_count": 3, "max_issues_count": null, "max_forks_count": 3, "avg_line_length": 25.3291139241, "max_line_length": 80, "alphanum_fraction": 0.5607196402}
const router = require("express").Router();

const db = require("../data/helpers/reviews-model.js");

router.get("/:id", async (req, res) => {
  const { id } = req.params;
  try {
    const review = await db.findById(id);
    if (review) {
      res.status(200).json(review);
    } else {
      res
        .status(404)
        .json({ message: "Review with specified ID does not exist." });
    }
  } catch (error) {
    res.status(500).json({ message: `Review request failed ${error}.` });
  }
});

router.post("/", async (req, res) => {
  const { review, rating, book_id, user_id } = req.body;
  if (!review || !rating || !book_id || !user_id) {
    res
      .status(401)
      .json({ message: "Please do not leave any of the review fields blank." });
  } else {
    try {
      const newReview = await db.create(req.body);
      if (newReview) {
        res.status(201).json(newReview);
      }
    } catch (error) {
      res
        .status(500)
        .json({ message: `Your review could not be posted ${error}.` });
    }
  }
});

router.delete("/:id", async (req, res) => {
  const { id } = req.params;
  try {
    const review = await db.remove(id);
    if (review) {
      res.status(200).json(review);
    } else {
      res
        .status(404)
        .json({ message: "The review with the specified ID does not exist." });
    }
  } catch (error) {
    res.status(500).json({
      message: `The review's information could not be modified: ${error}.`
    });
  }
});

router.put("/:id", async (req, res) => {
  const { id } = req.params;
  const review = req.body;
  try {
    const editedReview = await db.update(review, id);
    if (editedReview) {
      res.status(200).json(editedReview);
    } else {
      res.status(404).json({
        message: "The review with the specified ID does not exist."
      });
    }
  } catch (error) {
    res.status(500).json({
      message: `The review's information could not be modified: ${error}.`
    });
  }
});

module.exports = router;
