Commit 6b507219 authored by Stephan van Leeuwen's avatar Stephan van Leeuwen

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
parent 75043a9e
...@@ -11,6 +11,8 @@ GET /projects ...@@ -11,6 +11,8 @@ GET /projects
Parameters: Parameters:
- `archived` (optional) - if passed, limit by archived status - `archived` (optional) - if passed, limit by archived status
- `order_by` (optional) - Return requests ordered by `id`, `name`, `created_at` or `last_activity_at` fields
- `sort` (optional) - Return requests sorted in `asc` or `desc` order
```json ```json
[ [
...@@ -628,6 +630,8 @@ GET /projects/search/:query ...@@ -628,6 +630,8 @@ GET /projects/search/:query
Parameters: Parameters:
- query (required) - A string contained in the project name - `query` (required) - A string contained in the project name
- per_page (optional) - number of projects to return per page - `per_page` (optional) - number of projects to return per page
- page (optional) - the page to retrieve - `page` (optional) - the page to retrieve
- `order_by` (optional) - Return requests ordered by `id`, `name`, `created_at` or `last_activity_at` fields
- `sort` (optional) - Return requests sorted in `asc` or `desc` order
...@@ -23,6 +23,19 @@ module API ...@@ -23,6 +23,19 @@ module API
get do get do
@projects = current_user.authorized_projects @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 the archived parameter is passed, limit results accordingly
if params[:archived].present? if params[:archived].present?
@projects = @projects.where(archived: parse_boolean(params[:archived])) @projects = @projects.where(archived: parse_boolean(params[:archived]))
...@@ -37,7 +50,21 @@ module API ...@@ -37,7 +50,21 @@ module API
# Example Request: # Example Request:
# GET /projects/owned # GET /projects/owned
get '/owned' do 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 present @projects, with: Entities::Project
end end
...@@ -47,7 +74,21 @@ module API ...@@ -47,7 +74,21 @@ module API
# GET /projects/all # GET /projects/all
get '/all' do get '/all' do
authenticated_as_admin! 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 present @projects, with: Entities::Project
end end
...@@ -227,6 +268,20 @@ module API ...@@ -227,6 +268,20 @@ module API
ids = current_user.authorized_projects.map(&:id) ids = current_user.authorized_projects.map(&:id)
visibility_levels = [ Gitlab::VisibilityLevel::INTERNAL, Gitlab::VisibilityLevel::PUBLIC ] 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]}%") 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 present paginate(projects), with: Entities::Project
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment