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

Commit 226d9c1

Browse files
v3: clients can order apps by state
[finishes #173475906] Co-authored-by: Weyman Fung <wfung@pivotal.io> Co-authored-by: Merric de Launey <mdelauney@pivotal.io>
1 parent 55dec6c commit 226d9c1

3 files changed

Lines changed: 44 additions & 2 deletions

File tree

app/controllers/v3/apps_controller.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,13 @@ def index
4949
decorators << IncludeSpaceDecorator if IncludeSpaceDecorator.match?(message.include)
5050
decorators << IncludeOrganizationDecorator if IncludeOrganizationDecorator.match?(message.include)
5151

52+
page_results = SequelPaginator.new.get_page(dataset, message.try(:pagination_options))
53+
handle_order_by_presented_value(page_results)
54+
5255
render status: :ok,
5356
json: Presenters::V3::PaginatedListPresenter.new(
5457
presenter: Presenters::V3::AppPresenter,
55-
paginated_result: SequelPaginator.new.get_page(dataset, message.try(:pagination_options)),
58+
paginated_result: page_results,
5659
path: '/v3/apps',
5760
message: message,
5861
decorators: decorators
@@ -352,6 +355,12 @@ def translate_error(e)
352355

353356
private
354357

358+
def handle_order_by_presented_value(page_results)
359+
if page_results.try(:pagination_options).try(:order_by) == 'desired_state'
360+
page_results.pagination_options.order_by = 'state'
361+
end
362+
end
363+
355364
def deployment_in_progress!
356365
unprocessable!(
357366
'Unable to assign current droplet while the app has a deployment in progress. Wait for the deployment to complete or cancel it.'

app/messages/apps_list_message.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,19 @@ class AppsListMessage < MetadataListMessage
2323
validates :stacks, array: true, allow_nil: true
2424

2525
def valid_order_by_values
26-
super + [:name]
26+
super + [:name, :state]
2727
end
2828

2929
def self.from_params(params)
3030
super(params, %w(names guids organization_guids space_guids stacks include))
3131
end
32+
33+
def pagination_options
34+
super.tap do |po|
35+
if po.order_by == 'state'
36+
po.order_by = 'desired_state'
37+
end
38+
end
39+
end
3240
end
3341
end

spec/request/apps_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,31 @@
780780
expect(app_names).to eq(descending)
781781
expect(parsed_response['pagination']['first']['href']).to include('order_by=-name')
782782
end
783+
784+
it 'can order by state' do
785+
VCAP::CloudController::AppModel.make(space: space, desired_state: 'STARTED')
786+
VCAP::CloudController::AppModel.make(space: space, desired_state: 'STOPPED')
787+
VCAP::CloudController::AppModel.make(space: space, desired_state: 'STARTED')
788+
VCAP::CloudController::AppModel.make(space: space, desired_state: 'STOPPED')
789+
ascending = ['STARTED', 'STARTED', 'STOPPED', 'STOPPED']
790+
descending = ascending.reverse
791+
792+
# ASCENDING
793+
get '/v3/apps?order_by=state', nil, user_header
794+
expect(last_response.status).to eq(200)
795+
parsed_response = MultiJson.load(last_response.body)
796+
app_states = parsed_response['resources'].map { |i| i['state'] }
797+
expect(app_states).to eq(ascending)
798+
expect(parsed_response['pagination']['first']['href']).to include("order_by=#{CGI.escape('+')}state")
799+
800+
# DESCENDING
801+
get '/v3/apps?order_by=-state', nil, user_header
802+
expect(last_response.status).to eq(200)
803+
parsed_response = MultiJson.load(last_response.body)
804+
app_states = parsed_response['resources'].map { |i| i['state'] }
805+
expect(app_states).to eq(descending)
806+
expect(parsed_response['pagination']['first']['href']).to include('order_by=-state')
807+
end
783808
end
784809

785810
context 'labels' do

0 commit comments

Comments
 (0)