import _ from 'lodash';
import {Router} from 'express';
import {ValidationError} from 'sequelize';

import models from '../models';

const User = models.User;
const router = new Router();

// TODO add permission checks to all routes except create
// TODO handle all validation errors in one place

router.get('/', (req, res) => {

  return User
    .findAll()
    .then(data => res.send(data));
});

router.post('/', (req, res) => {

  return User
    .create(req.body)
    .then(data => res.send(data))
    .catch(ValidationError, err => {
      res.status(400).send(err.errors[0]);
    });
});

router.get('/:id', (req, res) => {

  return User
    .findById(req.params.id)
    .then(gig => res.send(gig));
});

router.put('/:id', (req, res) => {
  var fields = Object.keys(req.body);
  fields = _.without(fields, 'password');

  return User
    .update(req.body, {
      where: {id: req.params.id},
      returning: true,
      fields: fields,
    })
    .then(([count, records])=> {
      if (count !== 1) {
        // TODO throw error instead
        res.sendStatus(500);
      }

      res.send(records[0]);
    })
    .catch(ValidationError, err => {
      res.status(400).send(err.errors[0]);
    });
});

router.delete('/:id', (req, res) => {
  // TODO log username and metadata into seperate log file

  return User
    .destroy({where: {id: req.params.id}})
    .then(count => {
      var status = count === 1 ? 200 : 500;
      res.sendStatus(status);
    });
});

export default router;

@@ -53,65 +53,109 @@
 s';%0A
+%0A
 import 
-%7BValidationError%7D from 'sequeliz
+models from '../models';%0Aimport promise from './promis
 e';%0A
-%0A
 import 
-models
+%7BServerError, NotFoundError%7D
  fro
@@ -156,29 +156,33 @@
 r%7D from '../
-model
+lib/error
 s';%0A%0Aconst U
@@ -291,58 +291,8 @@
 eate
-%0A// TODO handle all validation errors in one place
 %0A%0Aro
@@ -305,33 +305,32 @@
 et('/', 
-(req, res
+promise((
 ) =%3E %7B%0A
-%0A
   return
@@ -330,29 +330,24 @@
  return User
-%0A    
 .findAll()%0A 
@@ -348,46 +348,13 @@
 ll()
-%0A    .then(data =%3E res.send(data))
 ;%0A%7D)
+)
 ;%0A%0Ar
@@ -369,33 +369,33 @@
 st('/', 
-(req, res)
+promise(req
  =%3E %7B%0A
-%0A
   return
@@ -399,21 +399,16 @@
 urn User
-%0A    
 .create(
@@ -420,132 +420,12 @@
 ody)
-%0A    .then(data =%3E res.send(data))%0A    .catch(ValidationError, err =%3E %7B%0A      res.status(400).send(err.errors%5B0%5D);%0A    %7D)
 ;%0A%7D
+)
 );%0A%0A
@@ -439,34 +439,35 @@
 get('/:id', 
-(req, res)
+promise(req
  =%3E %7B%0A%0A  ret
@@ -514,37 +514,66 @@
   .t
-hen(gig =%3E res.send(gig)
+ap(user =%3E %7B if (!user) throw new NotFoundError(); %7D
 );%0A%7D)
+)
 ;%0A%0Ar
@@ -586,34 +586,35 @@
 put('/:id', 
-(req, res)
+promise(req
  =%3E %7B%0A  var 
@@ -821,15 +821,16 @@
    .
-then
+spread
 ((
-%5B
 coun
@@ -839,18 +839,18 @@
  records
-%5D
 )
+ 
 =%3E %7B%0A   
@@ -873,197 +873,56 @@
  1) 
-%7B%0A        // TODO throw error instead%0A        res.sendStatus(500);%0A      %7D%0A%0A      res.send(records%5B0%5D);%0A    %7D)%0A    .catch(ValidationError, err =%3E %7B%0A      res.status(400).send(err.err
+throw new ServerError();%0A      return rec
 or
+d
 s%5B0%5D
-)
 ;%0A  
@@ -929,16 +929,17 @@
   %7D);%0A%7D)
+)
 ;%0A%0Aroute
@@ -959,18 +959,19 @@
 d', 
-(req, res)
+promise(req
  =%3E 
@@ -1113,93 +1113,58 @@
 =%3E %7B
-%0A      var status = 
+ if (
 count 
-=
+!
 == 1
- ? 200 : 500;%0A      res.sendStatus(status);%0A   
+) throw new ServerError();
  %7D);%0A%7D)
+)
 ;%0A%0Ae

modify user router to use promises instead of callbacks