{"size": 2140, "ext": "js", "max_stars_repo_path": "controller/dns-main.js", "max_stars_repo_name": "nowhere-cloud/node-api-gate", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "controller/dns-main.js", "max_issues_repo_name": "nowhere-cloud/node-api-gate", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "controller/dns-main.js", "max_forks_repo_name": "nowhere-cloud/node-api-gate", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.0107526882, "max_line_length": 73, "alphanum_fraction": 0.5813084112}
'use strict';

const Express = require('express');
const Router  = Express.Router();
const Models  = require('../models');
const Checker = require('../helper/dns-check');

/**
 * SubApp for handling some group of functions
 */
const search = require('./dns-search');
const create = require('./dns-create');

/**
 * Route Preprocess: Add JSON Header to reduce code dupe
 */
const pp_json_header = (req, res, next) => {
  res.setHeader('Content-Type', 'application/json; charset=utf-8');
  next();
};

/**
 * GET All DNS Entries
 * Sequelize does not support Stats, but Sequelize.authenticate is useful
 * for testing the MySQL is alive or dead
 */
Router.get('/', (req, res, next) => {
  Models.dns_record.findAll().then((rsvp) => {
    res.json(rsvp);
  }).catch((err) => {
    return next(err);
  });
});

/**
 * Health Check Endpoint.
 */
Router.get('/stats', (req, res, next) => {
  Models.sequelize.authenticate().then(() => {
    res.sendStatus(200);
  }).catch((err) => {
    next({
      status: 503,
      error: err
    });
  });
});

/**
 * forward all search to related SubApp
 */
Router.use('/search', search);

Router.use('/create', create);

/**
 * Get ONE Entry by ID
 */
Router.route('/:id')
  .get((req, res, next) => {
    Checker.normalizeid(req.params.id).then((id) => {
      return Models.dns_record.findById(id);
    }).then((rsvp) => {
      res.json(rsvp);
    }).catch((err) => {
      return next(err);
    });
  }).patch((req, res, next) => {
    let uid = 0;
    let obj = JSON.stringify(req.body);
    Checker.normalizeid(req.params.id).then((id) => {
      return Models.dns_record.findById(id);
    }).then((instance) => {
      return instance.update(JSON.parse(obj));
    }).then((result) => {
      res.json(result);
    }).catch((err) => {
      return next(err);
    });
  }).delete((req, res, next) => {
    Checker.normalizeid(req.params.id).then((id) => {
      return Models.dns_record.findById(id);
    }).then((instance) => {
      return instance.destroy();
    }).then((result) => {
      res.json(result);
    }).catch((err) => {
      return next(err);
    });
  });

module.exports = Router;
