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

E2484. Reimplement participants_controller.rb #135

Open
wants to merge 94 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
7312265
Created 'participants_controller.rb'
Pierce57 Nov 11, 2024
f110eb5
Created 'participants_controller_spec.rb'
Pierce57 Nov 11, 2024
6de47f0
Created 'participants_helper.rb'
Pierce57 Nov 11, 2024
da2312e
Add courses to participants
augentism Nov 11, 2024
86e61bb
Merge branch 'main' of https://github.com/Pierce57/participant_contro…
augentism Nov 11, 2024
76bb8f7
add 'participants' relationship to 'users.rb'
augentism Nov 11, 2024
b930498
seedfile for participants
augentism Nov 12, 2024
7de83a6
Adding the method header to 'participants_controller.rb'
Pierce57 Nov 17, 2024
faf99e7
Adding skeleton methods to 'participants_controller.rb'
Pierce57 Nov 17, 2024
d5dee0c
Adding routes for 'participants_controller.rb'
Pierce57 Nov 17, 2024
a774e92
Not needed
Pierce57 Nov 18, 2024
bd951af
Adding 'authorization' stub to 'participant.rb'
Pierce57 Nov 18, 2024
0ea5416
Removing 'authorization' stub from 'participant.rb'
Pierce57 Nov 18, 2024
ae97aa0
Adding 'participant_params' stub and renaming 'update_authorization' …
Pierce57 Nov 18, 2024
4c60651
remove participant from courses
augentism Nov 18, 2024
c5019a2
Merge branch 'main' into seed
augentism Nov 18, 2024
232bfe9
updated seeds to remove courses from participants
augentism Nov 18, 2024
222f075
Deleting 'bequeath' and 'inherit' || Completed 'participant_params'
Pierce57 Nov 29, 2024
94c21c1
Removing 'update_permissions' and 'update_handle' || Deleting 'can_su…
Pierce57 Nov 29, 2024
bef80a1
'show' Method Implemented
Pierce57 Nov 29, 2024
6bb0812
'index' Method Implemented
Pierce57 Nov 29, 2024
aa05606
'create' Method Implemented
Pierce57 Nov 29, 2024
a647f51
'destroy' Method Implemented
Pierce57 Nov 29, 2024
8edd85d
Updating 'participant_params' description
Pierce57 Nov 29, 2024
2cb5191
Improved 'destroy' error handling
Pierce57 Nov 29, 2024
6cf9e75
Reordering 'participant_params' parameters
Pierce57 Nov 29, 2024
07caca0
Adding 'user_id' and 'assignment_id' validations to the Participant M…
Pierce57 Nov 29, 2024
1bcc009
Modifying 'deletion_message' based off new Participant Validations
Pierce57 Nov 29, 2024
d835548
Setting the 'user_id' and 'assignment_id' for the Participant object …
Pierce57 Nov 29, 2024
b5301fe
The 'index' function can now list out all the Participants of a given…
Pierce57 Nov 29, 2024
e952ed9
The 'show' function can now list out a specific user's participant
Pierce57 Nov 29, 2024
9903460
Maintaining a consistent styling in 'participants_controller.rb'
Pierce57 Nov 29, 2024
7b148f2
Maintaining a consistent styling in 'participants_controller.rb'
Pierce57 Nov 29, 2024
d65dfe4
Updating 'participants_controller' comments
Pierce57 Nov 29, 2024
41adf6d
Undoing "The 'show' function can now list out a specific user's parti…
Pierce57 Nov 29, 2024
79fd638
The 'index' function can now return a list of participants for an ass…
Pierce57 Nov 29, 2024
c824f02
Updating 'routes.rb' to include the new 'index' API calls
Pierce57 Nov 29, 2024
8784102
removed can_submit, can_review, and handle from migrations
augentism Nov 30, 2024
b928dda
Updating 'index' method comment
Pierce57 Nov 29, 2024
2698b6d
Adding an Error Response to 'index'
Pierce57 Dec 1, 2024
b33cea8
Adding an Error Response to 'show'
Pierce57 Dec 1, 2024
7bf7973
seed
augentism Dec 1, 2024
a75d932
Merge branch 'main' of github.com:Pierce57/participant_controller_rei…
augentism Dec 1, 2024
79758a8
add can_take_quiz and can_mentor
augentism Dec 1, 2024
1ca78ab
add delegation association to course for participant
augentism Dec 1, 2024
4423e10
Changing 'create' to 'add'
Pierce57 Dec 2, 2024
ae5a994
Moving the code from 'build_participant' method back into the 'add' m…
Pierce57 Dec 2, 2024
9250d4b
Moving the code from 'deletion_message' back into 'destroy' method
Pierce57 Dec 2, 2024
b1f8a2b
Adding new Participant parameters - 'can_submit', 'can_review', 'can_…
Pierce57 Dec 2, 2024
6b8d1db
Correcting 'add' route
Pierce57 Dec 2, 2024
b11428d
Reverting from 'add' to 'create' due to redundancy in AssignmentsCont…
Pierce57 Dec 2, 2024
4614089
Modifying 'destroy' Method comment
Pierce57 Dec 2, 2024
fa0c897
'participants_controller.rb' Corrections during Testing || First Iter…
mochi6 Dec 2, 2024
f26701c
Adding the include for the 'participants_helper.rb' module to 'partic…
Pierce57 Dec 2, 2024
87af927
Adding a 'participant_permissions' helper function to 'participants_h…
Pierce57 Dec 2, 2024
275be0e
Changing 'create' back to 'add'
Pierce57 Dec 2, 2024
29082af
Creating an 'AssignmentParticipant' instance in 'add' instead of a 'P…
Pierce57 Dec 2, 2024
ca8b6a4
added role to participants
augentism Dec 2, 2024
9ee0ae5
Merge branch 'main' of github.com:Pierce57/participant_controller_rei…
augentism Dec 2, 2024
69684b7
added role association to model
augentism Dec 2, 2024
63a059f
fix role association
augentism Dec 2, 2024
a5a2aea
Renaming 'participant_permissions' to 'retrieve_participant_permissio…
Pierce57 Dec 2, 2024
dc4ecb6
Added Participant model validations for 'role', 'can_submit', 'can_re…
Pierce57 Dec 2, 2024
c3ee005
Handling Participant permissions assignment in the 'add' method || Ch…
Pierce57 Dec 2, 2024
746687a
Adding ':role' validation in the 'add' method - handled by the 'valid…
Pierce57 Dec 2, 2024
c9569e8
Adding the ':role' field to 'participant_params'
Pierce57 Dec 2, 2024
a0f8988
Updated 'add' route based on API Call change
Pierce57 Dec 2, 2024
8ccd45a
'update_role' Method Implemented
Pierce57 Dec 2, 2024
d43362f
Adding a route for the 'update_role' API Call
Pierce57 Dec 2, 2024
274ac02
Adding logic to find the Participant in 'update_role' - handled in 'f…
Pierce57 Dec 2, 2024
a890807
Adding comments to the private methods in 'participants_controller.rb'
Pierce57 Dec 2, 2024
5963dfd
Small modification to 'retrieve_participant_permissions' Method comment
Pierce57 Dec 2, 2024
e3e26e1
Utilizing AssignmentController's 'add_participant' method in the 'add…
Pierce57 Dec 2, 2024
0105c4d
re add handle to participants
augentism Dec 2, 2024
ed50085
Merge branch 'main' of github.com:Pierce57/participant_controller_rei…
augentism Dec 2, 2024
e281e33
Added 'handle' to 'participant_params'
Pierce57 Dec 2, 2024
06ed043
Taking away validations for 'role', 'can_submit', 'can_review', 'can_…
Pierce57 Dec 2, 2024
7ada66c
change role to designation
augentism Dec 2, 2024
2516bef
Switched all mentions of 'role' to 'designation' in 'participants_hel…
Pierce57 Dec 2, 2024
e2ea10e
Switched all mentions of 'role' to 'designation' in 'participants_con…
Pierce57 Dec 2, 2024
b53ace1
Switched all mentions of 'role' to 'designation' in 'routes.rb'
Pierce57 Dec 2, 2024
0d08fd5
Fixing 'designation' assignment in 'add' and 'update_designation' met…
Pierce57 Dec 2, 2024
09963ad
"Fixed the swagger UI test code"
mochi6 Dec 2, 2024
fac9cd5
change designation to authorization
augentism Dec 2, 2024
d9bea2e
Switched all mentions of 'designation' to 'authorization' in 'routes.rb'
Pierce57 Dec 2, 2024
57eb1d1
Switched all mentions of 'designation' to 'authorization' in 'partici…
Pierce57 Dec 2, 2024
a283278
Switched all mentions of 'designation' to 'authorization' in 'partici…
Pierce57 Dec 2, 2024
a3f36ab
Changing 'index' Routes to point to new 'user_index' and 'assignment_…
Pierce57 Dec 2, 2024
9695c90
Split the 'index' Method into 'user_index' and 'assignment_index' || …
Pierce57 Dec 2, 2024
620c372
"Updated the test cases to align with the current implementation"
mochi6 Dec 2, 2024
9ea1701
"Updated the destroy method in the controller to cover participant ni…
mochi6 Dec 3, 2024
f500002
"added one more test case"
mochi6 Dec 3, 2024
5055f2b
"updated the names for a few tests"
mochi6 Dec 3, 2024
ecfe92d
"Updated the Post test, added a new method to dynamically find user n…
mochi6 Dec 3, 2024
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
194 changes: 194 additions & 0 deletions app/controllers/api/v1/participants_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
class Api::V1::ParticipantsController < ApplicationController
include ParticipantsHelper

