var db = require('../helpers/db');
var _ = require('underscore');

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

router.get('/', function(req, res) {
  var query = "SELECT * FROM continent";

  db.query(query)
  .then(function(result) {
    res.status(200).send(result.rows);
  })
  .catch(function(error) {
    res.status(500).send(error);
  });
});

router.post('/', function(req, res) {
  var name = req.body.name;

  if(!(_.isString(name))) {
    res.status(400).send("Bad Request: no name parameter");
    return;
  }

  var query = 'INSERT INTO continent ("name") VALUES ($1) RETURNING continent_id';
  var values = [name];

  db.query(query, values)
  .then(function(result) {
    res.status(201).send(result.rows[0]);
  })
  .catch(function(error) {
    res.status(500).send(error);
  });
});

router.use('/:id', function(req, res, next) {
  var id = Number(req.params.id);

  if(_.isNumber(id)) {
    next();
  } else {
    res.status(400).send("Value should be a number");
  }
});

router.get('/:id', function(req, res) {
  var query = "SELECT * FROM continent WHERE continent_id = $1";
  var values = [req.params.id];

  db.query(query, values)
  .then(function(result) {
    res.status(200).send(result.rows[0]);
  })
  .catch(function(error) {
    res.status(500).send(error);
  });
});

module.exports = router;

@@ -573,16 +573,17 @@
 ontinent
+s
  (%22name%22
@@ -621,16 +621,16 @@
 nt_id';%0A
-
   var va
@@ -1084,16 +1084,17 @@
 ontinent
+s
  WHERE c
@@ -1297,32 +1297,32 @@
 0).send(error);%0A
-
   %7D);%0A%7D);%0A%0Amodul
@@ -1316,16 +1316,457 @@
 );%0A%7D);%0A%0A
+router.put('/:id', function(req, res) %7B%0A  var name = req.body.name;%0A%0A  if(!(_.isString(name))) %7B%0A    res.status(400).send(%22Bad Request: no name parameter%22);%0A    return;%0A  %7D%0A%0A  var query = %22UPDATE continents SET name = $1 WHERE continent_id = $2%22;%0A  var values = %5Bname, req.params.id%5D;%0A%0A  db.query(query, values)%0A  .then(function(result) %7B%0A    res.status(200).end();%0A  %7D)%0A  .catch(function(error) %7B%0A    res.status(500).send(error);%0A  %7D)%0A%7D);%0A%0A
 module.e

Create put method