var express = require('express');
var router = express.Router();

var models = require('../../models/index');
var ZiviService = require('../../services/zivi.service');

router.get('/', function (req, res) {
  models.Zivi.find({}).sort('order').exec(function (err, response) {
    return res.json({
      zivicount: response.length,
      zivis: response
    });
  });
});

router.get('/random', function (req, res) {
  models.Zivi.find({}).then(function (response) {
    return res.json(response[Math.floor(Math.random() * response.length)])
  });
});

router.post('/delete', function (req, res) {
  var name = req.body.name;
  if (!name) {
    return res.status(400).json({error: "No name given"});
  }
  var zivi = ZiviService.findOneByName(name, function (err, zivi) {
    if (err) {
      return res.status(500).json({error: err});
    }
    ZiviService.deleteZivi(zivi, function (err) {
      if (err) {
        return res.status(500).json({error: err});
      } else {
        return res.status(200).json({error: false});
      }
    })
  });
});

router.post('/update', function (req, res) {
  var name = req.body.name;
  var spec = req.body.spec;
  if (!name || typeof name !== "string" || !spec || typeof spec !== "object") {
    return res.status(400).json({error: "body needs name (String) and spec (object) properties"});
  }
  ZiviService.updateZiviByName(name, spec, function (err, zivi) {
    if (err) {
      return res.status(500).json({error: err});
    } else {
      return res.status(200).json({zivi: zivi});
    }
  });
});

router.post('/create', function (req, res) {
  var spec = req.body.spec;
  if (!spec || typeof spec !== "object") {
    return res.status(400).json({error: "body needs spec (object) property"});
  }
  ZiviService.createZivi(spec, function (err, zivi) {
    if (err) {
      return res.status(500).json({error: err});
    } else {
      return res.status(200).json({zivi: zivi});
    }
  });
});

module.exports = router;

@@ -362,24 +362,288 @@
 %0A  %7D);%0A%7D);%0A%0A
+router.get('/rip', function (req, res) %7B%0A  models.DeletedZivi.find(%7B%7D).exec(function (err, response) %7B%0A    return res.json(%7B%0A      ripcount: response.length,%0A      ripzivis: response,%0A      condolences: 'Rest in Peace. El Se%C3%B1or misses you all.'%0A    %7D);%0A  %7D);%0A%7D);%0A%0A
 router.get('

Add endpoint to get deleted zivis