<file_sep>'use strict';

import './../assets/css/master.css';

import angular from 'angular';
import ngAnimate from 'angular-animate';
import uiRouter from 'angular-ui-router';
import routing from './app.config';

import header from './directives/_shared/header';
import nav from './directives/_shared/nav';
import sideBar from './directives/_shared/sideBar';
import usersTable from './routes/usersTable.module.js'
import dashboard from './routes/dashboard.module.js'

export default angular.module('app', [
	uiRouter,
	ngAnimate,
	dashboard.name,
	usersTable.name,
	header.name,
	nav.name,
	sideBar.name
]).config(routing);
<file_sep>'use strict';

var bcrypt = require('bcrypt');
var connection = require('../connections/postgres');
var _ = require('lodash');

module.exports = {
	getUsers: () => {
		return new Promise((resolve, reject) => {
			connection.then((client) => {
				client.query('SELECT * FROM users', (err, result) => {
					if (err) { return reject(err) }
					resolve(result.rows);
				});
			});
		});
	},

	getUser: (props) => {
		return new Promise((resolve, reject) => {
			connection.then((client) => {
				client.query(
					'SELECT * FROM users WHERE user_id = $1',
					[props.userId],
					(err, result) => {
						if (err) { return reject(err); }
						resolve(result.rows);
					}
				)
			});
		});
	},

	editUser: (props, params) => {
		return new Promise((resolve, reject) => {
			params = _.pick(params, ["user_name", "first_name", "last_name", "password"]);
			var i = 1;
			var query = ['UPDATE users', 'SET'];
			var set = [];
			var values = [];
			_.forOwn(params, (value, key) => {
				set.push(key + ' = ($' + i + ')');
				values.push(value);
				i++;
			});
			query.push(set.join(', '));
			query.push('WHERE user_id = ' + props.userId + ' RETURNING *');
			query = query.join(' ');
			connection.then((client) => {
				client.query(
					query,
					values,
					(err, result) => {
						if (err) { return reject(err); }
						resolve(result.rows[0]);
					}
				)
			});
		});
	},

	comparePassword: (props) => {
		return new Promise((resolve, reject) => {
			connection.then((client) => {
				client.query(
					'SELECT * FROM users WHERE user_name = $1',
					[props.userName],
					(err, result) => {
						if (err) { return reject(err); }
						if (!result.rows.length) { return resolve(false); }
						bcrypt.compare(props.password, result.rows[0].password_hash, (err, same) => {
							if (err) { return reject(err); }
							if (!same) { return resolve(false); }
							return resolve(result.rows[0]);
						});
					}
				)
			});
		})
	},

	createUser: (props) => {
		return new Promise((resolve, reject) => {
			bcrypt.hash(props.password, 10, (err, hash) => {
				if (err) { return; }
				connection.then((client) => {
					client.query(
						'INSERT INTO users (first_name, last_name, user_name, password_hash) VALUES($1, $2, $3, $4) RETURNING *',
						[props.first_name, props.last_name, props.user_name, hash],
						(err, result) => {
							if (err) { return reject(err); }
							resolve(result.rows[0]);
						});
				});
			});
		});
	},

	deleteUser: (props) => {
		return new Promise((resolve, reject) => {
			connection.then((client) => {
				client.query(
					'DELETE FROM users WHERE user_id = $1',
					[props.userId],
					(err, result) => {
						if (err) { return reject(err); }
						resolve(result);
					}
				)
			});
		});
	}
};
<file_sep>import angular from 'angular';
import ngAnimate from 'angular-animate';

export default angular.module('directives.sideBar', [ngAnimate])
	.directive('sideBar', () => {
		return {
			restrict: 'E',
			templateUrl: 'templates/sideBar.html',
			replace: true,
			controller: ($scope) => {
				$scope.elements = [1,2,3];

				$scope.linkClicked = (index) => {
					console.log(index);
				};

				$scope.bool = true;
			}
		};
	});
<file_sep>routes.$inject = ['$stateProvider'];

