moved some logic from controllers to api client.