Commit 71ae8570 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'project-ordering' into 'master'

API: Project ordering

Added support for ordering and sorting projects by id, name, created_at or last_activity_at fields.

When the order_by parameter is passed the projects will be ordered by the appropriate field, when the parameter is not passed the original ordering will apply.

the sort parameter will default to ASC, and will only be DESC if the sort parameter is desc

See merge request !266
parents 75043a9e 23e83a6a
...@@ -19,7 +19,7 @@ v 7.7.0 ...@@ -19,7 +19,7 @@ v 7.7.0
- -
- Add alert message in case of outdated browser (IE < 10) - Add alert message in case of outdated browser (IE < 10)
- -
- - Added API support for sorting projects
v 7.6.0 v 7.6.0
- Fork repository to groups - Fork repository to groups
......
...@@ -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
...@@ -22,6 +22,15 @@ module API ...@@ -22,6 +22,15 @@ module API
# GET /projects # GET /projects
get do get do
@projects = current_user.authorized_projects @projects = current_user.authorized_projects
sort = params[:sort] == 'desc' ? 'desc' : 'asc'
@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?
...@@ -37,7 +46,17 @@ module API ...@@ -37,7 +46,17 @@ 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 = params[:sort] == 'desc' ? 'desc' : 'asc'
@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 +66,17 @@ module API ...@@ -47,7 +66,17 @@ module API
# GET /projects/all # GET /projects/all
get '/all' do get '/all' do
authenticated_as_admin! authenticated_as_admin!
@projects = paginate Project sort = params[:sort] == 'desc' ? 'desc' : 'asc'
@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 +256,16 @@ module API ...@@ -227,6 +256,16 @@ 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 = params[:sort] == 'desc' ? 'desc' : 'asc'
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