export default function routes($stateProvider) {
	$stateProvider
		.state('usersTable', {
			url: '/users-table',
			templateUrl: 'templates/usersTable.html'
		});
}
<file_sep>'use strict';

import angular from 'angular';

export default angular.module('directives.header', [])
	.directive('header', () => {
		return {
			restrict: 'E',
			templateUrl: 'templates/header.html',
			controller: ($scope) => {
				console.log($scope);
			},
			link: function (scope, element, attrs) {
				console.log(scope);
			}
			}
	});
<file_sep>import angular from 'angular';
import uiRouter from 'angular-ui-router';

import routing from './usersTable';
import usersTable from './../directives/_shared/usersTable';

export default angular.module('app.usersTable', [uiRouter])
	.config(routing)
	.directive('usersTable', usersTable);
<file_sep>'use strict';

var config = require('./src/services/config');
var express = require('express');
var expressJWT = require('express-jwt');
var app = express();

app.use((err, req, res, next) => { console.log(err); });

//app.use(expressJWT({secret: config.secret}).unless({path: ['/sessions', '/users']}));

app.use(require('http-responses'));
app.use(require('body-parser').json());

app.use('/users', require('./src/controllers/user'));
app.use('/sessions', require('./src/controllers/session'));

app.listen(config.port);
<file_sep>'use strict';

var express = require('express');
var router = express.Router();
var _ = require('lodash');
var User = require('../models/user');


router.get('/', (req, res) => {
	User.getUsers()
		.then((result) => {
			res.ok(result);
		})
		.catch((err) => {
			console.log(err);
			res.InternalServerError('Internal Server Error');
		});
});

router.get('/:userId', (req, res) => {
	User.getUser(req.params)
		.then((result) => {
			res.ok(result);
		})
		.catch((err) => {
			console.log(err);
			res.InternalServerError('Internal Server Error');
		});
});

router.patch('/:userId', (req, res) => {
	User.editUser(req.params, req.body)
		.then((result) => {
			res.ok(result);
		})
		.catch((err) => {
			console.log(err);
			res.InternalServerError('Internal Server Error');
		});
});

router.post('/', (req, res) => {
	User.createUser(req.body)
		.then((result) => {
			res.ok(result);
		})
		.catch((err) => {
			if (err.message.indexOf('violates unique constraint') > -1) { return res.ok({error: 'Username already exists.'}); }
			res.InternalServerError(err.message);
		});
});

router.delete('/:userId', (req, res) => {
	User.deleteUser(req.params)
		.then((result) => {
			res.ok();
		})
		.catch((err) => {
				console.log(err);
				res.InternalServerError('Internal Server Error');
		})
});

module.exports = router;
<file_sep>'use strict';

module.exports = {
	port: 3000,
	postgres: 'postgres://rpasqualone@localhost/etms2',
	secret: 'I love <NAME>'
};
<file_sep>import angular from 'angular';

export default angular.module('directives.nav', [])
	.directive('nav', () => {
		return {
			restrict: 'E',
			templateUrl: 'templates/nav.html',
			replace: true,
			controller: ($scope) => {
				$scope.elements = [1,2,3];
				
				$scope.linkClicked = (index) => {
					console.log(index);
				};
			}
		};
	});
<file_sep>import angular from 'angular';
import ngAnimate from 'angular-animate';

export default angular.module('directives.usersTale', [ngAnimate])
	.directive('dashboard', () => {
		return {
			restrict: 'E',
			templateUrl: 'templates/dashboard.html',
			replace: true,
			controller: ($scope) => {
				$scope.elements = [1,2,3];

				$scope.linkClicked = (index) => {
					console.log(index);
				};
			}
		};
	});
<file_sep>'use strict';

var config = require('../services/config');
var pg = require('pg');

module.exports = new Promise((resolve) => {
	var client = new pg.Client(config.postgres);
	client.connect((err) => {
		if (err) { throw err; }
		resolve(client);
	});
});
