Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented add_user in courses controller #44

Merged
Merged
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
57 changes: 49 additions & 8 deletions app/controllers/api/v1/courses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ def create
end

new_course = Course.create(course_name: course_name)
if new_course.save
flash[:success] = "Course created successfully"
render json: new_course, status: :created
else
flash[:error] = "Failed to save the new course to the database"
render json: new_course.errors, status: :unprocessable_entity
end
new_course.save
render_response(new_course,
"Course created successfully",
"Failed to save the new course to the database"
)

end

def index
Expand All @@ -30,8 +29,50 @@ 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)
new_user_to_course.save
render_response(new_user_to_course,
"User added to the course successfully.",
"Failed to add the user the to course."
)

end

private
def render_response(object, success_message, error_message)
if object.save
flash[:success] = success_message
render json: object, status: :created
else
flash[:error] = error_message
render json: { error: object.errors.full_messages }, status: :unprocessable_entity
end
end

end
end
end
12 changes: 9 additions & 3 deletions app/models/user_to_course.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
class UserToCourse < ApplicationRecord
belongs_to :user
belongs_to :course
end
# 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
Loading