Skip to content

Commit

Permalink
Added backend to retrive data based on page number and also to return…
Browse files Browse the repository at this point in the history
… the total amount of histories for the page numbers
  • Loading branch information
Jude Roz committed Dec 22, 2024
1 parent 5b972fc commit ebeff75
Showing 1 changed file with 62 additions and 7 deletions.
69 changes: 62 additions & 7 deletions app/api/projects_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,69 @@ class ProjectsApi < Grape::API
present projects, with: Entities::ProjectEntity, for_student: true, summary_only: true, user: current_user
end

desc 'Get project with target grade history'
desc 'Get project with target grade history (unchanged route)'
params do
requires :id, type: Integer, desc: 'The id of the project to get'
end
get '/projects/:id' do
project = Project.includes(:unit, :user, target_grade_histories: :changed_by).find(params[:id])

if authorise? current_user, project, :get
present project, with: Entities::ProjectEntity, user: current_user, for_student: true, in_project: true
present project, with: Entities::ProjectEntity,
user: current_user,
for_student: true,
in_project: true
else
error!({ error: "Couldn't find Project with id=#{params[:id]}" }, 403)
end
end


desc 'Get paginated target grade histories for a project (manual pagination)'
params do
requires :id, type: Integer, desc: 'The id of the project'
optional :page, type: Integer, desc: 'Page number', default: 1
optional :limit, type: Integer, desc: 'Records per page', default: 10
end
get '/projects/:id/target_grade_histories' do
project = Project.find(params[:id])
unless authorise?(current_user, project, :get)
error!({ error: "You are not authorised to view this project" }, 403)
end

page = params[:page]
limit = params[:limit]
offset = (page - 1) * limit

# Sort by changed_at DESC in DB, then limit/offset for pagination
total_histories = project.target_grade_histories.count
paged_histories = project.target_grade_histories
.order(changed_at: :desc)
.limit(limit)
.offset(offset)
.includes(:changed_by)

present({
target_grade_histories: paged_histories.map do |h|
{
id: h.id,
previous_grade: h.previous_grade,
new_grade: h.new_grade,
changed_at: h.changed_at,
changed_by: {
id: h.changed_by_id,
email: h.changed_by&.email,
first_name: h.changed_by&.first_name,
last_name: h.changed_by&.last_name,
username: h.changed_by&.username,
nickname: h.changed_by&.nickname
}
}
end,
total_histories: total_histories
})
end

desc 'Update a project'
params do
optional :trigger, type: String, desc: 'The update trigger'
Expand Down Expand Up @@ -67,7 +116,7 @@ class ProjectsApi < Grape::API
error!({ error: "You cannot change the campus for this student" }, 403)
end
for_student = false
project.campus_id = params[:campus_id] == -1 ? nil : params[:campus_id]
project.campus_id = (params[:campus_id] == -1) ? nil : params[:campus_id]
project.save!
elsif !params[:enrolled].nil?
unless authorise? current_user, project.unit, :change_project_enrolment
Expand Down Expand Up @@ -128,7 +177,6 @@ class ProjectsApi < Grape::API
end

for_student = false

project.grade = params[:grade]
project.grade_rationale = params[:grade_rationale]
project.save!
Expand All @@ -140,13 +188,20 @@ class ProjectsApi < Grape::API
error!({ error: "You do not have permissions to change this student" }, 403)
end

# if someone changes this setting manually, clear the autogenerated status
project.portfolio_auto_generated = false
project.compile_portfolio = params[:compile_portfolio]
project.save
end

Entities::ProjectEntity.represent(project, only: [:campus_id, :enrolled, :target_grade, :submitted_grade, :compile_portfolio, :portfolio_available, :uses_draft_learning_summary, :stats], for_student: for_student)
Entities::ProjectEntity.represent(
project,
only: [
:campus_id, :enrolled, :target_grade, :submitted_grade,
:compile_portfolio, :portfolio_available, :uses_draft_learning_summary,
:stats
],
for_student: for_student
)
end # put

desc 'Enrol a student in a unit, creating them a project'
Expand Down Expand Up @@ -199,4 +254,4 @@ class ProjectsApi < Grape::API
error!({ error: "Couldn't find Unit with id=#{params[:unit_id]}" }, 403)
end
end
end
end

0 comments on commit ebeff75

Please sign in to comment.