Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/api/api_root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class ApiRoot < Grape::API
mount Courseflow::CourseMapUnitApi
mount Courseflow::SpecializationApi
mount Courseflow::RequirementSetApi
mount Courseflow::RequirementApi
mount UnitDefinitionApi

#
Expand Down
7 changes: 4 additions & 3 deletions app/api/courseflow/course_map_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ class CourseMapApi < Grape::API
params do
requires :userId, type: Integer, desc: "User ID"
end
get '/coursemap/userId/:userId' do
course_map = CourseMap.find_by(userId: params[:userId])
get '/coursemap/userId/:userId' do
course_map = CourseMap.find_by(userId: params[:userId]) || CourseMap.find_by(userId: nil)

if course_map
present course_map, with: Entities::CourseMapEntity
else
error!({ error: "Course map #{params[:userId]} not found" }, 404)
error!({ error: "Course map for user #{params[:userId]} not found" }, 404)
end
end

Expand Down
15 changes: 15 additions & 0 deletions app/api/courseflow/entities/requirement_entity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Courseflow
module Entities
class RequirementEntity < Grape::Entity
expose :id
expose :unitId
expose :courseId
expose :type
expose :category
expose :description
expose :minimum
expose :maximum
expose :requirementSetGroupId
end
end
end
90 changes: 90 additions & 0 deletions app/api/courseflow/requirement_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
require 'grape'
module Courseflow
class RequirementApi < Grape::API
format :json
helpers AuthenticationHelpers
helpers AuthorisationHelpers

before do
authenticated?
end

desc 'Get all requirements'
get '/requirement' do
present Requirement.all, with: Entities::RequirementEntity
end

desc 'Get requirements by unit ID'
params do
requires :unitId, type: Integer, desc: 'Unit ID'
end
get '/requirement/unitId/:unitId' do
present Requirement.where(unitId: params[:unitId]), with: Entities::RequirementEntity
end

desc 'Get requirements by course ID'
params do
requires :courseId, type: Integer, desc: 'Course ID'
end
get '/requirement/courseId/:courseId' do
present Requirement.where(courseId: params[:courseId]), with: Entities::RequirementEntity
end

desc 'Create a new requirement'
params do
requires :unitId, type: Integer
requires :courseId, type: Integer
requires :type, type: String
requires :category, type: String
requires :description, type: String
optional :minimum, type: Integer
optional :maximum, type: Integer
requires :requirementSetGroupId, type: Integer
end
post '/requirement' do
error!({ error: 'Not authorised' }, 403) unless authorise?(current_user, User, :handle_courseflow)
req = Requirement.new(declared(params, include_missing: false))
if req.save
status 201
present req, with: Entities::RequirementEntity
else
error!({ error: 'Failed to create requirement', details: req.errors.full_messages }, 400)
end
end

desc 'Update a requirement'
params do
requires :id, type: Integer, desc: 'Requirement ID'
optional :unitId, type: Integer
optional :courseId, type: Integer
optional :type, type: String
optional :category, type: String
optional :description, type: String
optional :minimum, type: Integer
optional :maximum, type: Integer
optional :requirementSetGroupId, type: Integer
end
put '/requirement/:id' do
error!({ error: 'Not authorised' }, 403) unless authorise?(current_user, User, :handle_courseflow)
req = Requirement.find_by(id: params[:id])
error!({ error: 'Requirement not found' }, 404) unless req
if req.update(declared(params, include_missing: false).except(:id))
present req, with: Entities::RequirementEntity
else
error!({ error: 'Failed to update requirement', details: req.errors.full_messages }, 400)
end
end

desc 'Delete a requirement'
params do
requires :id, type: Integer, desc: 'Requirement ID'
end
delete '/requirement/:id' do
error!({ error: 'Not authorised' }, 403) unless authorise?(current_user, User, :handle_courseflow)
req = Requirement.find_by(id: params[:id])
error!({ error: 'Requirement not found' }, 404) unless req
req.destroy
status 204
end
end
end
6 changes: 5 additions & 1 deletion app/models/courseflow/course_map.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
module Courseflow
class CourseMap < ApplicationRecord
# Validation rules for attributes in the course map model
validates :userId, presence: true
validates :userId, presence: true, unless: :template?
validates :courseId, presence: true

def template?
userId.nil?
end
end
end
11 changes: 11 additions & 0 deletions app/models/courseflow/requirement.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Courseflow
class Requirement < ApplicationRecord
self.inheritance_column = :_type_disabled

validates :unitId, presence: true
validates :courseId, presence: true
validates :category, presence: true
validates :description, presence: true
validates :requirementSetGroupId, presence: true
end
end
16 changes: 16 additions & 0 deletions db/migrate/20250515025346_create_requirement.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CreateRequirement < ActiveRecord::Migration[7.1]
def change
create_table :requirements do |t|
t.integer :unitId
t.integer :courseId
t.string :type
t.string :category
t.string :description
t.integer :minimum
t.integer :maximum
t.integer :requirementSetGroupId

t.timestamps
end
end
end
Loading