{"size": 623, "ext": "js", "max_stars_repo_path": "server/api/castles.js", "max_stars_repo_name": "nyc-kingdom/kingdom-server", "max_stars_repo_stars_event_min_datetime": "2018-03-24T20:15:00.000Z", "max_stars_repo_stars_event_max_datetime": "2018-03-24T20:15:00.000Z", "max_issues_repo_path": "server/api/castles.js", "max_issues_repo_name": "nyc-kingdom/kingdom-server", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/api/castles.js", "max_forks_repo_name": "nyc-kingdom/kingdom-server", "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": 27.0869565217, "max_line_length": 58, "alphanum_fraction": 0.6452648475}
const router = require('express').Router();
const { Castle } = require('../db/models');
const asyncHandler = require('express-async-handler')
module.exports = router;

router.get('/', asyncHandler( async(req, res, next) => {
  let castles
  if (req.query.kingdom) {
    castles = await Castle.findAll({
      where: { kingdomId: +req.query.kingdom },
    })
  }
  if (req.query.establishment) {
    castles = await Castle.findAll({
      where: { establishmentId: +req.query.establishment }
    })
  }
  if (!req.query.kingdom && !req.query.establishment) {
    castles = await Castle.findAll()
  }
  res.json(castles)
}))
