"use strict";

var Level = require('../models').Level;
var assert = require('assert');

var controller = {
  // GET /levels
  index: function(req, res) {
    Level.findAll().then(function(levels) {
      res.format({
        html: function() {
          res.render('levels/index', {
            title:  'Levels',
            levels: levels
          });
        },
        json: function() {
          res.send(levels);
        }
      });
    });
  },

  // GET /levels/2
  show: function(req, res) {
    assert.ok(req.params.id, 'ID must be set');
    Level
      .find(req.params.id)
      .then(function(level) {
        res.render('show', { level: level });
      })
      .catch(function(err) {
        res.send(err);
      });
  },

  // POST /levels
  create: function(req, res) {
    Level
      .buildFromUrl(req.body.url, req)
      .save()
      .then(function(level) {
        res.send(level);
      })
      .catch(function(err) {
        res.status(422);
        res.send(err);
      });
  },

  // DELETE /levels/:id
  delete: function(req, res) {
    assert.equal(req.headers['authorization'], process.env.API_KEY, 'wrong API key');
    assert.ok(req.params.id, 'ID must be set');
    Level
      .find(req.params.id)
      .then(function(level) {
        level.destroy().then(function() {
          res.send({success: true});
        });
      })
      .catch(function(err) {
        res.send(err);
      });
  }
};

module.exports = controller;

@@ -626,37 +626,186 @@
 res.
-render('show', %7B level: level
+format(%7B%0A          html: function() %7B%0A            res.render('show', %7B level: level %7D);%0A          %7D,%0A          json: function() %7B%0A            res.send(level);%0A          %7D%0A       
  %7D);

Add JSON show