var Q = require('q');
var UserService = require('../services/UserService.js');
var LinkedinService = require('../services/LinkedinService.js');

// User Controller
module.exports = {

  create: function(req,res){
    var user = req.param('user');
    if (user){
      UserService.create(user)
        .then(function(response){
          console.log('sending '+JSON.stringify(response));
          res.send(response);
        });
    } else {
      res.send({success:false});
    }
  },

  find: function(req,res){
    var username;
    if (req.user) username = req.user.username;
    username = req.param('username') || username;
    if (!username){
      res.status(401).send({success:false});
    }
    UserService.find(username)
    .then(function(response){
      res.send(response);
    })
    .fail(function(err){
      console.error('Error 2: '+JSON.stringify(err));
      res.send({success:false,err:err});
    });
  },

  update: function(req,res){
    var user = req.param('user');
    UserService.update(user)
    .then(function(response){
      res.send(response);
    })
    .fail(function(err){
      console.error('Error: '+JSON.stringify(err));
      res.send({success:false,err:err});
    });
  }

};