###
Returns a Trust by it's ID
###
exports.get_by_id = (connection, trust_id, fn) ->

	connection.query 'SELECT * FROM trusts WHERE trusts.id=? LIMIT 1;', [ 1 * trust_id ], (err, results) ->

		if err
			fn err
		else
			fn err, results[0]

###
Returns a Trusts by User
###
exports.get_by_user_id = (connection, user_id, fn) ->

	connection.query 'SELECT * FROM trusts WHERE trusts.user=? LIMIT 1;', [ 1 * user_id ], (err, results) ->

		if err
			fn err
		else
			fn err, results[0]

###
Handles validation and create or update a Organization.

Only members are allowed depending on their groups
###
exports.save = (connection, org_params, fn) ->
	fn()

###
Removes the Organization with the specified ID
###
exports.delete = (connection, org_id, fn) ->
	fn()

exports.get_trust_level_of_organization_for_user = (connection, user_id, organization, fn) ->

	connection.query 'SELECT get_user_trust_level(?, ?);', [ 1 * trust_id, 1 * organization ], (err, results) ->

		if err
			fn err
		else
			fn err, results

###
Returns the list of organizatios
###
exports.get_trusted_organizations_by_user_id = (connection, user_id, fn) ->

	# SELECT organizations.* FROM users,organizations WHERE users.id=1 and find_in_set(users.id, get_trusted_users_of_organization(organizations.id));
	# or (Still have to test which is faster)
	# SELECT organizations.* FROM organizations WHERE get_user_trust_level(1, organizations.id) >= 5;

	connection.query 'SELECT organizations.* FROM users,organizations WHERE users.id=? and find_in_set(users.id, get_trusted_users_of_organization(organizations.id));', [ 1 * user_id ], (err, results) ->

		if err
			fn err
		else
			fn err, results

###
Returns all the users that the given userid has access as one of their
organizations have the users's trust.
###
exports.get_trusted_users_by_organization_id = (connection, org_id, fn) ->

	connection.query 'SELECT users.* FROM users,organizations WHERE organizations.id = ? AND find_in_set(users.id, get_trusted_users_of_organization(organizations.id));', [ 1 * org_id ], (err, results) ->

		if err
			fn err
		else
			fn err, results

