diff --git a/app/models/course.rb b/app/models/course.rb index 9e70ccf7..a7948ce5 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -4,7 +4,7 @@ class Course < ApplicationRecord validates :name, presence: true validates :directory_path, presence: true has_many :ta_mappings, dependent: :destroy - has_many :tas, through: :ta_mappings + has_many :tas, through: :ta_mappings, source: :ta # Returns the submission directory for the course def path @@ -16,13 +16,13 @@ def path def add_ta(user) if user.nil? return { success: false, message: "The user with id #{user.id} does not exist" } - elsif TaMapping.exists?(ta_id: user.id, course_id: id) + elsif TaMapping.exists?(user_id: user.id, course_id: id) return { success: false, message: "The user with id #{user.id} is already a TA for this course." } else - ta_mapping = TaMapping.create(ta_id: user.id, course_id: id) + ta_mapping = TaMapping.create(user_id: user.id, course_id: id) user.update(role: Role::TEACHING_ASSISTANT) if ta_mapping.save - return { success: true, data: ta_mapping.slice(:course_id, :ta_id) } + return { success: true, data: ta_mapping.slice(:course_id, :user_id) } else return { success: false, message: ta_mapping.errors } end @@ -30,11 +30,11 @@ def add_ta(user) end # Removes Teaching Assistant from the course - def remove_ta(ta_id) - ta_mapping = ta_mappings.find_by(ta_id: ta_id, course_id: :id) + def remove_ta(user_id) + ta_mapping = ta_mappings.find_by(user_id: user_id, course_id: :id) return { success: false, message: "No TA mapping found for the specified course and TA" } if ta_mapping.nil? - ta = User.find(ta_mapping.ta_id) - ta_count = TaMapping.where(ta_id: ta_id).size - 1 + ta = User.find(ta_mapping.user_id) + ta_count = TaMapping.where(user_id: user_id).size - 1 if ta_count.zero? ta.update(role: Role::STUDENT) end diff --git a/app/models/ta_mapping.rb b/app/models/ta_mapping.rb index 8a2ed80d..9979a984 100644 --- a/app/models/ta_mapping.rb +++ b/app/models/ta_mapping.rb @@ -1,10 +1,10 @@ class TaMapping < ApplicationRecord belongs_to :course - belongs_to :ta + belongs_to :ta, class_name: 'User', foreign_key: 'user_id' #Returns course ids of the TA def self.get_course_ids(user_id) - TaMapping.find_by(ta_id: user_id).course_id + TaMapping.find_by(user_id: user_id).course_id end #Returns courses of the TA diff --git a/spec/requests/api/v1/courses_spec.rb b/spec/requests/api/v1/courses_spec.rb index 64534d1a..6c414f81 100644 --- a/spec/requests/api/v1/courses_spec.rb +++ b/spec/requests/api/v1/courses_spec.rb @@ -11,41 +11,39 @@ @student = Role.find_or_create_by(name: 'Student', parent_id: @ta.id) end - let(:prof) { User.create( - name: "profa", - password_digest: "password", - role_id: @instructor.id, - full_name: "Prof A", - email: "testuser@example.com", - mru_directory_path: "/home/testuser", - ) } + let(:prof) { + User.create( + name: "profa", + password_digest: "password", + role_id: @instructor.id, + full_name: "Prof A", + email: "testuser@example.com", + mru_directory_path: "/home/testuser", + ) + } - let(:token) { JsonWebToken.encode({id: prof.id}) } + let(:token) { JsonWebToken.encode({ id: prof.id }) } let(:Authorization) { "Bearer #{token}" } - # GET /courses/{id}/add_ta/{ta_id} path '/api/v1/courses/{id}/add_ta/{ta_id}' do parameter name: :id, in: :path, type: :integer, required: true parameter name: :ta_id, in: :path, type: :integer, required: true let(:institution) { Institution.create(name: "NC State") } - let(:prof) { User.create( - name: "profa", - password_digest: "password", - role_id: 4, - fullname: "Prof A", - email: "testuser@example.com", - mru_directory_path: "/home/testuser", - ) } - let(:ta) { User.create( - name: "taa", - password_digest: "password", - role_id: 3, - fullname: "TA A", - email: "testuser@example.com", - mru_directory_path: "/home/testuser", - ) } - let(:course) { Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD', info: 'blank') } + let(:ta) { + User.create( + name: "taa", + password_digest: "password", + role_id: @ta.id, + full_name: "TA A", + email: "testuser@example.com", + mru_directory_path: "/home/testuser", + ) + } + let(:course) { + Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD', + info: 'blank') + } let(:id) { course.id } let(:ta_id) { ta.id } get('add_ta course') do @@ -82,7 +80,10 @@ path '/api/v1/courses/{id}/tas' do parameter name: 'id', in: :path, type: :string, description: 'id' let(:institution) { Institution.create(name: "NC State") } - let(:course) { Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD', info: 'blank') } + let(:course) { + Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD', + info: 'blank') + } let(:id) { course.id } get('view_tas course') do tags 'Courses' @@ -104,16 +105,21 @@ parameter name: 'id', in: :path, type: :string, description: 'id' parameter name: 'ta_id', in: :path, type: :string, description: 'ta_id' let(:institution) { Institution.create(name: "NC State") } - let(:course) { Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD', info: 'blank') } + let(:course) { + Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD', + info: 'blank') + } let(:id) { course.id } - let(:ta) { User.create( - name: "taa", - password_digest: "password", - role_id: 3, - fullname: "TA A", - email: "testuser@example.com", - mru_directory_path: "/home/testuser", - ) } + let(:ta) { + User.create( + name: "taa", + password_digest: "password", + role_id: @ta.id, + full_name: "TA A", + email: "testuser@example.com", + mru_directory_path: "/home/testuser", + ) + } let(:ta_id) { ta.id } let(:ta_mapping) { TaMapping.create(course_id: course.id, ta_id: ta.id) } get('remove_ta course') do @@ -140,7 +146,10 @@ path '/api/v1/courses/{id}/copy' do parameter name: 'id', in: :path, type: :string, description: 'id' let(:institution) { Institution.create(name: "NC State") } - let(:course) { Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD', info: 'blank') } + let(:course) { + Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD', + info: 'blank') + } let(:id) { course.id } get('copy course') do tags 'Courses' @@ -190,7 +199,10 @@ } response(201, 'successful') do let(:institution) { Institution.create(name: "NC State") } - let(:course) { { institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD', info: 'blank' } } + let(:course) { + { institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD', + info: 'blank' } + } after do |example| example.metadata[:response][:content] = { 'application/json' => { @@ -207,7 +219,10 @@ path '/api/v1/courses/{id}' do parameter name: 'id', in: :path, type: :string, description: 'id' let(:institution) { Institution.create(name: "NC State") } - let(:course) { Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD', info: 'blank') } + let(:course) { + Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD', + info: 'blank') + } let(:id) { course.id } get('show course') do tags 'Courses' @@ -239,7 +254,10 @@ required: %w[] } let(:institution) { Institution.create(name: "NC State") } - let(:course) { Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD', info: 'blank') } + let(:course) { + Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD', + info: 'blank') + } let(:id) { course.id } response(200, 'successful') do after do |example| @@ -269,7 +287,10 @@ required: %w[] } let(:institution) { Institution.create(name: "NC State") } - let(:course) { Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD', info: 'blank') } + let(:course) { + Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD', + info: 'blank') + } let(:id) { course.id } response(200, 'successful') do after do |example| @@ -287,7 +308,10 @@ delete('delete course') do tags 'Courses' let(:institution) { Institution.create(name: "NC State") } - let(:course) { Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD', info: 'blank') } + let(:course) { + Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD', + info: 'blank') + } let(:id) { course.id } response(204, 'successful') do after do |example|