# Return a list of participants for a given user
# params - user_id
# GET /participants/user/:user_id
def user_index
user = find_user if params[:user_id].present?
return if params[:user_id].present? && user.nil?

participants = filter_user_participants(user)

if participants.nil?
render json: participants.errors, status: :unprocessable_entity
else
render json: participants, status: :ok
end
end

# Return a list of participants for a given assignment
# params - assignment_id
# GET /participants/assignment/:assignment_id
def assignment_index
assignment = find_assignment if params[:assignment_id].present?
return if params[:assignment_id].present? && assignment.nil?

participants = filter_assignment_participants(assignment)

if participants.nil?
render json: participants.errors, status: :unprocessable_entity
else
render json: participants, status: :ok
end
end

# Return a specified participant
# params - id
# GET /participants/:id
def show
participant = Participant.find(params[:id])

if participant.nil?
render json: participant.errors, status: :unprocessable_entity
else
render json: participant, status: :created
end
end

# Assign the specified authorization to the participant and add them to an assignment
# POST /participants/:authorization
def add
user = find_user
return unless user

assignment = find_assignment
return unless assignment

authorization = validate_authorization
return unless authorization

permissions = retrieve_participant_permissions(authorization)

participant = assignment.add_participant(user)
participant.authorization = authorization
participant.can_submit = permissions[:can_submit]
participant.can_review = permissions[:can_review]
participant.can_take_quiz = permissions[:can_take_quiz]
participant.can_mentor = permissions[:can_mentor]

