Skip to content

Commit 7a952f0

Browse files
feat: add pagination and auth for swagger
1 parent d065a69 commit 7a952f0

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

‎app/api/api_root.rb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ class ApiRoot < Grape::API
157157
AuthenticationHelpers.add_auth_to DiscussionPromptsApi
158158
AuthenticationHelpers.add_auth_to OverseerStepsApi
159159
AuthenticationHelpers.add_auth_to TutorNotesApi
160+
AuthenticationHelpers.add_auth_to TaskPrioritizationApi
160161

161162
add_swagger_documentation \
162163
base_path: nil,

‎app/api/task_prioritization_api.rb‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,34 @@ class TaskPrioritizationApi < Grape::API
1212
desc 'Get prioritized task recommendations for a student',
1313
detail: 'Returns a ranked list of tasks across all active enrolled units based on deadline, effort, and workload scoring.'
1414

15+
params do
16+
optional :page, type: Integer, default: 1
17+
optional :per_page, type: Integer, default: 10, values: 1..50
18+
end
19+
1520
get '/tasks/recommended' do
21+
page = params[:page]
22+
per_page = params[:per_page]
23+
1624
tasks = fetch_active_tasks
1725
workload_score = calculate_workload_score(tasks)
1826

1927
results = tasks.map { |task| build_task_response(task, workload_score) }
20-
2128
sorted = results.sort_by { |t| -t[:priority_score] }
22-
present sorted
29+
30+
# Pagination after sorting
31+
offset = (page - 1) * per_page
32+
paginated = sorted.slice(offset, per_page) || []
33+
34+
{
35+
data: paginated,
36+
meta: {
37+
page: page,
38+
per_page: per_page,
39+
total_count: sorted.size,
40+
total_pages: (sorted.size / per_page.to_f).ceil
41+
}
42+
}
2343
rescue StandardError => e
2444
Rails.logger.error "TaskPrioritizationApi Error: #{e.message}"
2545
Rails.logger.error e.backtrace.join("\n")

0 commit comments

Comments
 (0)