add state filtering to account courses api. refs #9485

filter courses returned by the account courses endpoint by workflow
state, allowing users to return courses in all states, or in any
combination of states.

test plan:
  * make a call to accounts/:id/courses, passing some accepted value
    for state[]; verify that only courses in that state are returned.
  * make a call without a state[] param; verify that all courses
    except deleted ones are returned.

Change-Id: I9c85e3e237ebeb8addb58e6da39037848f8958f5
Reviewed-on: https://gerrit.instructure.com/12269
Tested-by: Jenkins <jenkins@instructure.com>
Reviewed-by: Jacob Fugal <jacob@instructure.com>
This commit is contained in:
Zach Pendleton 2012-07-16 13:54:22 -06:00
parent 5b8524b14a
commit 5dc3a17133
2 changed files with 26 additions and 7 deletions

View File

@ -63,20 +63,24 @@ class AccountsController < ApplicationController
include Api::V1::Course
# @API List active courses in an account
# Retrieve the list of active (non-deleted) courses in this account.
# Retrieve the list of courses in this account.
#
# @argument hide_enrollmentless_courses [optional] If set, only return courses that have at least one enrollment.
# @argument state[] [optional] If set, only return courses that are in the given state[s]. Valid states are "created," "claimed," "available," "completed," and "deleted." By default, all states but "deleted" are returned.
#
# @example_response
# [ { 'id': 1, 'name': 'first course', 'course_code': 'first', 'sis_course_id': 'first-sis' },
# { 'id': 2, 'name': 'second course', 'course_code': 'second', 'sis_course_id': null } ]
def courses_api
if authorized_action(@account, @current_user, :read)
@courses = @account.associated_courses.active
@courses = @courses.with_enrollments if params[:hide_enrollmentless_courses]
@courses = Api.paginate(@courses, self, api_v1_account_courses_path, :order => :id)
render :json => @courses.map { |c| course_json(c, @current_user, session, [], nil) }
end
return unless authorized_action(@account, @current_user, :read)
params[:state] ||= %w{created claimed available completed}
@courses = @account.associated_courses.scoped(:conditions => { :workflow_state => params[:state] })
@courses = @courses.with_enrollments if params[:hide_enrollmentless_courses]
@courses = Api.paginate(@courses, self, api_v1_account_courses_path, :order => :id)
render :json => @courses.map { |c| course_json(c, @current_user, session, [], nil) }
end
def update

View File

@ -151,6 +151,21 @@ describe "Accounts API", :type => :integration do
end
end
it "should return courses filtered by state[]" do
@me = @user
[:c1, :c2].each do |course|
instance_variable_set("@#{course}".to_sym, course_model(:name => course.to_s, :account => @a1))
end
@c2.destroy
@user = @me
json = api_call(:get, "/api/v1/accounts/#{@a1.id}/courses?state[]=deleted",
{ :controller => 'accounts', :action => 'courses_api', :account_id => @a1.to_param, :format => 'json', :state => %w[deleted] })
json.length.should eql 1
json.first['name'].should eql 'c2'
end
it "should limit the maximum per-page returned" do
@me = @user
15.times { |i| course_model(:name => "c#{i}", :account => @a1, :root_account => @a1) }