/**
 * We.js controller class
 */
var _ = require('lodash');

/**
 * Constructor
 */
function Controller(attrs) {
  for(var attr in attrs) {
    if (attrs[attr].bind) {
      this[attr] = attrs[attr].bind(this);
    } else {
      this[attr] = attrs[attr];
    }
  }
}

/**
 * Default crud actions
 */
Controller.prototype.find = function findAll(req, res, next) {
  return res.locals.Model.findAndCountAll(res.locals.query)
  .then(function (record) {
    if (!record) return next();

    res.locals.metadata.count = record.count;
    res.locals.data = record.rows;

    return res.ok();
  });
}

/**
 * Default findOne action
 *
 * Record is preloaded in context by default
 */
Controller.prototype.findOne = function findOne(req, res, next) {
  if (!res.locals.data) return next();

  req.we.hooks.trigger('we:after:send:ok:response', {
    res: res, req: req
  }, function (err) {
    if (err) return res.serverError(err);
    return res.ok();
  });
};

Controller.prototype.create = function create(req, res) {
  if (!res.locals.template) res.locals.template = res.locals.model + '/' + 'create';

  if (!res.locals.data) res.locals.data = {};

   _.merge(res.locals.data, req.query);

  if (req.method === 'POST') {
    if (req.isAuthenticated()) req.body.creatorId = req.user.id;

    // set temp record for use in validation errors
    res.locals.data = req.query;
    _.merge(res.locals.data, req.body);

    return res.locals.Model.create(req.body)
    .then(function (record) {
      res.locals.data = record;
      res.created();
    }).catch(res.queryError);
  } else {
    res.locals.data = req.query;
    res.ok();
  }
};

// edit and update route
Controller.prototype.edit = function edit(req, res) {
  if (!res.locals.template) res.locals.template = res.local.model + '/' + 'edit';

  var record = res.locals.data;

  if (req.method === 'POST' || req.method === 'PUT' || req.method === 'PATCH') {
    if (!record) return res.notFound();

    record.updateAttributes(req.body)
    .then(function() {
      res.locals.data = record;
      return res.updated();
    }).catch(res.queryError);
  } else {
    res.ok();
  }
};

// delete route
Controller.prototype.delete = function deletePage(req, res) {
  if (!res.locals.template)
    res.locals.template = res.local.model + '/' + 'delete';

  var record = res.locals.data;
  if (!record) return res.notFound();

  res.locals.deleteMsg = res.locals.model+'.delete.confirm.msg';

  if (req.method === 'POST' || req.method === 'DELETE') {
    record.destroy().then(function() {
      res.locals.deleted = true;
      return res.deleted();
    }).catch(res.queryError);
  } else {
    return res.ok();
  }
}

Controller.prototype.getAttribute = function getAttribute(req, res, next) {
  console.log('TODO');  next();
};

Controller.prototype.updateAttribute = function(req, res, next) {
  console.log('TODO');  next();
};

Controller.prototype.deleteAttribute = function(req, res, next) {
  console.log('TODO');  next();
};

Controller.prototype.addRecord = function(req, res, next) {
  console.log('TODO');  next();
};

Controller.prototype.removeRecord = function(req, res, next) {
  console.log('TODO');  next();
};

Controller.prototype.getRecord = function(req, res, next) {
  console.log('TODO');  next();
};

module.exports = Controller;

@@ -2662,16 +2662,30 @@
 %0A  %7D%0A%7D%0A%0A
+// TODO%0A//%0A// 
 Controll
@@ -2744,32 +2744,35 @@
 q, res, next) %7B%0A
+// 
   console.log('T
@@ -2779,36 +2779,42 @@
 ODO');  next();%0A
+// 
 %7D;%0A%0A
+// 
 Controller.proto
@@ -2855,34 +2855,37 @@
 q, res, next) %7B%0A
+//
   
+ 
 console.log('TOD
@@ -2890,36 +2890,42 @@
 ODO');  next();%0A
+// 
 %7D;%0A%0A
+// 
 Controller.proto
@@ -2966,32 +2966,35 @@
 q, res, next) %7B%0A
+// 
   console.log('T
@@ -3001,36 +3001,42 @@
 ODO');  next();%0A
+// 
 %7D;%0A%0A
+// 
 Controller.proto
@@ -3071,32 +3071,35 @@
 q, res, next) %7B%0A
+// 
   console.log('T
@@ -3106,36 +3106,42 @@
 ODO');  next();%0A
+// 
 %7D;%0A%0A
+// 
 Controller.proto
@@ -3179,32 +3179,35 @@
 q, res, next) %7B%0A
+// 
   console.log('T
@@ -3222,20 +3222,26 @@
 next();%0A
+// 
 %7D;%0A%0A
+// 
 Controll
@@ -3292,16 +3292,19 @@
 next) %7B%0A
+// 
   consol
@@ -3327,16 +3327,19 @@
 next();%0A
+// 
 %7D;%0A%0Amodu

comment uneed default controller actions