Skip to content

Commit

Permalink
Merge from main and resolve conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
Zzz212zzZ committed Apr 25, 2024
2 parents a311ed8 + e9162bb commit 52d83bf
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 16 deletions.
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
3 changes: 2 additions & 1 deletion app/models/assignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ class Assignment < ApplicationRecord

validates :name, presence: true
validates :external_assignment_id, presence: true
end
has_many :extensions
end
2 changes: 2 additions & 0 deletions app/models/course.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Course < ApplicationRecord

# Associations
has_many :course_to_lmss
has_many :lmss, through: :course_to_lmss
Expand All @@ -7,4 +8,5 @@ class Course < ApplicationRecord

# Validations
validates :course_name, presence: true

end
1 change: 1 addition & 0 deletions app/models/course_to_lms.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class CourseToLms < ApplicationRecord

# Associations
belongs_to :course
belongs_to :lms
Expand Down
7 changes: 7 additions & 0 deletions app/models/extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Extension < ApplicationRecord
#Relationship with Assignment
belongs_to :assignment

#Relationship with User
has_one :user
end
7 changes: 5 additions & 2 deletions app/models/lms.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# app/models/lms.rb
class Lms < ApplicationRecord
#Relationship with Course (and CourseToLms)
has_many :course_to_lmss
has_many :courses, :through => :course_to_lmss


#Relationship with Assignment
has_many :assignments
end
9 changes: 7 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ class User < ApplicationRecord
validates :email, presence: true, uniqueness: true
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP, message: 'must be a valid email address' }

# Associasions
# Associations
has_many :lms_credentials, dependent: :destroy
has_many :user_to_courses

# Relationship with Extension
has_many :extensions

#Relationship with Course (and UserToCourse)
has_many :user_to_courses
has_many :courses, :through => :user_to_courses
end
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
83 changes: 83 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,86 @@
# ["Action", "Comedy", "Drama", "Horror"].each do |genre_name|
# MovieGenre.find_or_create_by!(name: genre_name)
# end

LmsCredential.destroy_all
Extension.destroy_all
Assignment.destroy_all
CourseToLms.destroy_all
UserToCourse.destroy_all
Course.destroy_all
Lms.destroy_all
User.destroy_all

canvas = Lms.create!({
lms_name: "Canvas",
use_auth_token: true
})


test_assignment = Assignment.create!({
lms_id: canvas.id,
name: "Test Assignment",
external_assignment_id: "11111"
})


test_course = Course.create!({
course_name: "Test Course",
})

test_course_to_lms = CourseToLms.create!({
lms_id: canvas.id,
course_id: test_course.id,
external_course_id: "22222"
})

test_user = User.create!({
email: "[email protected]",
})

test_user_to_course = UserToCourse.create!({
user_id: test_user.id,
course_id: test_course.id,
role: "test"
})

test_extension = Extension.create!({
assignment_id: test_assignment.id,
student_email: "[email protected]",
initial_due_date: DateTime.iso8601('2024-04-20'),
new_due_date: DateTime.iso8601('2024-04-30'),
last_processed_by_id: test_user.id,
external_extension_id: "33333"
})

test_lms_credential = LmsCredential.create!({
user_id: test_user.id,
lms_name: "canvas",
token: "test token",
external_user_id: "44444"
})

real_course = Course.create!({
course_name: "CS169L Flextension",
})

real_course_to_lms = CourseToLms.create!({
lms_id: canvas.id,
course_id: real_course.id,
external_course_id: "1534405"
})

real_assignment = Assignment.create!({
lms_id: canvas.id,
name: "Seed Data Testing",
external_assignment_id: "8741483"
})

real_extension = Extension.create!({
assignment_id: real_assignment.id,
student_email: "[email protected]",
initial_due_date: DateTime.iso8601('2024-04-20'),
new_due_date: DateTime.iso8601('2024-04-27'),
last_processed_by_id: test_user.id,
external_extension_id: "270163"
})
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 52d83bf

Please sign in to comment.