var Ranks = function () {
  this.respondsWith = ['html', 'json', 'xml', 'js', 'txt'];

  this.index = function (req, resp, params) {
    var self = this;

    geddy.model.Rank.all(function(err, ranks) {
      if (err) {
        throw err;
      }
      self.respondWith(ranks, {type:'Rank'});
    });
  };

  this.add = function (req, resp, params) {
    this.respond({params: params});
  };

  this.create = function (req, resp, params) {
    var self = this
      , rank = geddy.model.Rank.create(params);

    if (!rank.isValid()) {
      this.respondWith(rank);
    }
    else {
      rank.save(function(err, data) {
        if (err) {
          throw err;
        }
        self.respondWith(rank, {status: err});
      });
    }
  };

  this.show = function (req, resp, params) {
    var self = this;

    geddy.model.Rank.first(params.id, function(err, rank) {
      if (err) {
        throw err;
      }
      if (!rank) {
        throw new geddy.errors.NotFoundError();
      }
      else {
        self.respondWith(rank);
      }
    });
  };

  this.edit = function (req, resp, params) {
    var self = this;

    geddy.model.Rank.first(params.id, function(err, rank) {
      if (err) {
        throw err;
      }
      if (!rank) {
        throw new geddy.errors.BadRequestError();
      }
      else {
        self.respondWith(rank);
      }
    });
  };

  this.update = function (req, resp, params) {
    var self = this;

    geddy.model.Rank.first(params.id, function(err, rank) {
      if (err) {
        throw err;
      }
      rank.updateProperties(params);

      if (!rank.isValid()) {
        self.respondWith(rank);
      }
      else {
        rank.save(function(err, data) {
          if (err) {
            throw err;
          }
          self.respondWith(rank, {status: err});
        });
      }
    });
  };

  this.remove = function (req, resp, params) {
    var self = this;

    geddy.model.Rank.first(params.id, function(err, rank) {
      if (err) {
        throw err;
      }
      if (!rank) {
        throw new geddy.errors.BadRequestError();
      }
      else {
        geddy.model.Rank.remove(params.id, function(err) {
          if (err) {
            throw err;
          }
          self.respondWith(rank);
        });
      }
    });
  };

};

exports.Ranks = Ranks;