if participant.save
render json: participant, status: :created
else
render json: participant.errors, status: :unprocessable_entity
end
end

# Update the specified participant to the specified authorization
# PATCH /participants/:id/:authorization
def update_authorization
participant = find_participant
return unless participant

authorization = validate_authorization
return unless authorization

permissions = retrieve_participant_permissions(authorization)

participant.authorization = authorization
participant.can_submit = permissions[:can_submit]
participant.can_review = permissions[:can_review]
participant.can_take_quiz = permissions[:can_take_quiz]
participant.can_mentor = permissions[:can_mentor]

if participant.save
render json: participant, status: :created
else
render json: participant.errors, status: :unprocessable_entity
end
end

# Delete a participant
# params - id
# DELETE /participants/:id
def destroy
participant = Participant.find_by(id: params[:id])

if participant.nil?
render json: { error: 'Not Found' }, status: :not_found
elsif participant.destroy
successful_deletion_message = if params[:team_id].nil?
"Participant #{params[:id]} in Assignment #{params[:assignment_id]} has been deleted successfully!"
else
"Participant #{params[:id]} in Team #{params[:team_id]} of Assignment #{params[:assignment_id]} has been deleted successfully!"
end
render json: { message: successful_deletion_message }, status: :ok
else
render json: participant.errors, status: :unprocessable_entity
end
end

# Permitted parameters for creating a Participant object
def participant_params
params.require(:participant).permit(:user_id, :assignment_id, :authorization, :can_submit,
:can_review, :can_take_quiz, :can_mentor, :handle,
:team_id, :join_team_request_id, :permission_granted,
:topic, :current_stage, :stage_deadline)
end

private

# Filters participants based on the provided user
# Returns participants ordered by their IDs
def filter_user_participants(user)
participants = Participant.all
participants = participants.where(user_id: user.id) if user
participants.order(:id)
end

# Filters participants based on the provided assignment
# Returns participants ordered by their IDs
def filter_assignment_participants(assignment)
participants = Participant.all
participants = participants.where(assignment_id: assignment.id) if assignment
participants.order(:id)
end

# Finds a user based on the user_id parameter
# Returns the user if found
def find_user
user_id = params[:user_id]
user = User.find_by(id: user_id)
render json: { error: 'User not found' }, status: :not_found unless user
user
end

