Skip to content

Commit

Permalink
Implemented add_user in courses controller
Browse files Browse the repository at this point in the history
  • Loading branch information
cynthia-lixinyi committed Apr 24, 2024
1 parent 5f0f654 commit 004fdb1
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
33 changes: 32 additions & 1 deletion app/controllers/api/v1/courses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,38 @@ def destroy
end

def add_user
render :json => 'The add_user method of CoursesControler us not yet implemented'.to_json, status: 501
user_id = params[:user_id]
course_id = params[:course_id]
role = params[:role]

# Check if the provided course_id is valid i.e. exists in courses table
if !Course.find_by(id: course_id)
render json: { error: "The course does not exist." }, status: :not_found
return
end

# Check if the provided user is valid i.e. exists in users table
if !User.find_by(id: user_id)
render json: { error: "The user does not exist." }, status: :not_found
return
end

# Check if the user has been already added to the course
existing_user_to_course = UserToCourse.find_by(course_id: course_id, user_id: user_id)
if existing_user_to_course
render json: { error: "The user is already added to the course."}, status: :unprocessable_entity
return
end

# Add the user to the course with the desired role
new_user_to_course = UserToCourse.new(course_id: course_id, user_id: user_id, role: role)
if new_user_to_course.save
flash[:success] = "User added to the course successfully."
render json: new_user_to_course, status: :created
else
flash[:error] = "Failed to add the user the to course."
render json: new_user_to_course.errors, status: :unprocessable_entity
end
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions app/models/user_to_course.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class UserToCourse < ApplicationRecord
# Associations
belongs_to :user
belongs_to :course

# Validations
validates :user_id, presence: true
validates :course_id, presence: true
validates :role, presence: true
end
33 changes: 30 additions & 3 deletions spec/controllers/api/v1/courses_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,36 @@ module V1
end

describe 'add_user' do
it 'throws a 501 error' do
put :add_user, params: { course_id: 16, user_id: 16 }
expect(response.status).to eq(501)
let(:test_course) { Course.create(course_name: "Test Course") }
let(:test_user) { User.create(email: "[email protected]") }

context "Provided parameters are valid" do
it "adds an existing user to an existing course" do
post :add_user, params: { course_id: test_course.id, user_id: test_user.id, role: "ta" }
expect(response).to have_http_status(:created)
expect(flash["success"]).to eq("User added to the course successfully.")
end
end

context "Provided parameter are invalid" do
it "returns an error if course is not existed in the courses table" do
post :add_user, params: { course_id: 123456, user_id: test_user.id, role: "ta" }
expect(response).to have_http_status(:not_found)
expect(JSON.parse(response.body)["error"]).to eq("The course does not exist.")
end

it "returns an error if user is not existed in the users table" do
post :add_user, params: { course_id: test_course.id, user_id: 123456, role: "ta" }
expect(response).to have_http_status(:not_found)
expect(JSON.parse(response.body)["error"]).to eq("The user does not exist.")
end

it "returns an error if the user is already associated with the course" do
post :add_user, params: { course_id: test_course.id, user_id: test_user.id, role: "student" }
post :add_user, params: { course_id: test_course.id, user_id: test_user.id, role: "student" }
expect(response).to have_http_status(:unprocessable_entity)
expect(JSON.parse(response.body)["error"]).to eq("The user is already added to the course.")
end
end
end
end
Expand Down

0 comments on commit 004fdb1

Please sign in to comment.