Skip to content
This repository has been archived by the owner on Jun 2, 2021. It is now read-only.

Commit

Permalink
v3: clients can order apps by state
Browse files Browse the repository at this point in the history
[finishes #173475906]

Co-authored-by: Weyman Fung <[email protected]>
Co-authored-by: Merric de Launey <[email protected]>
  • Loading branch information
weymanf and MerricdeLauney committed Sep 30, 2020
1 parent 55dec6c commit 226d9c1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
11 changes: 10 additions & 1 deletion app/controllers/v3/apps_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@ def index
decorators << IncludeSpaceDecorator if IncludeSpaceDecorator.match?(message.include)
decorators << IncludeOrganizationDecorator if IncludeOrganizationDecorator.match?(message.include)

page_results = SequelPaginator.new.get_page(dataset, message.try(:pagination_options))
handle_order_by_presented_value(page_results)

render status: :ok,
json: Presenters::V3::PaginatedListPresenter.new(
presenter: Presenters::V3::AppPresenter,
paginated_result: SequelPaginator.new.get_page(dataset, message.try(:pagination_options)),
paginated_result: page_results,
path: '/v3/apps',
message: message,
decorators: decorators
Expand Down Expand Up @@ -352,6 +355,12 @@ def translate_error(e)

private

def handle_order_by_presented_value(page_results)
if page_results.try(:pagination_options).try(:order_by) == 'desired_state'
page_results.pagination_options.order_by = 'state'
end
end

def deployment_in_progress!
unprocessable!(
'Unable to assign current droplet while the app has a deployment in progress. Wait for the deployment to complete or cancel it.'
Expand Down
10 changes: 9 additions & 1 deletion app/messages/apps_list_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@ class AppsListMessage < MetadataListMessage
validates :stacks, array: true, allow_nil: true

def valid_order_by_values
super + [:name]
super + [:name, :state]
end

def self.from_params(params)
super(params, %w(names guids organization_guids space_guids stacks include))
end

def pagination_options
super.tap do |po|
if po.order_by == 'state'
po.order_by = 'desired_state'
end
end
end
end
end
25 changes: 25 additions & 0 deletions spec/request/apps_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,31 @@
expect(app_names).to eq(descending)
expect(parsed_response['pagination']['first']['href']).to include('order_by=-name')
end

it 'can order by state' do
VCAP::CloudController::AppModel.make(space: space, desired_state: 'STARTED')
VCAP::CloudController::AppModel.make(space: space, desired_state: 'STOPPED')
VCAP::CloudController::AppModel.make(space: space, desired_state: 'STARTED')
VCAP::CloudController::AppModel.make(space: space, desired_state: 'STOPPED')
ascending = ['STARTED', 'STARTED', 'STOPPED', 'STOPPED']
descending = ascending.reverse

# ASCENDING
get '/v3/apps?order_by=state', nil, user_header
expect(last_response.status).to eq(200)
parsed_response = MultiJson.load(last_response.body)
app_states = parsed_response['resources'].map { |i| i['state'] }
expect(app_states).to eq(ascending)
expect(parsed_response['pagination']['first']['href']).to include("order_by=#{CGI.escape('+')}state")

# DESCENDING
get '/v3/apps?order_by=-state', nil, user_header
expect(last_response.status).to eq(200)
parsed_response = MultiJson.load(last_response.body)
app_states = parsed_response['resources'].map { |i| i['state'] }
expect(app_states).to eq(descending)
expect(parsed_response['pagination']['first']['href']).to include('order_by=-state')
end
end

context 'labels' do
Expand Down

0 comments on commit 226d9c1

Please sign in to comment.