diff options
author | Stephan van Leeuwen <stephan.vanleeuwen5@gmail.com> | 2014-12-19 15:27:27 +0300 |
---|---|---|
committer | Stephan van Leeuwen <stephan.vanleeuwen5@gmail.com> | 2014-12-29 18:36:56 +0300 |
commit | 6b507219465e50ceff726535f92b75fa9567906d (patch) | |
tree | ec46429a213278e3f0a5f6c1ce25f637f74ae8a6 /lib/api/projects.rb | |
parent | 75043a9eba557d1fe31d81dc30bd979af321e907 (diff) |
Updated projects api to allow ordering
Added support for order_by and sort parameters, to sort the projects by the specified values.
Updated projects api documentation including the order_by and sort parameters
Diffstat (limited to 'lib/api/projects.rb')
-rw-r--r-- | lib/api/projects.rb | 59 |
1 files changed, 57 insertions, 2 deletions
diff --git a/lib/api/projects.rb b/lib/api/projects.rb index 7fcf97d1ad6..2b6ec5e1b94 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -23,6 +23,19 @@ module API get do @projects = current_user.authorized_projects + sort = case params["sort"] + when 'desc' then 'DESC' + else 'ASC' + end + + @projects = case params["order_by"] + when 'id' then @projects.reorder("id #{sort}") + when 'name' then @projects.reorder("name #{sort}") + when 'created_at' then @projects.reorder("created_at #{sort}") + when 'last_activity_at' then @projects.reorder("last_activity_at #{sort}") + else @projects + end + # If the archived parameter is passed, limit results accordingly if params[:archived].present? @projects = @projects.where(archived: parse_boolean(params[:archived])) @@ -37,7 +50,21 @@ module API # Example Request: # GET /projects/owned get '/owned' do - @projects = paginate current_user.owned_projects + sort = case params["sort"] + when 'desc' then 'DESC' + else 'ASC' + end + + @projects = current_user.owned_projects + @projects = case params["order_by"] + when 'id' then @projects.reorder("id #{sort}") + when 'name' then @projects.reorder("name #{sort}") + when 'created_at' then @projects.reorder("created_at #{sort}") + when 'last_activity_at' then @projects.reorder("last_activity_at #{sort}") + else @projects + end + + @projects = paginate @projects present @projects, with: Entities::Project end @@ -47,7 +74,21 @@ module API # GET /projects/all get '/all' do authenticated_as_admin! - @projects = paginate Project + + sort = case params["sort"] + when 'desc' then 'DESC' + else 'ASC' + end + + @projects = case params["order_by"] + when 'id' then Project.order("id #{sort}") + when 'name' then Project.order("name #{sort}") + when 'created_at' then Project.order("created_at #{sort}") + when 'last_activity_at' then Project.order("last_activity_at #{sort}") + else Project + end + + @projects = paginate @projects present @projects, with: Entities::Project end @@ -227,6 +268,20 @@ module API ids = current_user.authorized_projects.map(&:id) visibility_levels = [ Gitlab::VisibilityLevel::INTERNAL, Gitlab::VisibilityLevel::PUBLIC ] projects = Project.where("(id in (?) OR visibility_level in (?)) AND (name LIKE (?))", ids, visibility_levels, "%#{params[:query]}%") + + sort = case params["sort"] + when 'desc' then 'DESC' + else 'ASC' + end + + projects = case params["order_by"] + when 'id' then projects.order("id #{sort}") + when 'name' then projects.order("name #{sort}") + when 'created_at' then projects.order("created_at #{sort}") + when 'last_activity_at' then projects.order("last_activity_at #{sort}") + else projects + end + present paginate(projects), with: Entities::Project end |