(function() {
	window.app.factory("userSvc",
	[
		"$http",
		function($http) {
			var users = [];

			loadUsers();

			var svc = {
				create: create,
				edit: edit,
				users: users,
				exportusers: exportusers
			};

			return svc;

			function loadUsers() {
				$http.post("/User/All")
					.success(function(data) {
						users.addRange(data);
					});
			}

			function exportusers() {
				return $http.post("/User/Export", {}, { responseType: "arraybuffer" });
			}

			function create(user) {
				return $http.post("/User/Create", user)
					.success(function(user) {
						users.unshift(user);
					});
			}

			function edit(existingUser, editedUser) {
				return $http.post("/User/Edit", editedUser)
					.success(function(data) {
						angular.copy(data, existingUser);
					});
			}
		}
	]);
})();