<commit_before>"use strict";

import _ from 'lodash';
import { Router } from 'express';
import model from './../model';
import post from './post';
import put from './put';
import del from './delete';
import { get, getAll } from './get';

const getRouter = (_conn, url, params, enableOdataSyntax) => {
  let options = params.options || {};
  let rest = params.rest || {};
  let actions = params.actions || {};

  let resourceURL = `/${url}`;
  let entityURL = `${resourceURL}\\(:id\\)`;

  let routes = [
    {
      method: 'post',
      url: resourceURL,
      controller: post,
      config: rest.post || {},
    },
    {
      method: 'put',
      url: entityURL,
      controller: put,
      config: rest.put || {},
    },
    {
      method: 'delete',
      url: entityURL,
      controller: del,
      config: rest.delete || rest.del || {},
    },
    {
      method: 'get',
      url: entityURL,
      controller: get,
      config: rest.get || {},
    },
    {
      method: 'get',
      url: resourceURL,
      controller: getAll,
      config: rest.getAll || {},
    },
  ];

  let mongooseModel = model.get(_conn, url);

  /*jshint -W064 */
  let router = Router();
  routes.map((route) => {
    router[route.method](route.url, (req, res, next) => {
      if (checkAuth(route.config.auth, req)) {
        //TODO: should run controller func after before done. [use app.post(url, auth, before, fn, after)]
        if (route.config.before) {
          if (route.method === 'post') {
            route.config.before(req.body);
          } else {
            mongooseModel.findOne({ _id: req.params.id }, (err, entity) => {
              if (err) {
                return;
              }
              route.config.before(entity);
            });
          }
        }
        route.controller(req, mongooseModel, options).then((result = {}) => {
          res.status(result.status || 200);
          if (result.text) {
            res.send(result.text);
          }
          else if (result.entity) {
            res.jsonp(result.entity);
          }
          else {
            res.end();
          }
          if (route.config.after) {
            route.config.after(result.entity, result.originEntity);
          }
        }, (err) => {
          if (err.status) {
            res.status(err.status).send(err.text || '');
          }
          else {
            next(err);
          }
        });
      }
      else {
        res.status(401).end();
      }
    });
  });

  for(let actionUrl in actions) {
    let action = actions[actionUrl];
    ((actionUrl, action) => {
      let fullUrl = `${entityURL}${actionUrl}`;
      router.post(fullUrl, (req, res, next) => {
        if(checkAuth(action.auth)) {
          action(req, res, next);
        }<commit_msg>Add res and req for before and after hook.<commit_after>"use strict";

import _ from 'lodash';
import { Router } from 'express';
import model from './../model';
import post from './post';
import put from './put';
import del from './delete';
import { get, getAll } from './get';

const getRouter = (_conn, url, params, enableOdataSyntax) => {
  let options = params.options || {};
  let rest = params.rest || {};
  let actions = params.actions || {};

  let resourceURL = `/${url}`;
  let entityURL = `${resourceURL}\\(:id\\)`;

  let routes = [
    {
      method: 'post',
      url: resourceURL,
      controller: post,
      config: rest.post || {},
    },
    {
      method: 'put',
      url: entityURL,
      controller: put,
      config: rest.put || {},
    },
    {
      method: 'delete',
      url: entityURL,
      controller: del,
      config: rest.delete || rest.del || {},
    },
    {
      method: 'get',
      url: entityURL,
      controller: get,
      config: rest.get || {},
    },
    {
      method: 'get',
      url: resourceURL,
      controller: getAll,
      config: rest.getAll || {},
    },
  ];

  let mongooseModel = model.get(_conn, url);

  /*jshint -W064 */
  let router = Router();
  routes.map((route) => {
    router[route.method](route.url, (req, res, next) => {
      if (checkAuth(route.config.auth, req)) {
        //TODO: should run controller func after before done. [use app.post(url, auth, before, fn, after)]
        if (route.config.before) {
          if (route.method === 'post') {
            route.config.before(req.body, req, res);
          } else {
            mongooseModel.findOne({ _id: req.params.id }, (err, entity) => {
              if (err) {
                return;
              }
              route.config.before(entity, req, res);
            });
          }
        }
        route.controller(req, mongooseModel, options).then((result = {}) => {
          res.status(result.status || 200);
          if (result.text) {
            res.send(result.text);
          }
          else if (result.entity) {
            res.jsonp(result.entity);
          }
          else {
            res.end();
          }
          if (route.config.after) {
            route.config.after(result.entity, result.originEntity, req, res);
          }
        }, (err) => {
          if (err.status) {
            res.status(err.status).send(err.text || '');
          }
          else {
            next(err);
          }
        });
      }
      else {
        res.status(401).end();
      }
    });
  });

  for(let actionUrl in actions) {
    let action = actions[actionUrl];
    ((actionUrl, action) => {
      let fullUrl = `${entityURL}${actionUrl}`;
      router.post(fullUrl, (req, res, next) => {
        if(checkAuth(action.auth)) {
          action(req, res, next);
        }
{'fraction_non_alphanumeric': 0.16449830689716627, 'fraction_numerical': 0.0032079843165211193, 'mean_word_length': 1.737560975609756, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3577904', 'n_tokens_mistral': 1725, 'n_tokens_neox': 1715, 'n_words': 388}