{"size": 1760, "ext": "js", "max_stars_repo_path": "server/src/lib/controller.js", "max_stars_repo_name": "larswww/SHPR", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/src/lib/controller.js", "max_issues_repo_name": "larswww/SHPR", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/src/lib/controller.js", "max_forks_repo_name": "larswww/SHPR", "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": 29.8305084746, "max_line_length": 80, "alphanum_fraction": 0.5852272727}
'use strict'

class Controller {
  constructor(facade) {
    this.facade = facade;
  }

  async create(req, res, next) {
    try {
      let doc = await this.facade.create(req.body)
      res.status(201).json({message: 'success'})
    } catch (e) {
      next({error: e, message: "Error during create.", statusCode: 500})
    }
  }

  find(req, res, next) {
    return this.facade.find(req.query, {password: 0, __v: 0})
      .then(collection => res.status(200).json(collection))
      .catch(err => next({message: "Error during find.", statusCode: 500}));
  }

  findOne(req, res, next) {
    return this.facade.findOne(req.query, {password: 0, __v: 0})
      .then(doc => res.status(200).json(doc))
      .catch(err => next({message: "Error during findOne.", statusCode: 500}));
  }

  findById(req, res, next) {
    return this.facade.findById(req.params.id, {password: 0, __v: 0})
      .then((doc) => {
        if (!doc) { return res.sendStatus(404); }
        return res.status(200).json(doc);
      })
      .catch(err => next({message: "Error during findById.", statusCode: 500}));
  }

  update(req, res, next) {
    this.facade.update({ _id: req.params.id }, req.body)
      .then((results) => {
        if (results.n < 1) { return res.sendStatus(404); }
        if (results.nModified < 1) { return res.sendStatus(304); }
        res.sendStatus(204);
      })
      .catch(err => next({message: "Error during update.", statusCode: 500}));
  }

  remove(req, res, next) {
    this.facade.remove({ _id: req.params.id })
      .then((doc) => {
        if (!doc) { return res.sendStatus(404); }
        return res.sendStatus(204);
      })
      .catch(err => next({message: "Error during remove", statusCode: 500}));
  }
}

module.exports = Controller;