# Finds an assignment based on the assignment_id parameter
# Returns the assignment if found
def find_assignment
assignment_id = params[:assignment_id]
assignment = Assignment.find_by(id: assignment_id)
render json: { error: 'Assignment not found' }, status: :not_found unless assignment
assignment
end

# Finds a participant based on the id parameter
# Returns the participant if found
def find_participant
participant_id = params[:id]
participant = Participant.find_by(id: participant_id)
render json: { error: 'Participant not found' }, status: :not_found unless participant
participant
end

# Validates that the authorization parameter is present and is one of the following valid authorizations: reader, reviewer, submitter, mentor
# Returns the authorization if valid
def validate_authorization
valid_authorizations = %w[reader reviewer submitter mentor]
authorization = params[:authorization]
authorization = authorization.downcase if authorization.present?

unless authorization
render json: { error: 'authorization is required' }, status: :unprocessable_entity
return
end

unless valid_authorizations.include?(authorization)
render json: { error: 'authorization not valid. Valid authorizations are: Reader, Reviewer, Submitter, Mentor' },
status: :unprocessable_entity
return
end

authorization
end
end
28 changes: 28 additions & 0 deletions app/helpers/participants_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module ParticipantsHelper
# ===========================================================
# A participant can be one of the following authorizations:
# Reader
# Reviewer
# Submitter
# Mentor
# ===========================================================
# Grant a participant permissions to submit, review,
# take quizzes, and mentor based on their authorization
def retrieve_participant_permissions(authorization)
default_permissions = {
can_submit: true,
can_review: true,
can_take_quiz: true,
can_mentor: false
}

permissions_map = {
'reader' => { can_submit: false },
'reviewer' => { can_submit: false, can_take_quiz: false },
'submitter' => { can_review: false, can_take_quiz: false },
'mentor' => { can_mentor: true }
}

default_permissions.merge(permissions_map[authorization])
end
end
2 changes: 1 addition & 1 deletion app/models/course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ def copy_course
new_course.name += '_copy'
new_course.save
end
end
end
8 changes: 8 additions & 0 deletions app/models/participant.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
class Participant < ApplicationRecord
# Associations
belongs_to :user
belongs_to :assignment, foreign_key: 'assignment_id', inverse_of: false
has_many :join_team_requests, dependent: :destroy
belongs_to :team, optional: true

delegate :course, to: :assignment

# Validations
validates :user_id, presence: true
validates :assignment_id, presence: true

# Methods
def fullname
user.fullname
end
Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class User < ApplicationRecord
has_many :assignments
has_many :teams_users, dependent: :destroy
has_many :teams, through: :teams_users
has_many :participants

scope :students, -> { where role_id: Role::STUDENT }
scope :tas, -> { where role_id: Role::TEACHING_ASSISTANT }
Expand Down
11 changes: 11 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@
get :processed, action: :processed_requests
end
end

resources :participants do
collection do
get '/user/:user_id', to: 'participants#user_index'
get '/assignment/:assignment_id', to: 'participants#assignment_index'
get '/:id', to: 'participants#show'
post '/:authorization', to: 'participants#add'
patch '/:id/:authorization', to: 'participants#update_authorization'
delete '/:id', to: 'participants#destroy'
end
end
end
end
end
10 changes: 5 additions & 5 deletions db/migrate/20231026002451_add_can_submit_to_participant.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class AddCanSubmitToParticipant < ActiveRecord::Migration[7.0]
def change
add_column :participants, :can_submit, :boolean, :default => true
end
end
class AddCanSubmitToParticipant < ActiveRecord::Migration[7.0]
def change
add_column :participants, :can_submit, :boolean, :default => true
end
end
10 changes: 5 additions & 5 deletions db/migrate/20231026002543_add_can_review_to_participant.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class AddCanReviewToParticipant < ActiveRecord::Migration[7.0]
def change
add_column :participants, :can_review, :boolean, :default => true
end
end
class AddCanReviewToParticipant < ActiveRecord::Migration[7.0]
def change
add_column :participants, :can_review, :boolean, :default => true
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCanTakeQuizToParticipants < ActiveRecord::Migration[7.0]
def change
add_column :participants, :can_take_quiz, :boolean
end
end
5 changes: 5 additions & 0 deletions db/migrate/20241201224137_add_can_mentor_to_participants.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCanMentorToParticipants < ActiveRecord::Migration[7.0]
def change
add_column :participants, :can_mentor, :boolean
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddAuthorizationToParticipants < ActiveRecord::Migration[7.0]
def change
add_column :participants, :authorization, :string
end
end
5 changes: 4 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading