/**
 * Using Rails-like standard naming convention for endpoints.
 * GET     /api/skilltree              ->  index
 * POST    /api/skilltree              ->  create
 * GET     /api/skilltree/:id          ->  show
 * PUT     /api/skilltree/:id          ->  update
 * DELETE  /api/skilltree/:id          ->  destroy
 */

'use strict';

var _ = require('lodash');
var Skilltree = require('./skilltree.model');

function handleError(res, statusCode) {
  statusCode = statusCode || 500;
  return function(err) {
    res.status(statusCode).send(err);
  };
}

function responseWithResult(res, statusCode) {
  statusCode = statusCode || 200;
  return function(entity) {
    if (entity) {
      res.status(statusCode).json(entity);
    }
  };
}

function handleEntityNotFound(res) {
  return function(entity) {
    if (!entity) {
      res.status(404).end();
      return null;
    }
    return entity;
  };
}

function saveUpdates(updates) {
  return function(entity) {
    var updated = _.merge(entity, updates);
    return updated.saveAsync()
      .spread(function(updated) {
        return updated;
      });
  };
}

function removeEntity(res) {
  return function(entity) {
    if (entity) {
      return entity.removeAsync()
        .then(function() {
          res.status(204).end();
        });
    }
  };
}

// Gets a list of Skilltrees
exports.index = function(req, res) {
  Skilltree.findAsync()
    .then(responseWithResult(res))
    .catch(handleError(res));
};

// Gets a single Skilltree from the DB
exports.show = function(req, res) {
  Skilltree.findByIdAsync(req.params.id)
    .then(handleEntityNotFound(res))
    .then(responseWithResult(res))
    .catch(handleError(res));
};

// Creates a new Skilltree in the DB
exports.create = function(req, res) {
  Skilltree.createAsync(req.body)
    .then(responseWithResult(res, 201))
    .catch(handleError(res));
};

// Updates an existing Skilltree in the DB
exports.update = function(req, res) {
  if (req.body._id) {
    delete req.body._id;
  }
  Skilltree.findByIdAsync(req.params.id)
    .then(handleEntityNotFound(res))
    .then(saveUpdates(req.body))
    .then(responseWithResult(res))
    .catch(handleError(res));
};

// Deletes a Skilltree from the DB
exports.destroy = function(req, res) {
  Skilltree.findByIdAsync(req.params.id)
    .then(handleEntityNotFound(res))
    .then(removeEntity(res))
    .catch(handleError(res));
};

/**
 * Using Rails-like standard naming convention for endpoints.
 * GET     /api/skilltree              ->  index
 * POST    /api/skilltree              ->  create
 * GET     /api/skilltree/:id          ->  show
 * PUT     /api/skilltree/:id          ->  update
 * DELETE  /api/skilltree/:id          ->  destroy
 */

'use strict';

var _ = require('lodash');
var Skilltree = require('./skilltree.model');

function handleError(res, statusCode) {
  statusCode = statusCode || 500;
  return function(err) {
    res.status(statusCode).send(err);
  };
}

function responseWithResult(res, statusCode) {
  statusCode = statusCode || 200;
  return function(entity) {
    if (entity) {
      res.status(statusCode).json(entity);
    }
  };
}

function handleEntityNotFound(res) {
  return function(entity) {
    if (!entity) {
      res.status(404).end();
      return null;
    }
    return entity;
  };
}

function saveUpdates(updates) {
  return function(entity) {
    var updated = _.merge(entity, updates);
    return updated.saveAsync()
      .spread(function(updated) {
        return updated;
      });
  };
}

function removeEntity(res) {
  return function(entity) {
    if (entity) {
      return entity.removeAsync()
        .then(function() {
          res.status(204).end();
        });
    }
  };
}

// Gets a list of Skilltrees
exports.index = function(req, res) {
  Skilltree.findAsync()
    .then(responseWithResult(res))
    .catch(handleError(res));
};

// Gets a single Skilltree from the DB
exports.show = function(req, res) {
  Skilltree.findByIdAsync(req.params.id)
    .then(handleEntityNotFound(res))
    .then(function(res) {
      res.getChildrenTree(function(err, tree) {
        console.log(tree);
      });
    })
    .then(responseWithResult(res))
    .catch(handleError(res));
};

// Creates a new Skilltree in the DB
exports.create = function(req, res) {
  Skilltree.createAsync(req.body)
    .then(responseWithResult(res, 201))
    .catch(handleError(res));
};

// Updates an existing Skilltree in the DB
exports.update = function(req, res) {
  if (req.body._id) {
    delete req.body._id;
  }
  Skilltree.findByIdAsync(req.params.id)
    .then(handleEntityNotFound(res))
    .then(saveUpdates(req.body))
    .then(responseWithResult(res))
    .catch(handleError(res));
};

// Deletes a Skilltree from the DB
exports.destroy = function(req, res) {
  Skilltree.findByIdAsync(req.params.id)
    .then(handleEntityNotFound(res))
    .then(removeEntity(res))
    .catch(handleError(res));
};

add tree population on retrieval