Move SQL in views to controller