'use strict';

const Router = require('koa-router');
const Supercar = require(__dirname + '/../models/supercars');
const bodyParser = require('koa-body-parser');
const errorHandler = require(__dirname + '/../lib/error_handler');
const supercarRouter = module.exports = Router();

module.exports = exports = supercarRouter
  .get('/supercars', function* () {
    yield Supercar.find({}, (err, data) => {
      if (err) return errorHandler(err).bind(this);
      this.response.status = 200;
      this.response.body = data;
    });
  })

  .post('/supercars', bodyParser(), function* () {
    const newSupercar = yield Supercar.create(this.request.body);
    yield newSupercar.save((err, data) => {
      if (err) return errorHandler(err).bind(this);
      this.response.status = 200;
      this.response.body = data;
    });
  })

  .put('/supercars/:id', bodyParser(), function* () {
    var carData = this.request.body;
    delete carData._id;
    Supercar.update({ _id: this.params.id }, carData, (err) => {
      if (err) return errorHandler(err).bind(this);
      this.response.status = 200;
    });
    this.response.body = { msg: 'Car information edited successfully!' };
  })

  .delete('/supercars/:id', bodyParser(), function* () {
    yield Supercar.remove({ _id: this.params.id }, err => {
      if (err) return errorHandler(err).bind(this);
      this.response.status = 200;
      this.response.body = { msg: 'Car successfully removed!' };
    });
  });

@@ -336,34 +336,34 @@
 rcars', function
-*
  
+*
 () %7B%0A    yield S
@@ -565,34 +565,34 @@
 rser(), function
-*
  
+*
 () %7B%0A    const n
@@ -862,34 +862,34 @@
 rser(), function
-*
  
+*
 () %7B%0A    var car
@@ -918,32 +918,8 @@
 dy;%0A
-    delete carData._id;%0A
     
@@ -1207,10 +1207,10 @@
 tion
-*
  
+*
 () %7B

clean up superRoute.js