diff --git a/app/api/api_root.rb b/app/api/api_root.rb index e2860f8e4..2d7449711 100644 --- a/app/api/api_root.rb +++ b/app/api/api_root.rb @@ -56,12 +56,12 @@ class ApiRoot < Grape::API mount BreaksApi mount DiscussionCommentApi mount ExtensionCommentsApi + mount FeedbackApi::StageApi mount GroupSetsApi mount LearningOutcomesApi mount LearningAlignmentApi mount ProjectsApi mount SettingsApi - mount StageApi mount StudentsApi mount Submission::PortfolioApi mount Submission::PortfolioEvidenceApi @@ -96,7 +96,7 @@ class ApiRoot < Grape::API AuthenticationHelpers.add_auth_to LearningAlignmentApi AuthenticationHelpers.add_auth_to ProjectsApi AuthenticationHelpers.add_auth_to StudentsApi - AuthenticationHelpers.add_auth_to StageApi + AuthenticationHelpers.add_auth_to FeedbackApi::StageApi AuthenticationHelpers.add_auth_to Submission::PortfolioApi AuthenticationHelpers.add_auth_to Submission::PortfolioEvidenceApi AuthenticationHelpers.add_auth_to Submission::BatchTaskApi diff --git a/app/api/feedback_api/stage_api.rb b/app/api/feedback_api/stage_api.rb new file mode 100644 index 000000000..f2de44906 --- /dev/null +++ b/app/api/feedback_api/stage_api.rb @@ -0,0 +1,77 @@ +require 'grape' + +module FeedbackApi + class StageApi < Grape::API + + desc 'Feedback is provided in stages. This endpoint allows you to create a new stage for feedback on tasks for a given task definition.' + params do + requires :task_definition_id, type: Integer, desc: 'The task definition to which the stage belongs' + requires :title, type: String, desc: 'The title of the new stage' + requires :order, type: Integer, desc: 'The order to determine the order in which to display stages' + end + post '/stages' do + task_definition = TaskDefinition.find(params[:task_definition_id]) + + unless authorise? current_user, task_definition.unit, :update + error!({ error: 'Not authorised to create a stage for this unit' }, 403) + end + + stage_parameters = ActionController::Parameters.new(params) + .permit(:title, :order) + + stage_parameters[:task_definition] = task_definition + + result = Stage.create!(stage_parameters) + + present result, with: Entities::StageEntity + end + + desc 'This endpoint allows you to get all the stages for a given task definition.' + params do + requires :task_definition_id, type: Integer, desc: 'The task definition to which the stage belongs' + end + get '/stages' do + task_definition = TaskDefinition.find(params[:task_definition_id]) + + unless authorise? current_user, task_definition.unit, :provide_feedback + error!({ error: 'Not authorised to get feedback stages for this unit' }, 403) + end + + present task_definition.stages, with: Entities::StageEntity + end + + desc 'This endpoint allows you to update the name and order of a stage.' + params do + optional :title, type: String, desc: 'The new title for the stage' + optional :order, type: Integer, desc: 'The order value for the stage' + end + put '/stages/:id' do + # Get the stage from the task definition + stage = Stage.find(params[:id]) + + unless authorise? current_user, stage.unit, :update + error!({ error: 'Not authorised to update feedback stages for this unit' }, 403) + end + + stage_params = ActionController::Parameters.new(params) + .permit(:title, :order) + + stage.update!(stage_params) + + present stage, with: Entities::StageEntity + end + + desc 'This endpoint allows you to delete a stage.' + delete '/stages/:id' do + # Get the stage from the task definition + stage = Stage.find(params[:id]) + + unless authorise? current_user, stage.unit, :update + error!({ error: 'Not authorised to delete feedback stages for this unit' }, 403) + end + + stage.destroy! + end + + end +end diff --git a/app/api/stage_api.rb b/app/api/stage_api.rb deleted file mode 100644 index 95c7f3478..000000000 --- a/app/api/stage_api.rb +++ /dev/null @@ -1,32 +0,0 @@ -require 'grape' - -class StageApi < Grape::API - - desc 'Create a new stage' - params do - requires :task_definition_id, type: Integer, desc: 'The task definition to which the stage belongs' - requires :title, type: String, desc: 'The title of the new stage' - requires :order, type: Integer, desc: 'The order to determine the order in which to display stages' - end - post '/stages' do - task_definition = TaskDefinition.find(params[:task_definition_id]) - - unless authorise? current_user, task_definition.unit, :update - error!({ error: 'Not authorised to create a stage for this unit' }, 403) - end - - stage_parameters = ActionController::Parameters.new(params) - .permit(:title, :order) - - stage_parameters[:task_definition] = task_definition - - result = Stage.create!(stage_parameters) - - present result, with: Entities::StageEntity - end - - get '/stages' do - present Stage.all, with: Entities::StageEntity - end - -end diff --git a/app/models/stage.rb b/app/models/feedback/stage.rb similarity index 72% rename from app/models/stage.rb rename to app/models/feedback/stage.rb index 34da7234b..998f8a362 100644 --- a/app/models/stage.rb +++ b/app/models/feedback/stage.rb @@ -1,5 +1,6 @@ class Stage < ApplicationRecord belongs_to :task_definition + has_one :unit, through: :task_definition validates :title, :order, presence: true diff --git a/config/application.rb b/config/application.rb index ab2853098..d94b537e3 100644 --- a/config/application.rb +++ b/config/application.rb @@ -153,7 +153,7 @@ class Application < Rails::Application # config.paths.add 'app/api', glob: '**/*.rb' # config.autoload_paths += Dir["#{Rails.root}/app"] # config.autoload_paths += Dir[Rails.root.join("app", "models", "{*/}")] - config.eager_load_paths << Rails.root.join('app') << Rails.root.join('app', 'models', 'comments') + config.eager_load_paths << Rails.root.join('app') << Rails.root.join('app', 'models', 'comments') << Rails.root.join('app', 'models', 'feedback') # CORS config config.middleware.insert_before Warden::Manager, Rack::Cors do