From 73122651ad74afb5c86c1c57321830fdf2112df7 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Mon, 11 Nov 2024 13:57:50 -0500 Subject: [PATCH 01/91] Created 'participants_controller.rb' --- app/controllers/api/v1/participants_controller.rb | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 app/controllers/api/v1/participants_controller.rb diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb new file mode 100644 index 000000000..e69de29bb From f110eb5eaaa1a5fc2b2b273db9adaed456a1dfb9 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Mon, 11 Nov 2024 14:26:07 -0500 Subject: [PATCH 02/91] Created 'participants_controller_spec.rb' --- spec/models/participants_controller_spec.rb | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 spec/models/participants_controller_spec.rb diff --git a/spec/models/participants_controller_spec.rb b/spec/models/participants_controller_spec.rb new file mode 100644 index 000000000..e69de29bb From 6de47f0fae7e94637a4e1f1dacdf227688ef8ced Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Mon, 11 Nov 2024 14:27:37 -0500 Subject: [PATCH 03/91] Created 'participants_helper.rb' --- app/helpers/participants_helper.rb | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 app/helpers/participants_helper.rb diff --git a/app/helpers/participants_helper.rb b/app/helpers/participants_helper.rb new file mode 100644 index 000000000..e69de29bb From da2312e05eccd84efb455637788f17b8fe2abf41 Mon Sep 17 00:00:00 2001 From: augentism Date: Mon, 11 Nov 2024 15:30:04 -0500 Subject: [PATCH 04/91] Add courses to participants --- app/controllers/api/v1/participants_controller.rb | 0 app/helpers/participants_helper.rb | 0 app/models/course.rb | 3 ++- app/models/participant.rb | 1 + db/migrate/20241111201613_add_course_to_participants.rb | 5 +++++ db/schema.rb | 5 ++++- spec/models/participants_controller_spec.rb | 0 7 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 app/controllers/api/v1/participants_controller.rb create mode 100644 app/helpers/participants_helper.rb create mode 100644 db/migrate/20241111201613_add_course_to_participants.rb create mode 100644 spec/models/participants_controller_spec.rb diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb new file mode 100644 index 000000000..e69de29bb diff --git a/app/helpers/participants_helper.rb b/app/helpers/participants_helper.rb new file mode 100644 index 000000000..e69de29bb diff --git a/app/models/course.rb b/app/models/course.rb index 9e70ccf7d..2663eed07 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -5,6 +5,7 @@ class Course < ApplicationRecord validates :directory_path, presence: true has_many :ta_mappings, dependent: :destroy has_many :tas, through: :ta_mappings + has_many :participants # Returns the submission directory for the course def path @@ -49,4 +50,4 @@ def copy_course new_course.name += '_copy' new_course.save end -end \ No newline at end of file +end diff --git a/app/models/participant.rb b/app/models/participant.rb index 4ab489ab2..c990e822f 100644 --- a/app/models/participant.rb +++ b/app/models/participant.rb @@ -3,6 +3,7 @@ class Participant < ApplicationRecord belongs_to :assignment, foreign_key: 'assignment_id', inverse_of: false has_many :join_team_requests, dependent: :destroy belongs_to :team, optional: true + belongs_to :course def fullname user.fullname diff --git a/db/migrate/20241111201613_add_course_to_participants.rb b/db/migrate/20241111201613_add_course_to_participants.rb new file mode 100644 index 000000000..a5784a0e0 --- /dev/null +++ b/db/migrate/20241111201613_add_course_to_participants.rb @@ -0,0 +1,5 @@ +class AddCourseToParticipants < ActiveRecord::Migration[7.0] + def change + add_reference :participants, :course, null: false, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 1770d8997..fba8b0256 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_04_15_192048) do +ActiveRecord::Schema[7.0].define(version: 2024_11_11_201613) do create_table "account_requests", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.string "username" t.string "full_name" @@ -187,7 +187,9 @@ t.string "topic" t.string "current_stage" t.datetime "stage_deadline" + t.bigint "course_id", null: false t.index ["assignment_id"], name: "index_participants_on_assignment_id" + t.index ["course_id"], name: "index_participants_on_course_id" t.index ["join_team_request_id"], name: "index_participants_on_join_team_request_id" t.index ["team_id"], name: "index_participants_on_team_id" t.index ["user_id"], name: "fk_participant_users" @@ -338,6 +340,7 @@ add_foreign_key "courses", "institutions" add_foreign_key "courses", "users", column: "instructor_id" add_foreign_key "participants", "assignments" + add_foreign_key "participants", "courses" add_foreign_key "participants", "join_team_requests" add_foreign_key "participants", "teams" add_foreign_key "participants", "users" diff --git a/spec/models/participants_controller_spec.rb b/spec/models/participants_controller_spec.rb new file mode 100644 index 000000000..e69de29bb From 76bb8f731afe5061b0dfe981c08f7cf423ae9c25 Mon Sep 17 00:00:00 2001 From: augentism Date: Mon, 11 Nov 2024 17:00:05 -0500 Subject: [PATCH 05/91] add 'participants' relationship to 'users.rb' --- app/models/user.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/user.rb b/app/models/user.rb index 87f138fe5..ebcdf9ed0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 } From b9304983e3cf7340dc7b2ad4e93c8f40d737883e Mon Sep 17 00:00:00 2001 From: augentism Date: Tue, 12 Nov 2024 16:16:17 -0500 Subject: [PATCH 06/91] seedfile for participants --- db/seeds.rb | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 113 insertions(+), 3 deletions(-) diff --git a/db/seeds.rb b/db/seeds.rb index 134ac82e5..874bca699 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,8 +1,8 @@ begin #Create an instritution - Institution.create!( + inst_id = Institution.create!( name: 'North Carolina State University', - ) + ).id # Create an admin user User.create!( @@ -13,6 +13,116 @@ institution_id: 1, role_id: 1 ) + + + #Generate Random Users + num_students = 48 + num_assignments = 8 + num_teams = 16 + num_courses = 2 + num_instructors = 2 + + puts "creating instructors" + instructor_user_ids = [] + num_instructors.times do + instructor_user_ids << User.create( + name: Faker::Internet.unique.username, + email: Faker::Internet.unique.email, + password: "password", + full_name: Faker::Name.name, + institution_id: 1, + role_id: 3, + ).id + end + + puts "creating courses" + course_ids = [] + num_courses.times do |i| + course_ids << Course.create( + instructor_id: instructor_user_ids[i], + institution_id: inst_id, + directory_path: Faker::File.dir(segment_count: 2), + name: Faker::Company.industry, + info: "A fake class", + private: false + ).id + end + + puts "creating assignments" + assignment_ids = [] + num_assignments.times do |i| + assignment_ids << Assignment.create( + name: Faker::Verb.base, + instructor_id: instructor_user_ids[i%num_instructors], + course_id: course_ids[i%num_courses], + has_teams: true, + private: false + ).id + end + + + puts "creating teams" + team_ids = [] + num_teams.times do |i| + team_ids << Team.create( + assignment_id: assignment_ids[i%num_assignments] + ).id + end + + puts "creating students" + student_user_ids = [] + num_students.times do + student_user_ids << User.create( + name: Faker::Internet.unique.username, + email: Faker::Internet.unique.email, + password: "password", + full_name: Faker::Name.name, + institution_id: 1, + role_id: 5, + ).id + end + + puts "assigning students to teams" + teams_users_ids = [] + #num_students.times do |i| + # teams_users_ids << TeamsUser.create( + # team_id: team_ids[i%num_teams], + # user_id: student_user_ids[i] + # ).id + #end + + num_students.times do |i| + puts "Creating TeamsUser with team_id: #{team_ids[i % num_teams]}, user_id: #{student_user_ids[i]}" + teams_user = TeamsUser.create( + team_id: team_ids[i % num_teams], + user_id: student_user_ids[i] + ) + if teams_user.persisted? + teams_users_ids << teams_user.id + puts "Created TeamsUser with ID: #{teams_user.id}" + else + puts "Failed to create TeamsUser: #{teams_user.errors.full_messages.join(', ')}" + end + end + + puts "assigning participant to students, teams, courses, and assignments" + participant_ids = [] + num_students.times do |i| + participant_ids << Participant.create( + user_id: student_user_ids[i], + assignment_id: assignment_ids[i%num_assignments], + team_id: team_ids[i%num_teams], + course_id: course_ids[i%num_courses] + ).id + end + + + + + + + + rescue ActiveRecord::RecordInvalid => e puts 'The db has already been seeded' -end \ No newline at end of file +end From 7de83a6a0451e2cf8ebcc6bc5cd6e14a4979a602 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 17 Nov 2024 11:29:12 -0500 Subject: [PATCH 07/91] Adding the method header to 'participants_controller.rb' --- app/controllers/api/v1/participants_controller.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index e69de29bb..9789ffa1c 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -0,0 +1,3 @@ +class Api::V1::ParticipantsController < ApplicationController + +end \ No newline at end of file From faf99e76a8dede583b7898fb7ce1404af40f08b8 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 17 Nov 2024 11:38:57 -0500 Subject: [PATCH 08/91] Adding skeleton methods to 'participants_controller.rb' --- .../api/v1/participants_controller.rb | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 9789ffa1c..575a3c799 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -1,3 +1,33 @@ class Api::V1::ParticipantsController < ApplicationController + # Return a list of participants + # GET /participants + def index; end -end \ No newline at end of file + # Return a specified participant + # GET /participant/:id + def show; end + + # Copy all participants from a course to an assignment + # GET /participants/inherit + def inherit; end + + # Copy all participants from an assignment to a course + # GET /participants/bequeath + def bequeath; end + + # Create a participant + # POST /participant + def create; end + + # Update the permissions of a participant + # PATCH /participant/:id/authorization + def update_authorization; end + + # Update the handle of a participant + # PATCH /participant/:id/handle + def update_handle; end + + # Delete a specified participant + # DELETE /participant/:id + def destroy; end +end From d5dee0c446f988099f03903bbed970f39ee657de Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 17 Nov 2024 12:16:46 -0500 Subject: [PATCH 09/91] Adding routes for 'participants_controller.rb' --- app/controllers/api/v1/participants_controller.rb | 10 +++++----- config/routes.rb | 13 +++++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 575a3c799..fc9577048 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -4,7 +4,7 @@ class Api::V1::ParticipantsController < ApplicationController def index; end # Return a specified participant - # GET /participant/:id + # GET /participants/:id def show; end # Copy all participants from a course to an assignment @@ -16,18 +16,18 @@ def inherit; end def bequeath; end # Create a participant - # POST /participant + # POST /participants def create; end # Update the permissions of a participant - # PATCH /participant/:id/authorization + # PATCH /participants/:id/authorization def update_authorization; end # Update the handle of a participant - # PATCH /participant/:id/handle + # PATCH /participants/:id/handle def update_handle; end # Delete a specified participant - # DELETE /participant/:id + # DELETE /participants/:id def destroy; end end diff --git a/config/routes.rb b/config/routes.rb index fc8a710a2..85448dd4c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -109,6 +109,19 @@ get :processed, action: :processed_requests end end + + resources :participants do + collection do + get '/', to: 'participants#index' + get '/:id', to: 'participants#show' + get '/inherit', to: 'participants#inherit' + get '/bequeath', to: 'participants#bequeath' + post '/', to: 'participants#create' + patch '/:id/authorization', to: 'participants#update_authorization' + patch '/:id/handle', to: 'participants#update_handle' + delete '/:id', to: 'participants#destroy' + end + end end end end \ No newline at end of file From a774e922d519545b089953f39a38e81ac8f7f749 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 17 Nov 2024 23:32:14 -0500 Subject: [PATCH 10/91] Not needed --- app/helpers/participants_helper.rb | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 app/helpers/participants_helper.rb diff --git a/app/helpers/participants_helper.rb b/app/helpers/participants_helper.rb deleted file mode 100644 index e69de29bb..000000000 From bd951af14fbd6d971cb93bcbfb3f679f9b58a18d Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 17 Nov 2024 23:36:01 -0500 Subject: [PATCH 11/91] Adding 'authorization' stub to 'participant.rb' --- app/models/participant.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/models/participant.rb b/app/models/participant.rb index c990e822f..9c96876d9 100644 --- a/app/models/participant.rb +++ b/app/models/participant.rb @@ -8,4 +8,8 @@ class Participant < ApplicationRecord def fullname user.fullname end + + def authorization + # To be implemented + end end From 0ea5416593821d8a6bfa44e4feeedf1a8f259738 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 17 Nov 2024 23:59:28 -0500 Subject: [PATCH 12/91] Removing 'authorization' stub from 'participant.rb' --- app/models/participant.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/models/participant.rb b/app/models/participant.rb index 9c96876d9..c990e822f 100644 --- a/app/models/participant.rb +++ b/app/models/participant.rb @@ -8,8 +8,4 @@ class Participant < ApplicationRecord def fullname user.fullname end - - def authorization - # To be implemented - end end From ae97aa028dd33fee48dea723ed8c68224ff46a44 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Mon, 18 Nov 2024 00:00:07 -0500 Subject: [PATCH 13/91] Adding 'participant_params' stub and renaming 'update_authorization' to 'update_permissions' in 'participants_controller.rb' --- app/controllers/api/v1/participants_controller.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index fc9577048..3746eea9a 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -20,8 +20,8 @@ def bequeath; end def create; end # Update the permissions of a participant - # PATCH /participants/:id/authorization - def update_authorization; end + # PATCH /participants/:id/permissions + def update_permissions; end # Update the handle of a participant # PATCH /participants/:id/handle @@ -30,4 +30,7 @@ def update_handle; end # Delete a specified participant # DELETE /participants/:id def destroy; end + + # Permitted parameters for creating or updating a Participant object + def participant_params; end end From 4c60651acfb8e928bef1a289fa883aeaded07360 Mon Sep 17 00:00:00 2001 From: augentism Date: Mon, 18 Nov 2024 03:01:28 -0500 Subject: [PATCH 14/91] remove participant from courses --- app/models/course.rb | 1 - app/models/participant.rb | 1 - db/migrate/20241111201613_add_course_to_participants.rb | 5 ----- db/schema.rb | 5 +---- 4 files changed, 1 insertion(+), 11 deletions(-) delete mode 100644 db/migrate/20241111201613_add_course_to_participants.rb diff --git a/app/models/course.rb b/app/models/course.rb index 2663eed07..e008fda7c 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -5,7 +5,6 @@ class Course < ApplicationRecord validates :directory_path, presence: true has_many :ta_mappings, dependent: :destroy has_many :tas, through: :ta_mappings - has_many :participants # Returns the submission directory for the course def path diff --git a/app/models/participant.rb b/app/models/participant.rb index c990e822f..4ab489ab2 100644 --- a/app/models/participant.rb +++ b/app/models/participant.rb @@ -3,7 +3,6 @@ class Participant < ApplicationRecord belongs_to :assignment, foreign_key: 'assignment_id', inverse_of: false has_many :join_team_requests, dependent: :destroy belongs_to :team, optional: true - belongs_to :course def fullname user.fullname diff --git a/db/migrate/20241111201613_add_course_to_participants.rb b/db/migrate/20241111201613_add_course_to_participants.rb deleted file mode 100644 index a5784a0e0..000000000 --- a/db/migrate/20241111201613_add_course_to_participants.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AddCourseToParticipants < ActiveRecord::Migration[7.0] - def change - add_reference :participants, :course, null: false, foreign_key: true - end -end diff --git a/db/schema.rb b/db/schema.rb index fba8b0256..1770d8997 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_11_11_201613) do +ActiveRecord::Schema[7.0].define(version: 2024_04_15_192048) do create_table "account_requests", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.string "username" t.string "full_name" @@ -187,9 +187,7 @@ t.string "topic" t.string "current_stage" t.datetime "stage_deadline" - t.bigint "course_id", null: false t.index ["assignment_id"], name: "index_participants_on_assignment_id" - t.index ["course_id"], name: "index_participants_on_course_id" t.index ["join_team_request_id"], name: "index_participants_on_join_team_request_id" t.index ["team_id"], name: "index_participants_on_team_id" t.index ["user_id"], name: "fk_participant_users" @@ -340,7 +338,6 @@ add_foreign_key "courses", "institutions" add_foreign_key "courses", "users", column: "instructor_id" add_foreign_key "participants", "assignments" - add_foreign_key "participants", "courses" add_foreign_key "participants", "join_team_requests" add_foreign_key "participants", "teams" add_foreign_key "participants", "users" From 232bfe97519afa4258ed1434bd3aff122e848e29 Mon Sep 17 00:00:00 2001 From: augentism Date: Mon, 18 Nov 2024 03:03:06 -0500 Subject: [PATCH 15/91] updated seeds to remove courses from participants --- db/seeds.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/db/seeds.rb b/db/seeds.rb index 874bca699..b6de376f2 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -112,7 +112,6 @@ user_id: student_user_ids[i], assignment_id: assignment_ids[i%num_assignments], team_id: team_ids[i%num_teams], - course_id: course_ids[i%num_courses] ).id end From 222f0756f47c929bfc89963aa7518503f1f3d76c Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Fri, 29 Nov 2024 12:44:50 -0500 Subject: [PATCH 16/91] Deleting 'bequeath' and 'inherit' || Completed 'participant_params' --- app/controllers/api/v1/participants_controller.rb | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 3746eea9a..fa8287809 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -7,14 +7,6 @@ def index; end # GET /participants/:id def show; end - # Copy all participants from a course to an assignment - # GET /participants/inherit - def inherit; end - - # Copy all participants from an assignment to a course - # GET /participants/bequeath - def bequeath; end - # Create a participant # POST /participants def create; end @@ -32,5 +24,9 @@ def update_handle; end def destroy; end # Permitted parameters for creating or updating a Participant object - def participant_params; end + def participant_params + params.require(:participant).permit(:user_id, :assignment_id, :can_submit, :can_review, :handle, + :permission_granted, :join_team_request_id, :team_id, :topic, + :current_stage, :stage_deadline) + end end From 94c21c1c4e925b30d2d2fc9442c9d177750b9fdc Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Fri, 29 Nov 2024 13:39:00 -0500 Subject: [PATCH 17/91] Removing 'update_permissions' and 'update_handle' || Deleting 'can_submit', 'can_review', and 'handle' fields from "participant_params" || Deleting all deprecated Participant routes --- app/controllers/api/v1/participants_controller.rb | 13 ++----------- config/routes.rb | 4 ---- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index fa8287809..85dcaca80 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -11,22 +11,13 @@ def show; end # POST /participants def create; end - # Update the permissions of a participant - # PATCH /participants/:id/permissions - def update_permissions; end - - # Update the handle of a participant - # PATCH /participants/:id/handle - def update_handle; end - # Delete a specified participant # DELETE /participants/:id def destroy; end # Permitted parameters for creating or updating a Participant object def participant_params - params.require(:participant).permit(:user_id, :assignment_id, :can_submit, :can_review, :handle, - :permission_granted, :join_team_request_id, :team_id, :topic, - :current_stage, :stage_deadline) + params.require(:participant).permit(:user_id, :assignment_id, :permission_granted, :join_team_request_id, + :team_id, :topic, :current_stage, :stage_deadline) end end diff --git a/config/routes.rb b/config/routes.rb index 85448dd4c..837cc22e7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -114,11 +114,7 @@ collection do get '/', to: 'participants#index' get '/:id', to: 'participants#show' - get '/inherit', to: 'participants#inherit' - get '/bequeath', to: 'participants#bequeath' post '/', to: 'participants#create' - patch '/:id/authorization', to: 'participants#update_authorization' - patch '/:id/handle', to: 'participants#update_handle' delete '/:id', to: 'participants#destroy' end end From bef80a17902b987e9ec2ba0dcc4b91a4af89d317 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Fri, 29 Nov 2024 13:40:46 -0500 Subject: [PATCH 18/91] 'show' Method Implemented --- app/controllers/api/v1/participants_controller.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 85dcaca80..bb3c08e59 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -5,7 +5,10 @@ def index; end # Return a specified participant # GET /participants/:id - def show; end + def show + participant = Participant.find(params[:id]) + render json: participant, status: :ok + end # Create a participant # POST /participants @@ -20,4 +23,4 @@ def participant_params params.require(:participant).permit(:user_id, :assignment_id, :permission_granted, :join_team_request_id, :team_id, :topic, :current_stage, :stage_deadline) end -end +end \ No newline at end of file From 6bb0812811c38c6857d593c8f48d256523528c82 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Fri, 29 Nov 2024 13:41:03 -0500 Subject: [PATCH 19/91] 'index' Method Implemented --- app/controllers/api/v1/participants_controller.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index bb3c08e59..0a97e3932 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -1,7 +1,10 @@ class Api::V1::ParticipantsController < ApplicationController # Return a list of participants # GET /participants - def index; end + def index + participants = Participant.order(:id) + render json: participants, status: :ok + end # Return a specified participant # GET /participants/:id From aa0560666130854f026f711889418eea37104be3 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Fri, 29 Nov 2024 13:41:45 -0500 Subject: [PATCH 20/91] 'create' Method Implemented --- app/controllers/api/v1/participants_controller.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 0a97e3932..87e8d083c 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -15,7 +15,14 @@ def show # Create a participant # POST /participants - def create; end + def create + participant = Participant.new(participant_params) + if participant.save + render json: participant, status: :created + else + render json: participant.errors, status: :unprocessable_entity + end + end # Delete a specified participant # DELETE /participants/:id From a647f51670e067972664fe1994259fedc0578274 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Fri, 29 Nov 2024 13:44:39 -0500 Subject: [PATCH 21/91] 'destroy' Method Implemented --- app/controllers/api/v1/participants_controller.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 87e8d083c..84dda54d1 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -26,7 +26,11 @@ def create # Delete a specified participant # DELETE /participants/:id - def destroy; end + def destroy + participant = Participant.find(params[:id]) + participant.destroy + render json: { message: "Participant #{params[:id]} deleted successfully!" }, status: :no_content + end # Permitted parameters for creating or updating a Participant object def participant_params From 8edd85d760a690da4a6cca310a2b8a9c88303c33 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Fri, 29 Nov 2024 13:44:53 -0500 Subject: [PATCH 22/91] Updating 'participant_params' description --- app/controllers/api/v1/participants_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 84dda54d1..b9d39675f 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -32,7 +32,7 @@ def destroy render json: { message: "Participant #{params[:id]} deleted successfully!" }, status: :no_content end - # Permitted parameters for creating or updating a Participant object + # Permitted parameters for creating a Participant object def participant_params params.require(:participant).permit(:user_id, :assignment_id, :permission_granted, :join_team_request_id, :team_id, :topic, :current_stage, :stage_deadline) From 2cb519154f8048688af887f2e9c1b556f242aeab Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Fri, 29 Nov 2024 14:11:54 -0500 Subject: [PATCH 23/91] Improved 'destroy' error handling --- .../api/v1/participants_controller.rb | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index b9d39675f..1bbbd91b8 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -28,8 +28,11 @@ def create # DELETE /participants/:id def destroy participant = Participant.find(params[:id]) - participant.destroy - render json: { message: "Participant #{params[:id]} deleted successfully!" }, status: :no_content + if participant.destroy + render json: { message: deletion_message(params) }, status: :ok + else + render json: participant.errors, status: :unprocessable_entity + end end # Permitted parameters for creating a Participant object @@ -37,4 +40,16 @@ def participant_params params.require(:participant).permit(:user_id, :assignment_id, :permission_granted, :join_team_request_id, :team_id, :topic, :current_stage, :stage_deadline) end -end \ No newline at end of file + + private + + def deletion_message(params) + if params[:assignment_id].nil? && params[:team_id].nil? + "Participant #{params[:id]} has been deleted successfully!" + elsif 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 + end +end From 6cf9e75a6850e79054432f1d4abe84d47ab74bd6 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Fri, 29 Nov 2024 14:18:46 -0500 Subject: [PATCH 24/91] Reordering 'participant_params' parameters --- app/controllers/api/v1/participants_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 1bbbd91b8..bae091652 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -37,8 +37,8 @@ def destroy # Permitted parameters for creating a Participant object def participant_params - params.require(:participant).permit(:user_id, :assignment_id, :permission_granted, :join_team_request_id, - :team_id, :topic, :current_stage, :stage_deadline) + params.require(:participant).permit(:user_id, :assignment_id, :team_id, :join_team_request_id, + :permission_granted, :topic, :current_stage, :stage_deadline) end private From 07caca0c924757a203a33348d47028af3d9ecef5 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Fri, 29 Nov 2024 14:19:06 -0500 Subject: [PATCH 25/91] Adding 'user_id' and 'assignment_id' validations to the Participant Model --- app/models/participant.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/models/participant.rb b/app/models/participant.rb index 4ab489ab2..75eb76ea1 100644 --- a/app/models/participant.rb +++ b/app/models/participant.rb @@ -1,9 +1,15 @@ 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 + # Validations + validates :user_id, presence: true + validates :assignment_id, presence: true + + # Methods def fullname user.fullname end From 1bcc0091f787c7c758447faa1a3d647e1bc8bf76 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Fri, 29 Nov 2024 14:19:51 -0500 Subject: [PATCH 26/91] Modifying 'deletion_message' based off new Participant Validations --- app/controllers/api/v1/participants_controller.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index bae091652..6569e1424 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -44,9 +44,7 @@ def participant_params private def deletion_message(params) - if params[:assignment_id].nil? && params[:team_id].nil? - "Participant #{params[:id]} has been deleted successfully!" - elsif params[:team_id].nil? + 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!" From d835548f011d6272cd17620bc9841044ac976255 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Fri, 29 Nov 2024 15:37:42 -0500 Subject: [PATCH 27/91] Setting the 'user_id' and 'assignment_id' for the Participant object in the 'create' method --- .../api/v1/participants_controller.rb | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 6569e1424..367bf1899 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -16,7 +16,13 @@ def show # Create a participant # POST /participants def create - participant = Participant.new(participant_params) + user = find_user + return unless user + + assignment = find_assignment + return unless assignment + + participant = build_participant(user, assignment) if participant.save render json: participant, status: :created else @@ -50,4 +56,23 @@ def deletion_message(params) "Participant #{params[:id]} in Team #{params[:team_id]} of Assignment #{params[:assignment_id]} has been deleted successfully!" end end + + def find_user + user = User.find_by(id: participant_params[:user_id]) + render json: { error: 'User not found' }, status: :not_found unless user + user + end + + def find_assignment + assignment = Assignment.find_by(id: participant_params[:assignment_id]) + render json: { error: 'Assignment not found' }, status: :not_found unless assignment + assignment + end + + def build_participant(user, assignment) + Participant.new(participant_params).tap do |participant| + participant.user_id = user.id + participant.assignment_id = assignment.id + end + end end From b5301fe1061555fee2f052f6b957fa4dad9cafb9 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Fri, 29 Nov 2024 15:40:24 -0500 Subject: [PATCH 28/91] The 'index' function can now list out all the Participants of a given user --- app/controllers/api/v1/participants_controller.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 367bf1899..318821e55 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -2,7 +2,12 @@ class Api::V1::ParticipantsController < ApplicationController # Return a list of participants # GET /participants def index - participants = Participant.order(:id) + participants = if params[:user_id].present? + Participant.where(user_id: params[:user_id]).order(:id) + else + Participant.order(:id) + end + render json: participants, status: :ok end From e952ed98c6a1a2d031b0ac6037ac1b844b38c9f0 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Fri, 29 Nov 2024 15:43:05 -0500 Subject: [PATCH 29/91] The 'show' function can now list out a specific user's participant --- app/controllers/api/v1/participants_controller.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 318821e55..95bc610b9 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -14,8 +14,13 @@ def index # Return a specified participant # GET /participants/:id def show - participant = Participant.find(params[:id]) - render json: participant, status: :ok + participant = Participant.find_by(id: params[:id], user_id: params[:user_id]) + + if participant + render json: participant, status: :ok + else + render json: { error: 'Participant not found or does not belong to the specified user' }, status: :not_found + end end # Create a participant From 9903460a9fef6cdb0270ee3de3583d118ba8e0ed Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Fri, 29 Nov 2024 15:43:34 -0500 Subject: [PATCH 30/91] Maintaining a consistent styling in 'participants_controller.rb' --- app/controllers/api/v1/participants_controller.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 95bc610b9..63de0bec2 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -33,6 +33,7 @@ def create return unless assignment participant = build_participant(user, assignment) + if participant.save render json: participant, status: :created else @@ -44,6 +45,7 @@ def create # DELETE /participants/:id def destroy participant = Participant.find(params[:id]) + if participant.destroy render json: { message: deletion_message(params) }, status: :ok else From 7b148f2b1e583997439315ec5788635f67ea13a8 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Fri, 29 Nov 2024 15:44:45 -0500 Subject: [PATCH 31/91] Maintaining a consistent styling in 'participants_controller.rb' --- app/controllers/api/v1/participants_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 63de0bec2..75a230e39 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -33,7 +33,7 @@ def create return unless assignment participant = build_participant(user, assignment) - + if participant.save render json: participant, status: :created else From d65dfe4b093c4d6b708f938f91a6c047bf3c4eac Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Fri, 29 Nov 2024 15:50:49 -0500 Subject: [PATCH 32/91] Updating 'participants_controller' comments --- app/controllers/api/v1/participants_controller.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 75a230e39..9c5b0cbe0 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -1,5 +1,8 @@ class Api::V1::ParticipantsController < ApplicationController - # Return a list of participants + # Return a list of participants for a user or assignment + # params - id + # user_id + # assignment_id # GET /participants def index participants = if params[:user_id].present? @@ -12,6 +15,7 @@ def index end # Return a specified participant + # params - id # GET /participants/:id def show participant = Participant.find_by(id: params[:id], user_id: params[:user_id]) @@ -42,6 +46,7 @@ def create end # Delete a specified participant + # params - id # DELETE /participants/:id def destroy participant = Participant.find(params[:id]) From 41adf6d4005990555733c739fe02468f3989f4d0 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Fri, 29 Nov 2024 15:51:28 -0500 Subject: [PATCH 33/91] Undoing "The 'show' function can now list out a specific user's participant" --- app/controllers/api/v1/participants_controller.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 9c5b0cbe0..1893e2081 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -18,13 +18,9 @@ def index # params - id # GET /participants/:id def show - participant = Participant.find_by(id: params[:id], user_id: params[:user_id]) + participant = Participant.find(params[:id]) - if participant - render json: participant, status: :ok - else - render json: { error: 'Participant not found or does not belong to the specified user' }, status: :not_found - end + render json: participant, status: :ok end # Create a participant From 79fd63801db3ae541429371312d3e38c243c6684 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Fri, 29 Nov 2024 16:02:09 -0500 Subject: [PATCH 34/91] The 'index' function can now return a list of participants for an assignment and user || Reordering private methods to line up with first usage --- .../api/v1/participants_controller.rb | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 1893e2081..2e2570a08 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -5,11 +5,15 @@ class Api::V1::ParticipantsController < ApplicationController # assignment_id # GET /participants def index - participants = if params[:user_id].present? - Participant.where(user_id: params[:user_id]).order(:id) - else - Participant.order(:id) - end + # Validate and find user if user_id is provided + user = find_user if params[:user_id].present? + return if params[:user_id].present? && user.nil? + + # Validate and find assignment if assignment_id is provided + assignment = find_assignment if params[:assignment_id].present? + return if params[:assignment_id].present? && assignment.nil? + + filter_participants(user, assignment) render json: participants, status: :ok end @@ -62,12 +66,11 @@ def participant_params private - def deletion_message(params) - 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 + def filter_participants(user, assignment) + participants = Participant.all + participants = participants.where(user_id: user.id) if user + participants = participants.where(assignment_id: assignment.id) if assignment + participants.order(:id) end def find_user @@ -82,6 +85,14 @@ def find_assignment assignment end + def deletion_message(params) + 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 + end + def build_participant(user, assignment) Participant.new(participant_params).tap do |participant| participant.user_id = user.id From c824f02b28d71f3b0e81818f95d5cb8c230e679b Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Fri, 29 Nov 2024 16:07:12 -0500 Subject: [PATCH 35/91] Updating 'routes.rb' to include the new 'index' API calls --- app/controllers/api/v1/participants_controller.rb | 6 +++--- config/routes.rb | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 2e2570a08..5b8c6ed9a 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -1,9 +1,9 @@ class Api::V1::ParticipantsController < ApplicationController # Return a list of participants for a user or assignment - # params - id - # user_id + # params - user_id # assignment_id - # GET /participants + # GET /participants/:user_id + # GET /participants/:assignment_id def index # Validate and find user if user_id is provided user = find_user if params[:user_id].present? diff --git a/config/routes.rb b/config/routes.rb index 837cc22e7..e779d1b75 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -112,7 +112,8 @@ resources :participants do collection do - get '/', to: 'participants#index' + get '/:user_id', to: 'participants#index' + get '/:assignment_id', to: 'participants#index' get '/:id', to: 'participants#show' post '/', to: 'participants#create' delete '/:id', to: 'participants#destroy' From 8784102f8067753700466ab3c092d873bb1fa1d5 Mon Sep 17 00:00:00 2001 From: augentism Date: Fri, 29 Nov 2024 23:07:52 -0500 Subject: [PATCH 36/91] removed can_submit, can_review, and handle from migrations --- db/migrate/20231026002451_add_can_submit_to_participant.rb | 5 ----- db/migrate/20231026002543_add_can_review_to_participant.rb | 5 ----- db/migrate/20231027211715_add_handle_to_participant.rb | 5 ----- db/schema.rb | 3 --- 4 files changed, 18 deletions(-) delete mode 100644 db/migrate/20231026002451_add_can_submit_to_participant.rb delete mode 100644 db/migrate/20231026002543_add_can_review_to_participant.rb delete mode 100644 db/migrate/20231027211715_add_handle_to_participant.rb diff --git a/db/migrate/20231026002451_add_can_submit_to_participant.rb b/db/migrate/20231026002451_add_can_submit_to_participant.rb deleted file mode 100644 index 5257a362f..000000000 --- a/db/migrate/20231026002451_add_can_submit_to_participant.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AddCanSubmitToParticipant < ActiveRecord::Migration[7.0] - def change - add_column :participants, :can_submit, :boolean, :default => true - end -end diff --git a/db/migrate/20231026002543_add_can_review_to_participant.rb b/db/migrate/20231026002543_add_can_review_to_participant.rb deleted file mode 100644 index 482e34b4f..000000000 --- a/db/migrate/20231026002543_add_can_review_to_participant.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AddCanReviewToParticipant < ActiveRecord::Migration[7.0] - def change - add_column :participants, :can_review, :boolean, :default => true - end -end diff --git a/db/migrate/20231027211715_add_handle_to_participant.rb b/db/migrate/20231027211715_add_handle_to_participant.rb deleted file mode 100644 index ff3399757..000000000 --- a/db/migrate/20231027211715_add_handle_to_participant.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AddHandleToParticipant < ActiveRecord::Migration[7.0] - def change - add_column :participants, :handle, :string - end -end diff --git a/db/schema.rb b/db/schema.rb index 1770d8997..8a28bff2b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -178,9 +178,6 @@ t.bigint "assignment_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.boolean "can_submit", default: true - t.boolean "can_review", default: true - t.string "handle" t.boolean "permission_granted", default: false t.bigint "join_team_request_id" t.bigint "team_id" From b928dda1012adc650aee4a09f4ff21d2ef900abe Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Fri, 29 Nov 2024 16:07:47 -0500 Subject: [PATCH 37/91] Updating 'index' method comment --- app/controllers/api/v1/participants_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 5b8c6ed9a..eecb765c5 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -1,5 +1,5 @@ class Api::V1::ParticipantsController < ApplicationController - # Return a list of participants for a user or assignment + # Return a list of participants for a given user or assignment # params - user_id # assignment_id # GET /participants/:user_id From 2698b6da870d5a36f3b3f2a9caf917fe49fcff69 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sat, 30 Nov 2024 22:04:20 -0500 Subject: [PATCH 38/91] Adding an Error Response to 'index' --- app/controllers/api/v1/participants_controller.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index eecb765c5..4b0f46e6e 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -15,7 +15,11 @@ def index filter_participants(user, assignment) - render json: participants, status: :ok + if participants.nil? + render json: participants.errors, status: :unprocessable_entity + else + render json: participants, status: :ok + end end # Return a specified participant From b33cea89e136106bacd7b7e156331ee37080d976 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sat, 30 Nov 2024 22:04:33 -0500 Subject: [PATCH 39/91] Adding an Error Response to 'show' --- app/controllers/api/v1/participants_controller.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 4b0f46e6e..9205b6564 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -28,7 +28,11 @@ def index def show participant = Participant.find(params[:id]) - render json: participant, status: :ok + if participant.nil? + render json: participant.errors, status: :unprocessable_entity + else + render json: participant, status: :created + end end # Create a participant From 7bf7973ba4c5eeb12753d78876040b28d7a4d639 Mon Sep 17 00:00:00 2001 From: augentism Date: Sun, 1 Dec 2024 17:32:24 -0500 Subject: [PATCH 40/91] seed --- db/migrate/20231026002451_add_can_submit_to_participant.rb | 5 +++++ db/migrate/20231026002543_add_can_review_to_participant.rb | 5 +++++ db/schema.rb | 2 ++ 3 files changed, 12 insertions(+) create mode 100644 db/migrate/20231026002451_add_can_submit_to_participant.rb create mode 100644 db/migrate/20231026002543_add_can_review_to_participant.rb diff --git a/db/migrate/20231026002451_add_can_submit_to_participant.rb b/db/migrate/20231026002451_add_can_submit_to_participant.rb new file mode 100644 index 000000000..166cc638a --- /dev/null +++ b/db/migrate/20231026002451_add_can_submit_to_participant.rb @@ -0,0 +1,5 @@ +class AddCanSubmitToParticipant < ActiveRecord::Migration[7.0] + def change + add_column :participants, :can_submit, :boolean, :default => true + end +end \ No newline at end of file diff --git a/db/migrate/20231026002543_add_can_review_to_participant.rb b/db/migrate/20231026002543_add_can_review_to_participant.rb new file mode 100644 index 000000000..3deac9c3c --- /dev/null +++ b/db/migrate/20231026002543_add_can_review_to_participant.rb @@ -0,0 +1,5 @@ +class AddCanReviewToParticipant < ActiveRecord::Migration[7.0] + def change + add_column :participants, :can_review, :boolean, :default => true + end +end \ No newline at end of file diff --git a/db/schema.rb b/db/schema.rb index 8a28bff2b..064c65869 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -178,6 +178,8 @@ t.bigint "assignment_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.boolean "can_submit", default: true + t.boolean "can_review", default: true t.boolean "permission_granted", default: false t.bigint "join_team_request_id" t.bigint "team_id" From 79758a837ca17cd101a0ce49b998e219138e2d1b Mon Sep 17 00:00:00 2001 From: augentism Date: Sun, 1 Dec 2024 17:43:10 -0500 Subject: [PATCH 41/91] add can_take_quiz and can_mentor --- .../20241201224112_add_can_take_quiz_to_participants.rb | 5 +++++ db/migrate/20241201224137_add_can_mentor_to_participants.rb | 5 +++++ db/schema.rb | 4 +++- 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20241201224112_add_can_take_quiz_to_participants.rb create mode 100644 db/migrate/20241201224137_add_can_mentor_to_participants.rb diff --git a/db/migrate/20241201224112_add_can_take_quiz_to_participants.rb b/db/migrate/20241201224112_add_can_take_quiz_to_participants.rb new file mode 100644 index 000000000..97dd2ea7a --- /dev/null +++ b/db/migrate/20241201224112_add_can_take_quiz_to_participants.rb @@ -0,0 +1,5 @@ +class AddCanTakeQuizToParticipants < ActiveRecord::Migration[7.0] + def change + add_column :participants, :can_take_quiz, :boolean + end +end diff --git a/db/migrate/20241201224137_add_can_mentor_to_participants.rb b/db/migrate/20241201224137_add_can_mentor_to_participants.rb new file mode 100644 index 000000000..890458682 --- /dev/null +++ b/db/migrate/20241201224137_add_can_mentor_to_participants.rb @@ -0,0 +1,5 @@ +class AddCanMentorToParticipants < ActiveRecord::Migration[7.0] + def change + add_column :participants, :can_mentor, :boolean + end +end diff --git a/db/schema.rb b/db/schema.rb index 064c65869..27c78c209 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_04_15_192048) do +ActiveRecord::Schema[7.0].define(version: 2024_12_01_224137) do create_table "account_requests", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.string "username" t.string "full_name" @@ -186,6 +186,8 @@ t.string "topic" t.string "current_stage" t.datetime "stage_deadline" + t.boolean "can_take_quiz" + t.boolean "can_mentor" t.index ["assignment_id"], name: "index_participants_on_assignment_id" t.index ["join_team_request_id"], name: "index_participants_on_join_team_request_id" t.index ["team_id"], name: "index_participants_on_team_id" From 1ca78aba6e7bf0bebaf01ba06499017e656b5e7c Mon Sep 17 00:00:00 2001 From: augentism Date: Sun, 1 Dec 2024 17:52:43 -0500 Subject: [PATCH 42/91] add delegation association to course for participant --- app/models/participant.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/models/participant.rb b/app/models/participant.rb index 75eb76ea1..cce037f0b 100644 --- a/app/models/participant.rb +++ b/app/models/participant.rb @@ -5,6 +5,8 @@ class Participant < ApplicationRecord 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 From 4423e10c4140764bb6892aa448f95b3d815f8741 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 1 Dec 2024 20:44:35 -0500 Subject: [PATCH 43/91] Changing 'create' to 'add' --- app/controllers/api/v1/participants_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 9205b6564..81120e2f4 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -37,7 +37,7 @@ def show # Create a participant # POST /participants - def create + def add user = find_user return unless user From ae5a994431f137f8539199bfd38bc609ad993c7b Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 1 Dec 2024 20:45:52 -0500 Subject: [PATCH 44/91] Moving the code from 'build_participant' method back into the 'add' method --- app/controllers/api/v1/participants_controller.rb | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 81120e2f4..b53d4e7c9 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -44,7 +44,10 @@ def add assignment = find_assignment return unless assignment - participant = build_participant(user, assignment) + Participant.new(participant_params).tap do |participant| + participant.user_id = user.id + participant.assignment_id = assignment.id + end if participant.save render json: participant, status: :created @@ -100,11 +103,4 @@ def deletion_message(params) "Participant #{params[:id]} in Team #{params[:team_id]} of Assignment #{params[:assignment_id]} has been deleted successfully!" end end - - def build_participant(user, assignment) - Participant.new(participant_params).tap do |participant| - participant.user_id = user.id - participant.assignment_id = assignment.id - end - end end From 9250d4b8c8861c87a75de6bf7ef3fca093848a98 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 1 Dec 2024 20:49:48 -0500 Subject: [PATCH 45/91] Moving the code from 'deletion_message' back into 'destroy' method --- app/controllers/api/v1/participants_controller.rb | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index b53d4e7c9..5fe22b919 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -63,7 +63,12 @@ def destroy participant = Participant.find(params[:id]) if participant.destroy - render json: { message: deletion_message(params) }, status: :ok + 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 @@ -95,12 +100,4 @@ def find_assignment render json: { error: 'Assignment not found' }, status: :not_found unless assignment assignment end - - def deletion_message(params) - 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 - end end From b1f8a2ba48fb5bbb0bd763aac4241248d7d5e987 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 1 Dec 2024 20:52:34 -0500 Subject: [PATCH 46/91] Adding new Participant parameters - 'can_submit', 'can_review', 'can_take_quiz', and 'can_mentor' --- app/controllers/api/v1/participants_controller.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 5fe22b919..6f2cd8000 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -76,7 +76,8 @@ def destroy # Permitted parameters for creating a Participant object def participant_params - params.require(:participant).permit(:user_id, :assignment_id, :team_id, :join_team_request_id, + params.require(:participant).permit(:user_id, :assignment_id, :can_submit, :can_review, :can_take_quiz, :can_mentor, + :team_id, :join_team_request_id, :permission_granted, :topic, :current_stage, :stage_deadline) end From 6b8d1dbb8118a3f37d673128f8d4d1ee8e2aa1c2 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 1 Dec 2024 20:58:26 -0500 Subject: [PATCH 47/91] Correcting 'add' route --- config/routes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index e779d1b75..4db3536a1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -115,7 +115,7 @@ get '/:user_id', to: 'participants#index' get '/:assignment_id', to: 'participants#index' get '/:id', to: 'participants#show' - post '/', to: 'participants#create' + post '/', to: 'participants#add' delete '/:id', to: 'participants#destroy' end end From b11428daa6f5043ca6618f8938df0475cd394b44 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 1 Dec 2024 21:04:26 -0500 Subject: [PATCH 48/91] Reverting from 'add' to 'create' due to redundancy in AssignmentsController --- app/controllers/api/v1/participants_controller.rb | 2 +- config/routes.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 6f2cd8000..0403f9c14 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -37,7 +37,7 @@ def show # Create a participant # POST /participants - def add + def create user = find_user return unless user diff --git a/config/routes.rb b/config/routes.rb index 4db3536a1..e779d1b75 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -115,7 +115,7 @@ get '/:user_id', to: 'participants#index' get '/:assignment_id', to: 'participants#index' get '/:id', to: 'participants#show' - post '/', to: 'participants#add' + post '/', to: 'participants#create' delete '/:id', to: 'participants#destroy' end end From 4614089ac663e5e52fed71132e9f783df7bedf36 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 1 Dec 2024 21:06:05 -0500 Subject: [PATCH 49/91] Modifying 'destroy' Method comment --- app/controllers/api/v1/participants_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 0403f9c14..68dd2108d 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -56,7 +56,7 @@ def create end end - # Delete a specified participant + # Delete a participant # params - id # DELETE /participants/:id def destroy From fa0c897fd3b584e10e70ccab54b7998edd22e2a1 Mon Sep 17 00:00:00 2001 From: mochi6 Date: Sun, 1 Dec 2024 21:10:01 -0500 Subject: [PATCH 50/91] 'participants_controller.rb' Corrections during Testing || First Iteration of Participant RSpec Tests (#3) Co-authored-by: Pierce57 --- .../api/v1/participants_controller.rb | 8 +- config/routes.rb | 4 +- spec/models/participants_controller_spec.rb | 0 .../api/v1/participants_controller_spec.rb | 111 ++++++++++++++++++ 4 files changed, 118 insertions(+), 5 deletions(-) delete mode 100644 spec/models/participants_controller_spec.rb create mode 100644 spec/requests/api/v1/participants_controller_spec.rb diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 68dd2108d..387a77691 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -13,7 +13,7 @@ def index assignment = find_assignment if params[:assignment_id].present? return if params[:assignment_id].present? && assignment.nil? - filter_participants(user, assignment) + participants = filter_participants(user, assignment) if participants.nil? render json: participants.errors, status: :unprocessable_entity @@ -91,13 +91,15 @@ def filter_participants(user, assignment) end def find_user - user = User.find_by(id: participant_params[:user_id]) + 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 def find_assignment - assignment = Assignment.find_by(id: participant_params[:assignment_id]) + 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 diff --git a/config/routes.rb b/config/routes.rb index e779d1b75..75df82d40 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -112,8 +112,8 @@ resources :participants do collection do - get '/:user_id', to: 'participants#index' - get '/:assignment_id', to: 'participants#index' + get '/user/:user_id', to: 'participants#index' + get '/assignment/:assignment_id', to: 'participants#index' get '/:id', to: 'participants#show' post '/', to: 'participants#create' delete '/:id', to: 'participants#destroy' diff --git a/spec/models/participants_controller_spec.rb b/spec/models/participants_controller_spec.rb deleted file mode 100644 index e69de29bb..000000000 diff --git a/spec/requests/api/v1/participants_controller_spec.rb b/spec/requests/api/v1/participants_controller_spec.rb new file mode 100644 index 000000000..542092bec --- /dev/null +++ b/spec/requests/api/v1/participants_controller_spec.rb @@ -0,0 +1,111 @@ +require 'swagger_helper' + +RSpec.describe 'Participants API', type: :request do + path '/api/v1/participants/user/{user_id}' do + get 'Filter participants by user' do + tags 'Participants' + produces 'application/json' + parameter name: :user_id, in: :path, type: :integer, description: 'User ID', required: true + + response '200', 'Participants found' do + let(:user) { create(:user) } + let(:user_id) { user.id } + let!(:participant) { create(:participant, user_id: user.id) } + run_test! + end + + response '404', 'User not found' do + let(:user_id) { 0 } # Nonexistent user ID + run_test! + end + end + end + + path '/api/v1/participants/assignment/{assignment_id}' do + get 'Filter participants by assignment' do + tags 'Participants' + produces 'application/json' + parameter name: :assignment_id, in: :path, type: :integer, description: 'Assignment ID', required: true + + response '200', 'Participants found' do + let(:assignment) { create(:assignment) } # Assuming FactoryBot + let(:assignment_id) { assignment.id } # Use the created assignment's ID + let!(:participant) { create(:participant, assignment_id: assignment.id) } + run_test! + end + + response '404', 'Assignment not found' do + let(:assignment_id) { 0 } # Nonexistent assignment ID + run_test! + end + end + end + + path '/api/v1/participants/{id}' do + get 'Retrieve a participant' do + tags 'Participants' + produces 'application/json' + parameter name: :id, in: :path, type: :integer, description: 'Participant ID', required: true + + response '200', 'Participant found' do + let(:participant) { create(:participant) } + let(:id) { participant.id } + + run_test! + end + response '404', 'Participant not found' do + let(:participant_id) { 0 } # Nonexistent assignment ID + run_test! + end + end + end + + + path '/api/v1/participants' do + post('create participant') do + tags 'Participants' + consumes 'application/json' + parameter name: :participant, in: :body, schema: { + type: :object, + properties: { + user_id: { type: :integer }, + assignment_id: { type: :integer }, + team_id: { type: :integer, nullable: true } + }, + required: ['user_id', 'assignment_id'] + } + + response(201, 'participant created') do + let(:user) { User.create(email: 'test@example.com', password: 'password') } + let(:assignment) { Assignment.create(title: 'Test Assignment') } + let(:participant) { { user_id: user.id, assignment_id: assignment.id } } + + run_test! + end + + response(422, 'invalid request') do + let(:participant) { { user_id: nil, assignment_id: nil } } + run_test! + end + end + end + + path '/api/v1/participants/{id}' do + delete('Delete participant') do + tags 'Participants' + parameter name: :id, in: :path, type: :integer, description: 'Participant ID', required: true + + response '200', 'Participant deleted successfully' do + let(:participant) { create(:participant) } + let(:id) { participant.id } + run_test! + end + + response '404', 'Participant not found' do + let(:id) { 0 } + run_test! + end + end + end + +end \ No newline at end of file From f26701cc4d4b3952338db6872fce6ba7eae3b526 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 1 Dec 2024 21:25:32 -0500 Subject: [PATCH 51/91] Adding the include for the 'participants_helper.rb' module to 'participants_controller.rb' --- app/controllers/api/v1/participants_controller.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 387a77691..9f1f805b6 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -1,4 +1,6 @@ class Api::V1::ParticipantsController < ApplicationController + include ParticipantsHelper + # Return a list of participants for a given user or assignment # params - user_id # assignment_id From 87af927434fa92ed4676dae640c86a372ac6d7cc Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 1 Dec 2024 21:25:49 -0500 Subject: [PATCH 52/91] Adding a 'participant_permissions' helper function to 'participants_helper.rb' --- app/helpers/participants_helper.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 app/helpers/participants_helper.rb diff --git a/app/helpers/participants_helper.rb b/app/helpers/participants_helper.rb new file mode 100644 index 000000000..b198cece2 --- /dev/null +++ b/app/helpers/participants_helper.rb @@ -0,0 +1,29 @@ +module ParticipantsHelper + # ==================================================== + # A participant can be one of the following roles: + # Reader + # Reviewer + # Submitter + # Mentor + # ==================================================== + # Grant a participant permissions to submit, review, + # take quizzes, and mentor based on their designated + # role + def participant_permissions(participant_role) + 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[participant_role]) + end +end From 275be0e593d76d3b46effa8b5e2ae8a193f819c9 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 1 Dec 2024 21:28:16 -0500 Subject: [PATCH 53/91] Changing 'create' back to 'add' --- app/controllers/api/v1/participants_controller.rb | 2 +- config/routes.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 9f1f805b6..630bf10f2 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -39,7 +39,7 @@ def show # Create a participant # POST /participants - def create + def add user = find_user return unless user diff --git a/config/routes.rb b/config/routes.rb index 75df82d40..a1444854b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -115,7 +115,7 @@ get '/user/:user_id', to: 'participants#index' get '/assignment/:assignment_id', to: 'participants#index' get '/:id', to: 'participants#show' - post '/', to: 'participants#create' + post '/', to: 'participants#add' delete '/:id', to: 'participants#destroy' end end From 29082af33887e5e46793652dd680b14f15b815f9 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 1 Dec 2024 21:28:49 -0500 Subject: [PATCH 54/91] Creating an 'AssignmentParticipant' instance in 'add' instead of a 'Participant' instance --- app/controllers/api/v1/participants_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 630bf10f2..93c361a2a 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -46,7 +46,7 @@ def add assignment = find_assignment return unless assignment - Participant.new(participant_params).tap do |participant| + AssignmentParticipant.new(participant_params).tap do |participant| participant.user_id = user.id participant.assignment_id = assignment.id end From ca8b6a4abf5a511cb51f21ac23a4233c1cb5ea43 Mon Sep 17 00:00:00 2001 From: augentism Date: Sun, 1 Dec 2024 21:47:52 -0500 Subject: [PATCH 55/91] added role to participants --- db/migrate/20241202024710_add_role_to_participants.rb | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 db/migrate/20241202024710_add_role_to_participants.rb diff --git a/db/migrate/20241202024710_add_role_to_participants.rb b/db/migrate/20241202024710_add_role_to_participants.rb new file mode 100644 index 000000000..ab0333665 --- /dev/null +++ b/db/migrate/20241202024710_add_role_to_participants.rb @@ -0,0 +1,5 @@ +class AddRoleToParticipants < ActiveRecord::Migration[7.0] + def change + add_reference :participants, :role, null: false, foreign_key: true + end +end From 69684b7e33d8924624b76da0f2180a137be4faad Mon Sep 17 00:00:00 2001 From: augentism Date: Sun, 1 Dec 2024 22:08:26 -0500 Subject: [PATCH 56/91] added role association to model --- app/models/participant.rb | 1 + db/schema.rb | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/app/models/participant.rb b/app/models/participant.rb index cce037f0b..b1cd77ccf 100644 --- a/app/models/participant.rb +++ b/app/models/participant.rb @@ -6,6 +6,7 @@ class Participant < ApplicationRecord belongs_to :team, optional: true delegate :course, to: :assignment + has_one :role, foreign_key: 'role_id' # Validations validates :user_id, presence: true diff --git a/db/schema.rb b/db/schema.rb index 27c78c209..aa7c8ba5d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_12_01_224137) do +ActiveRecord::Schema[7.0].define(version: 2024_12_02_024710) do create_table "account_requests", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.string "username" t.string "full_name" @@ -188,8 +188,10 @@ t.datetime "stage_deadline" t.boolean "can_take_quiz" t.boolean "can_mentor" + t.bigint "role_id", null: false t.index ["assignment_id"], name: "index_participants_on_assignment_id" t.index ["join_team_request_id"], name: "index_participants_on_join_team_request_id" + t.index ["role_id"], name: "index_participants_on_role_id" t.index ["team_id"], name: "index_participants_on_team_id" t.index ["user_id"], name: "fk_participant_users" t.index ["user_id"], name: "index_participants_on_user_id" @@ -340,6 +342,7 @@ add_foreign_key "courses", "users", column: "instructor_id" add_foreign_key "participants", "assignments" add_foreign_key "participants", "join_team_requests" + add_foreign_key "participants", "roles" add_foreign_key "participants", "teams" add_foreign_key "participants", "users" add_foreign_key "questions", "questionnaires" From 63a059f0994e8a3cc00206b9f1f631b97e2f841e Mon Sep 17 00:00:00 2001 From: augentism Date: Sun, 1 Dec 2024 22:15:30 -0500 Subject: [PATCH 57/91] fix role association --- app/models/participant.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/participant.rb b/app/models/participant.rb index b1cd77ccf..0be0f922b 100644 --- a/app/models/participant.rb +++ b/app/models/participant.rb @@ -6,7 +6,7 @@ class Participant < ApplicationRecord belongs_to :team, optional: true delegate :course, to: :assignment - has_one :role, foreign_key: 'role_id' + belongs_to :role # Validations validates :user_id, presence: true From a5a2aeac33f0c893f90b7874e5f8eba27f3ffe07 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 1 Dec 2024 21:32:32 -0500 Subject: [PATCH 58/91] Renaming 'participant_permissions' to 'retrieve_participant_permissions' for clarity --- app/helpers/participants_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/helpers/participants_helper.rb b/app/helpers/participants_helper.rb index b198cece2..44c680262 100644 --- a/app/helpers/participants_helper.rb +++ b/app/helpers/participants_helper.rb @@ -9,7 +9,7 @@ module ParticipantsHelper # Grant a participant permissions to submit, review, # take quizzes, and mentor based on their designated # role - def participant_permissions(participant_role) + def retrieve_participant_permissions(participant_role) default_permissions = { can_submit: true, can_review: true, From dc4ecb63da9a2fe6c9250cb61324c4cb75997c33 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 1 Dec 2024 21:52:31 -0500 Subject: [PATCH 59/91] Added Participant model validations for 'role', 'can_submit', 'can_review', 'can_take_quiz', and 'can_mentor' --- app/models/participant.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/models/participant.rb b/app/models/participant.rb index 0be0f922b..36a505b5d 100644 --- a/app/models/participant.rb +++ b/app/models/participant.rb @@ -11,6 +11,11 @@ class Participant < ApplicationRecord # Validations validates :user_id, presence: true validates :assignment_id, presence: true + validates :role, presence: true + validates :can_submit, presence: true + validates :can_review, presence: true + validates :can_take_quiz, presence: true + validates :can_mentor, presence: true # Methods def fullname From c3ee0058ebb098caa038f0cb46ca495620d7f2b9 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 1 Dec 2024 21:54:30 -0500 Subject: [PATCH 60/91] Handling Participant permissions assignment in the 'add' method || Changed 'add' API Call to '/participants/:role' so that the User can specify the desired Participant Role --- app/controllers/api/v1/participants_controller.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 93c361a2a..fc275763b 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -37,8 +37,8 @@ def show end end - # Create a participant - # POST /participants + # Assign the specified role to the participant and add them to an assignment + # POST /participants/:role def add user = find_user return unless user @@ -46,9 +46,16 @@ def add assignment = find_assignment return unless assignment + permissions = retrieve_participant_permissions(params[:role]) + AssignmentParticipant.new(participant_params).tap do |participant| participant.user_id = user.id participant.assignment_id = assignment.id + participant.role = permissions[:role] + 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] end if participant.save From 746687ac62290d6ef117456b2c6c1881ebfbb418 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 1 Dec 2024 21:55:29 -0500 Subject: [PATCH 61/91] Adding ':role' validation in the 'add' method - handled by the 'validate_role' method --- .../api/v1/participants_controller.rb | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index fc275763b..67a58e46e 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -46,7 +46,10 @@ def add assignment = find_assignment return unless assignment - permissions = retrieve_participant_permissions(params[:role]) + role = validate_role + return unless role + + permissions = retrieve_participant_permissions(role) AssignmentParticipant.new(participant_params).tap do |participant| participant.user_id = user.id @@ -58,6 +61,8 @@ def add participant.can_mentor = permissions[:can_mentor] end + assignment.add_participant(user) + if participant.save render json: participant, status: :created else @@ -112,4 +117,23 @@ def find_assignment render json: { error: 'Assignment not found' }, status: :not_found unless assignment assignment end + + def validate_role + valid_roles = %w[reader reviewer submitter mentor] + role = params[:role] + role = role.downcase if role.present? + + unless role + render json: { error: 'Role is required' }, status: :unprocessable_entity + return + end + + unless valid_roles.include?(role) + render json: { error: 'Role not valid. Valid roles are: Reader, Reviewer, Submitter, Mentor' }, + status: :unprocessable_entity + return + end + + role + end end From c9569e83cb9c138ae5bc116b1f14ac393078a1d7 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 1 Dec 2024 21:56:18 -0500 Subject: [PATCH 62/91] Adding the ':role' field to 'participant_params' --- app/controllers/api/v1/participants_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 67a58e46e..7d4786b6b 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -90,8 +90,8 @@ def destroy # Permitted parameters for creating a Participant object def participant_params - params.require(:participant).permit(:user_id, :assignment_id, :can_submit, :can_review, :can_take_quiz, :can_mentor, - :team_id, :join_team_request_id, + params.require(:participant).permit(:user_id, :assignment_id, :role, :can_submit, :can_review, + :can_take_quiz, :can_mentor, :team_id, :join_team_request_id, :permission_granted, :topic, :current_stage, :stage_deadline) end From a0f8988c4f6eafe41a15a13209b155e54e45fcbd Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 1 Dec 2024 22:00:05 -0500 Subject: [PATCH 63/91] Updated 'add' route based on API Call change --- config/routes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index a1444854b..a40a07fd7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -115,7 +115,7 @@ get '/user/:user_id', to: 'participants#index' get '/assignment/:assignment_id', to: 'participants#index' get '/:id', to: 'participants#show' - post '/', to: 'participants#add' + post '/:role', to: 'participants#add' delete '/:id', to: 'participants#destroy' end end From 8ccd45a90b7da10ea4115d1ec1db5a9163ae5d7b Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 1 Dec 2024 22:08:23 -0500 Subject: [PATCH 64/91] 'update_role' Method Implemented --- .../api/v1/participants_controller.rb | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 7d4786b6b..84953c6f7 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -70,6 +70,27 @@ def add end end + # Update the specified participant to the specified role + # PATCH /participants/:id/:role + def update_role + role = validate_role + return unless role + + permissions = retrieve_participant_permissions(role) + + participant.role = permissions[:role] + 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 From d43362fea83952905c0ffadc2add34293f82afa8 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 1 Dec 2024 22:10:42 -0500 Subject: [PATCH 65/91] Adding a route for the 'update_role' API Call --- config/routes.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/config/routes.rb b/config/routes.rb index a40a07fd7..3b903584f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -116,6 +116,7 @@ get '/assignment/:assignment_id', to: 'participants#index' get '/:id', to: 'participants#show' post '/:role', to: 'participants#add' + patch '/:id/:role', to: 'participants#update_role' delete '/:id', to: 'participants#destroy' end end From 274ac02568548bd1374cd77971d769dd5bcf66a0 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 1 Dec 2024 22:13:52 -0500 Subject: [PATCH 66/91] Adding logic to find the Participant in 'update_role' - handled in 'find_participant' function --- app/controllers/api/v1/participants_controller.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 84953c6f7..ec8c228df 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -73,6 +73,9 @@ def add # Update the specified participant to the specified role # PATCH /participants/:id/:role def update_role + participant = find_participant + return unless participant + role = validate_role return unless role @@ -139,6 +142,13 @@ def find_assignment assignment end + 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 + def validate_role valid_roles = %w[reader reviewer submitter mentor] role = params[:role] From a890807e614906fc3ac95a75d96df4c17416f09e Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 1 Dec 2024 22:23:56 -0500 Subject: [PATCH 67/91] Adding comments to the private methods in 'participants_controller.rb' --- app/controllers/api/v1/participants_controller.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index ec8c228df..7cdb8766a 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -121,6 +121,10 @@ def participant_params private + # Filters participants based on the provided user and/or assignment + # If a user is provided, participants are filtered by user_id + # If an assignment is provided, participants are filtered by assignment_id + # Returns participants ordered by their IDs def filter_participants(user, assignment) participants = Participant.all participants = participants.where(user_id: user.id) if user @@ -128,6 +132,8 @@ def filter_participants(user, 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) @@ -135,6 +141,8 @@ def find_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) @@ -142,6 +150,8 @@ def find_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) @@ -149,6 +159,8 @@ def find_participant participant end + # Validates that the role parameter is present and is one of the following valid roles: reader, reviewer, submitter, mentor + # Returns the role if valid def validate_role valid_roles = %w[reader reviewer submitter mentor] role = params[:role] From 5963dfdfef1196cb33ea46e24ecd92db2a77b3dc Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 1 Dec 2024 22:26:01 -0500 Subject: [PATCH 68/91] Small modification to 'retrieve_participant_permissions' Method comment --- app/helpers/participants_helper.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/helpers/participants_helper.rb b/app/helpers/participants_helper.rb index 44c680262..719537f21 100644 --- a/app/helpers/participants_helper.rb +++ b/app/helpers/participants_helper.rb @@ -7,8 +7,7 @@ module ParticipantsHelper # Mentor # ==================================================== # Grant a participant permissions to submit, review, - # take quizzes, and mentor based on their designated - # role + # take quizzes, and mentor based on their designated role def retrieve_participant_permissions(participant_role) default_permissions = { can_submit: true, From e3e26e1df65d6cc2257fa7b3b2f559a4cb57492c Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Sun, 1 Dec 2024 23:45:06 -0500 Subject: [PATCH 69/91] Utilizing AssignmentController's 'add_participant' method in the 'add' method of ParticipantController for Participant creation --- .../api/v1/participants_controller.rb | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 7cdb8766a..195306e72 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -51,17 +51,12 @@ def add permissions = retrieve_participant_permissions(role) - AssignmentParticipant.new(participant_params).tap do |participant| - participant.user_id = user.id - participant.assignment_id = assignment.id - participant.role = permissions[:role] - 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] - end - - assignment.add_participant(user) + participant = assignment.add_participant(user) + participant.role = permissions[:role] + 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 From 0105c4d8c0d7509e3980a390e7c61dc690bf160f Mon Sep 17 00:00:00 2001 From: augentism Date: Mon, 2 Dec 2024 00:02:10 -0500 Subject: [PATCH 70/91] re add handle to participants --- db/migrate/20231027211715_add_handle_to_participant.rb | 5 +++++ db/schema.rb | 1 + 2 files changed, 6 insertions(+) create mode 100644 db/migrate/20231027211715_add_handle_to_participant.rb diff --git a/db/migrate/20231027211715_add_handle_to_participant.rb b/db/migrate/20231027211715_add_handle_to_participant.rb new file mode 100644 index 000000000..ff3399757 --- /dev/null +++ b/db/migrate/20231027211715_add_handle_to_participant.rb @@ -0,0 +1,5 @@ +class AddHandleToParticipant < ActiveRecord::Migration[7.0] + def change + add_column :participants, :handle, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index aa7c8ba5d..f87d6625c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -180,6 +180,7 @@ t.datetime "updated_at", null: false t.boolean "can_submit", default: true t.boolean "can_review", default: true + t.string "handle" t.boolean "permission_granted", default: false t.bigint "join_team_request_id" t.bigint "team_id" From e281e331461be412c91283903d44f68f653e2095 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Mon, 2 Dec 2024 00:13:36 -0500 Subject: [PATCH 71/91] Added 'handle' to 'participant_params' --- app/controllers/api/v1/participants_controller.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 195306e72..cf3b29941 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -110,8 +110,9 @@ def destroy # Permitted parameters for creating a Participant object def participant_params params.require(:participant).permit(:user_id, :assignment_id, :role, :can_submit, :can_review, - :can_take_quiz, :can_mentor, :team_id, :join_team_request_id, - :permission_granted, :topic, :current_stage, :stage_deadline) + :can_take_quiz, :can_mentor, :handle, :team_id, + :join_team_request_id, :permission_granted, :topic, + :current_stage, :stage_deadline) end private From 06ed043d7a5189802dc10f5a7c18edf0ac237599 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Mon, 2 Dec 2024 00:43:44 -0500 Subject: [PATCH 72/91] Taking away validations for 'role', 'can_submit', 'can_review', 'can_take_quiz', and 'can_mentor' due to Participant creation approach in the 'add' method --- app/models/participant.rb | 5 ----- 1 file changed, 5 deletions(-) diff --git a/app/models/participant.rb b/app/models/participant.rb index 36a505b5d..0be0f922b 100644 --- a/app/models/participant.rb +++ b/app/models/participant.rb @@ -11,11 +11,6 @@ class Participant < ApplicationRecord # Validations validates :user_id, presence: true validates :assignment_id, presence: true - validates :role, presence: true - validates :can_submit, presence: true - validates :can_review, presence: true - validates :can_take_quiz, presence: true - validates :can_mentor, presence: true # Methods def fullname From 7ada66cf822e0856dd8c3221412a510e068b4425 Mon Sep 17 00:00:00 2001 From: augentism Date: Mon, 2 Dec 2024 01:00:13 -0500 Subject: [PATCH 73/91] change role to designation --- app/models/participant.rb | 1 - db/migrate/20241202024710_add_role_to_participants.rb | 5 ----- db/migrate/20241202055921_add_designation_to_participant.rb | 5 +++++ 3 files changed, 5 insertions(+), 6 deletions(-) delete mode 100644 db/migrate/20241202024710_add_role_to_participants.rb create mode 100644 db/migrate/20241202055921_add_designation_to_participant.rb diff --git a/app/models/participant.rb b/app/models/participant.rb index 0be0f922b..cce037f0b 100644 --- a/app/models/participant.rb +++ b/app/models/participant.rb @@ -6,7 +6,6 @@ class Participant < ApplicationRecord belongs_to :team, optional: true delegate :course, to: :assignment - belongs_to :role # Validations validates :user_id, presence: true diff --git a/db/migrate/20241202024710_add_role_to_participants.rb b/db/migrate/20241202024710_add_role_to_participants.rb deleted file mode 100644 index ab0333665..000000000 --- a/db/migrate/20241202024710_add_role_to_participants.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AddRoleToParticipants < ActiveRecord::Migration[7.0] - def change - add_reference :participants, :role, null: false, foreign_key: true - end -end diff --git a/db/migrate/20241202055921_add_designation_to_participant.rb b/db/migrate/20241202055921_add_designation_to_participant.rb new file mode 100644 index 000000000..fce46a3d1 --- /dev/null +++ b/db/migrate/20241202055921_add_designation_to_participant.rb @@ -0,0 +1,5 @@ +class AddDesignationToParticipant < ActiveRecord::Migration[7.0] + def change + add_column :participants, :designation, :string + end +end From 2516bef1bfafac8cd4580a4d151bde96b8a9c718 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Mon, 2 Dec 2024 01:03:02 -0500 Subject: [PATCH 74/91] Switched all mentions of 'role' to 'designation' in 'participants_helper.rb' --- app/helpers/participants_helper.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/helpers/participants_helper.rb b/app/helpers/participants_helper.rb index 719537f21..40dea3fc3 100644 --- a/app/helpers/participants_helper.rb +++ b/app/helpers/participants_helper.rb @@ -1,14 +1,14 @@ module ParticipantsHelper - # ==================================================== - # A participant can be one of the following roles: + # ======================================================= + # A participant can be one of the following designations: # Reader # Reviewer # Submitter # Mentor - # ==================================================== + # ======================================================= # Grant a participant permissions to submit, review, - # take quizzes, and mentor based on their designated role - def retrieve_participant_permissions(participant_role) + # take quizzes, and mentor based on their designation + def retrieve_participant_permissions(designation) default_permissions = { can_submit: true, can_review: true, @@ -23,6 +23,6 @@ def retrieve_participant_permissions(participant_role) 'mentor' => { can_mentor: true } } - default_permissions.merge(permissions_map[participant_role]) + default_permissions.merge(permissions_map[designation]) end end From e2ea10ec13b6abfed419eb6678d74ea33f72d5fc Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Mon, 2 Dec 2024 01:05:44 -0500 Subject: [PATCH 75/91] Switched all mentions of 'role' to 'designation' in 'participants_controller.rb' --- .../api/v1/participants_controller.rb | 56 +++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index cf3b29941..865e7901f 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -37,8 +37,8 @@ def show end end - # Assign the specified role to the participant and add them to an assignment - # POST /participants/:role + # Assign the specified designation to the participant and add them to an assignment + # POST /participants/:designation def add user = find_user return unless user @@ -46,13 +46,13 @@ def add assignment = find_assignment return unless assignment - role = validate_role - return unless role + designation = validate_designation + return unless designation - permissions = retrieve_participant_permissions(role) + permissions = retrieve_participant_permissions(designation) participant = assignment.add_participant(user) - participant.role = permissions[:role] + participant.designation = permissions[:designation] participant.can_submit = permissions[:can_submit] participant.can_review = permissions[:can_review] participant.can_take_quiz = permissions[:can_take_quiz] @@ -65,18 +65,18 @@ def add end end - # Update the specified participant to the specified role - # PATCH /participants/:id/:role - def update_role + # Update the specified participant to the specified designation + # PATCH /participants/:id/:designation + def update_designation participant = find_participant return unless participant - role = validate_role - return unless role + designation = validate_designation + return unless designation - permissions = retrieve_participant_permissions(role) + permissions = retrieve_participant_permissions(designation) - participant.role = permissions[:role] + participant.designation = permissions[:designation] participant.can_submit = permissions[:can_submit] participant.can_review = permissions[:can_review] participant.can_take_quiz = permissions[:can_take_quiz] @@ -109,10 +109,10 @@ def destroy # Permitted parameters for creating a Participant object def participant_params - params.require(:participant).permit(:user_id, :assignment_id, :role, :can_submit, :can_review, - :can_take_quiz, :can_mentor, :handle, :team_id, - :join_team_request_id, :permission_granted, :topic, - :current_stage, :stage_deadline) + params.require(:participant).permit(:user_id, :assignment_id, :designation, :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 @@ -155,24 +155,24 @@ def find_participant participant end - # Validates that the role parameter is present and is one of the following valid roles: reader, reviewer, submitter, mentor - # Returns the role if valid - def validate_role - valid_roles = %w[reader reviewer submitter mentor] - role = params[:role] - role = role.downcase if role.present? + # Validates that the designation parameter is present and is one of the following valid designations: reader, reviewer, submitter, mentor + # Returns the designation if valid + def validate_designation + valid_designations = %w[reader reviewer submitter mentor] + designation = params[:designation] + designation = designation.downcase if designation.present? - unless role - render json: { error: 'Role is required' }, status: :unprocessable_entity + unless designation + render json: { error: 'Designation is required' }, status: :unprocessable_entity return end - unless valid_roles.include?(role) - render json: { error: 'Role not valid. Valid roles are: Reader, Reviewer, Submitter, Mentor' }, + unless valid_designations.include?(designation) + render json: { error: 'Designation not valid. Valid designations are: Reader, Reviewer, Submitter, Mentor' }, status: :unprocessable_entity return end - role + designation end end From b53ace1159580fbaf6e5ca953ebe06f4a9876ad8 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Mon, 2 Dec 2024 01:06:23 -0500 Subject: [PATCH 76/91] Switched all mentions of 'role' to 'designation' in 'routes.rb' --- config/routes.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index 3b903584f..459e10ebd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -115,8 +115,8 @@ get '/user/:user_id', to: 'participants#index' get '/assignment/:assignment_id', to: 'participants#index' get '/:id', to: 'participants#show' - post '/:role', to: 'participants#add' - patch '/:id/:role', to: 'participants#update_role' + post '/:designation', to: 'participants#add' + patch '/:id/:designation', to: 'participants#update_designation' delete '/:id', to: 'participants#destroy' end end From 0d08fd5eeb8b59ffb2c4b181b0b79b1aa31b1106 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Mon, 2 Dec 2024 01:24:13 -0500 Subject: [PATCH 77/91] Fixing 'designation' assignment in 'add' and 'update_designation' methods --- app/controllers/api/v1/participants_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index 865e7901f..dc830ae6a 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -52,7 +52,7 @@ def add permissions = retrieve_participant_permissions(designation) participant = assignment.add_participant(user) - participant.designation = permissions[:designation] + participant.designation = designation participant.can_submit = permissions[:can_submit] participant.can_review = permissions[:can_review] participant.can_take_quiz = permissions[:can_take_quiz] @@ -76,7 +76,7 @@ def update_designation permissions = retrieve_participant_permissions(designation) - participant.designation = permissions[:designation] + participant.designation = designation participant.can_submit = permissions[:can_submit] participant.can_review = permissions[:can_review] participant.can_take_quiz = permissions[:can_take_quiz] From 09963adb34fc2491455b38d9380b99ebf8889aea Mon Sep 17 00:00:00 2001 From: mochi6 Date: Mon, 2 Dec 2024 01:29:11 -0500 Subject: [PATCH 78/91] "Fixed the swagger UI test code" --- .../api/v1/participants_controller_spec.rb | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/spec/requests/api/v1/participants_controller_spec.rb b/spec/requests/api/v1/participants_controller_spec.rb index 542092bec..c1b143ed9 100644 --- a/spec/requests/api/v1/participants_controller_spec.rb +++ b/spec/requests/api/v1/participants_controller_spec.rb @@ -61,35 +61,63 @@ end - path '/api/v1/participants' do - post('create participant') do + path '/api/v1/participants/{designation}' do + post('add participant') do tags 'Participants' consumes 'application/json' + parameter name: :designation, in: :path, type: :string, description: 'designation of the participant' parameter name: :participant, in: :body, schema: { type: :object, properties: { user_id: { type: :integer }, assignment_id: { type: :integer }, - team_id: { type: :integer, nullable: true } + team_id: { type: :integer, nullable: true }, }, - required: ['user_id', 'assignment_id'] + required: ['user_id', 'assignment_id'] # Add designation to required fields } response(201, 'participant created') do let(:user) { User.create(email: 'test@example.com', password: 'password') } let(:assignment) { Assignment.create(title: 'Test Assignment') } + let(:designation) { 'student' } let(:participant) { { user_id: user.id, assignment_id: assignment.id } } run_test! end response(422, 'invalid request') do + let(:designation) { 'invalid_designation' } let(:participant) { { user_id: nil, assignment_id: nil } } run_test! end end end + path '/api/v1/participants/{id}/{designation}' do + patch('update participant designation') do + tags 'Participants' + consumes 'application/json' + parameter name: :id, in: :path, type: :integer, description: 'Participant ID' + parameter name: :designation, in: :path, type: :string, description: 'New designation for the participant' + + response(201, 'participant designation updated') do + let(:user) { User.create(email: 'test@example.com', password: 'password') } + let(:assignment) { Assignment.create(title: 'Test Assignment') } + let(:participant) { AssignmentParticipant.create(user_id: user.id, assignment_id: assignment.id, designation: 'student') } + let(:id) { participant.id } + let(:designation) { 'mentor' } + + run_test! + end + + response(422, 'invalid request') do + let(:id) { 0 } + let(:designation) { 'invalid_designation' } + run_test! + end + end + end + path '/api/v1/participants/{id}' do delete('Delete participant') do tags 'Participants' From fac9cd5a4521b57da0aab79f1ef60b96754847bf Mon Sep 17 00:00:00 2001 From: augentism Date: Mon, 2 Dec 2024 11:54:02 -0500 Subject: [PATCH 79/91] change designation to authorization --- db/migrate/20241202055921_add_designation_to_participant.rb | 5 ----- .../20241202165201_add_authorization_to_participants.rb | 5 +++++ db/schema.rb | 6 ++---- 3 files changed, 7 insertions(+), 9 deletions(-) delete mode 100644 db/migrate/20241202055921_add_designation_to_participant.rb create mode 100644 db/migrate/20241202165201_add_authorization_to_participants.rb diff --git a/db/migrate/20241202055921_add_designation_to_participant.rb b/db/migrate/20241202055921_add_designation_to_participant.rb deleted file mode 100644 index fce46a3d1..000000000 --- a/db/migrate/20241202055921_add_designation_to_participant.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AddDesignationToParticipant < ActiveRecord::Migration[7.0] - def change - add_column :participants, :designation, :string - end -end diff --git a/db/migrate/20241202165201_add_authorization_to_participants.rb b/db/migrate/20241202165201_add_authorization_to_participants.rb new file mode 100644 index 000000000..3ca665d5f --- /dev/null +++ b/db/migrate/20241202165201_add_authorization_to_participants.rb @@ -0,0 +1,5 @@ +class AddAuthorizationToParticipants < ActiveRecord::Migration[7.0] + def change + add_column :participants, :authorization, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index f87d6625c..e50704472 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_12_02_024710) do +ActiveRecord::Schema[7.0].define(version: 2024_12_02_165201) do create_table "account_requests", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.string "username" t.string "full_name" @@ -189,10 +189,9 @@ t.datetime "stage_deadline" t.boolean "can_take_quiz" t.boolean "can_mentor" - t.bigint "role_id", null: false + t.string "authorization" t.index ["assignment_id"], name: "index_participants_on_assignment_id" t.index ["join_team_request_id"], name: "index_participants_on_join_team_request_id" - t.index ["role_id"], name: "index_participants_on_role_id" t.index ["team_id"], name: "index_participants_on_team_id" t.index ["user_id"], name: "fk_participant_users" t.index ["user_id"], name: "index_participants_on_user_id" @@ -343,7 +342,6 @@ add_foreign_key "courses", "users", column: "instructor_id" add_foreign_key "participants", "assignments" add_foreign_key "participants", "join_team_requests" - add_foreign_key "participants", "roles" add_foreign_key "participants", "teams" add_foreign_key "participants", "users" add_foreign_key "questions", "questionnaires" From d9bea2ea1b5ea05e197bba67701699927d3a1dd0 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Mon, 2 Dec 2024 16:28:23 -0500 Subject: [PATCH 80/91] Switched all mentions of 'designation' to 'authorization' in 'routes.rb' --- config/routes.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index 459e10ebd..88b047dbd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -115,8 +115,8 @@ get '/user/:user_id', to: 'participants#index' get '/assignment/:assignment_id', to: 'participants#index' get '/:id', to: 'participants#show' - post '/:designation', to: 'participants#add' - patch '/:id/:designation', to: 'participants#update_designation' + post '/:authorization', to: 'participants#add' + patch '/:id/:authorization', to: 'participants#update_authorization' delete '/:id', to: 'participants#destroy' end end From 57eb1d17bc89e81f8a016e2d690a3a63f06fe471 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Mon, 2 Dec 2024 16:29:10 -0500 Subject: [PATCH 81/91] Switched all mentions of 'designation' to 'authorization' in 'participants_helper.rb' --- app/helpers/participants_helper.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/helpers/participants_helper.rb b/app/helpers/participants_helper.rb index 40dea3fc3..6a3e7011a 100644 --- a/app/helpers/participants_helper.rb +++ b/app/helpers/participants_helper.rb @@ -1,14 +1,14 @@ module ParticipantsHelper - # ======================================================= - # A participant can be one of the following designations: + # =========================================================== + # 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 designation - def retrieve_participant_permissions(designation) + # take quizzes, and mentor based on their authorization + def retrieve_participant_permissions(authorization) default_permissions = { can_submit: true, can_review: true, @@ -23,6 +23,6 @@ def retrieve_participant_permissions(designation) 'mentor' => { can_mentor: true } } - default_permissions.merge(permissions_map[designation]) + default_permissions.merge(permissions_map[authorization]) end end From a28327869a178813a03d56134592195788add947 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Mon, 2 Dec 2024 16:31:10 -0500 Subject: [PATCH 82/91] Switched all mentions of 'designation' to 'authorization' in 'participants_controller.rb' --- .../api/v1/participants_controller.rb | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index dc830ae6a..d68fabf4f 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -37,8 +37,8 @@ def show end end - # Assign the specified designation to the participant and add them to an assignment - # POST /participants/:designation + # Assign the specified authorization to the participant and add them to an assignment + # POST /participants/:authorization def add user = find_user return unless user @@ -46,13 +46,13 @@ def add assignment = find_assignment return unless assignment - designation = validate_designation - return unless designation + authorization = validate_authorization + return unless authorization - permissions = retrieve_participant_permissions(designation) + permissions = retrieve_participant_permissions(authorization) participant = assignment.add_participant(user) - participant.designation = designation + participant.authorization = authorization participant.can_submit = permissions[:can_submit] participant.can_review = permissions[:can_review] participant.can_take_quiz = permissions[:can_take_quiz] @@ -65,18 +65,18 @@ def add end end - # Update the specified participant to the specified designation - # PATCH /participants/:id/:designation - def update_designation + # Update the specified participant to the specified authorization + # PATCH /participants/:id/:authorization + def update_authorization participant = find_participant return unless participant - designation = validate_designation - return unless designation + authorization = validate_authorization + return unless authorization - permissions = retrieve_participant_permissions(designation) + permissions = retrieve_participant_permissions(authorization) - participant.designation = designation + participant.authorization = authorization participant.can_submit = permissions[:can_submit] participant.can_review = permissions[:can_review] participant.can_take_quiz = permissions[:can_take_quiz] @@ -109,7 +109,7 @@ def destroy # Permitted parameters for creating a Participant object def participant_params - params.require(:participant).permit(:user_id, :assignment_id, :designation, :can_submit, + 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) @@ -155,24 +155,24 @@ def find_participant participant end - # Validates that the designation parameter is present and is one of the following valid designations: reader, reviewer, submitter, mentor - # Returns the designation if valid - def validate_designation - valid_designations = %w[reader reviewer submitter mentor] - designation = params[:designation] - designation = designation.downcase if designation.present? + # 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 designation - render json: { error: 'Designation is required' }, status: :unprocessable_entity + unless authorization + render json: { error: 'authorization is required' }, status: :unprocessable_entity return end - unless valid_designations.include?(designation) - render json: { error: 'Designation not valid. Valid designations are: Reader, Reviewer, Submitter, Mentor' }, + unless valid_authorizations.include?(authorization) + render json: { error: 'authorization not valid. Valid authorizations are: Reader, Reviewer, Submitter, Mentor' }, status: :unprocessable_entity return end - designation + authorization end end From a3f36ab561ed3d6c0f9d3ab004064f6fd03c5929 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Mon, 2 Dec 2024 16:46:37 -0500 Subject: [PATCH 83/91] Changing 'index' Routes to point to new 'user_index' and 'assignment_index' Methods --- config/routes.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index 88b047dbd..e5d805c4f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -112,8 +112,8 @@ resources :participants do collection do - get '/user/:user_id', to: 'participants#index' - get '/assignment/:assignment_id', to: 'participants#index' + 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' From 9695c90b7943e27fa55cde1346a96c4ee373f491 Mon Sep 17 00:00:00 2001 From: Pierce57 Date: Mon, 2 Dec 2024 16:50:36 -0500 Subject: [PATCH 84/91] Split the 'index' Method into 'user_index' and 'assignment_index' || Split the 'filter_participants' Method into 'filter_user_participants' and 'filter_assignment_participants' --- .../api/v1/participants_controller.rb | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index d68fabf4f..eaeaee6ab 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -1,21 +1,30 @@ class Api::V1::ParticipantsController < ApplicationController include ParticipantsHelper - # Return a list of participants for a given user or assignment + # Return a list of participants for a given user # params - user_id - # assignment_id - # GET /participants/:user_id - # GET /participants/:assignment_id - def index - # Validate and find user if user_id is provided + # GET /participants/user/:user_id + def user_index user = find_user if params[:user_id].present? return if params[:user_id].present? && user.nil? - # Validate and find assignment if assignment_id is provided + 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_participants(user, assignment) + participants = filter_assignment_participants(assignment) if participants.nil? render json: participants.errors, status: :unprocessable_entity @@ -117,13 +126,18 @@ def participant_params private - # Filters participants based on the provided user and/or assignment - # If a user is provided, participants are filtered by user_id - # If an assignment is provided, participants are filtered by assignment_id + # Filters participants based on the provided user # Returns participants ordered by their IDs - def filter_participants(user, assignment) + 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 From 620c372bbc3e7da69e52a6dd99ce2805556821b0 Mon Sep 17 00:00:00 2001 From: mochi6 Date: Mon, 2 Dec 2024 17:47:46 -0500 Subject: [PATCH 85/91] "Updated the test cases to align with the current implementation" --- .../api/v1/participants_controller_spec.rb | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/spec/requests/api/v1/participants_controller_spec.rb b/spec/requests/api/v1/participants_controller_spec.rb index c1b143ed9..d2712c798 100644 --- a/spec/requests/api/v1/participants_controller_spec.rb +++ b/spec/requests/api/v1/participants_controller_spec.rb @@ -61,11 +61,11 @@ end - path '/api/v1/participants/{designation}' do + path '/api/v1/participants/{authorization}' do post('add participant') do tags 'Participants' consumes 'application/json' - parameter name: :designation, in: :path, type: :string, description: 'designation of the participant' + parameter name: :authorization, in: :path, type: :string, description: 'authorization of the participant' parameter name: :participant, in: :body, schema: { type: :object, properties: { @@ -73,46 +73,46 @@ assignment_id: { type: :integer }, team_id: { type: :integer, nullable: true }, }, - required: ['user_id', 'assignment_id'] # Add designation to required fields + required: ['user_id', 'assignment_id'] # Add authorization to required fields } response(201, 'participant created') do let(:user) { User.create(email: 'test@example.com', password: 'password') } let(:assignment) { Assignment.create(title: 'Test Assignment') } - let(:designation) { 'student' } + let(:authorization) { 'student' } let(:participant) { { user_id: user.id, assignment_id: assignment.id } } run_test! end response(422, 'invalid request') do - let(:designation) { 'invalid_designation' } + let(:authorization) { 'invalid_authorization' } let(:participant) { { user_id: nil, assignment_id: nil } } run_test! end end end - path '/api/v1/participants/{id}/{designation}' do - patch('update participant designation') do + path '/api/v1/participants/{id}/{authorization}' do + patch('update participant authorization') do tags 'Participants' consumes 'application/json' parameter name: :id, in: :path, type: :integer, description: 'Participant ID' - parameter name: :designation, in: :path, type: :string, description: 'New designation for the participant' + parameter name: :authorization, in: :path, type: :string, description: 'New authorization for the participant' - response(201, 'participant designation updated') do + response(201, 'participant authorization updated') do let(:user) { User.create(email: 'test@example.com', password: 'password') } let(:assignment) { Assignment.create(title: 'Test Assignment') } - let(:participant) { AssignmentParticipant.create(user_id: user.id, assignment_id: assignment.id, designation: 'student') } + let(:participant) { AssignmentParticipant.create(user_id: user.id, assignment_id: assignment.id, authorization: 'student') } let(:id) { participant.id } - let(:designation) { 'mentor' } + let(:authorization) { 'mentor' } run_test! end response(422, 'invalid request') do let(:id) { 0 } - let(:designation) { 'invalid_designation' } + let(:authorization) { 'invalid_authorization' } run_test! end end From 9ea1701c4f6af3209b8bbc038fc7654ac52878d7 Mon Sep 17 00:00:00 2001 From: mochi6 Date: Tue, 3 Dec 2024 11:41:56 -0500 Subject: [PATCH 86/91] "Updated the destroy method in the controller to cover participant nil edge case, finished rswag tests and tests all passes" --- .../api/v1/participants_controller.rb | 8 +- .../api/v1/participants_controller_spec.rb | 332 +++++++++++++----- 2 files changed, 257 insertions(+), 83 deletions(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index eaeaee6ab..a3f68a304 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -102,9 +102,11 @@ def update_authorization # params - id # DELETE /participants/:id def destroy - participant = Participant.find(params[:id]) - - if participant.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 diff --git a/spec/requests/api/v1/participants_controller_spec.rb b/spec/requests/api/v1/participants_controller_spec.rb index d2712c798..27ee7022b 100644 --- a/spec/requests/api/v1/participants_controller_spec.rb +++ b/spec/requests/api/v1/participants_controller_spec.rb @@ -1,139 +1,311 @@ require 'swagger_helper' RSpec.describe 'Participants API', type: :request do + before(:all) do + # Log in and retrieve the token once before all tests + post '/login', params: { user_name: 'admin', password: 'password123' }, headers: { 'Host' => 'localhost:3002' } + expect(response.status).to eq(200) + @token = JSON.parse(response.body)['token'] + end + path '/api/v1/participants/user/{user_id}' do - get 'Filter participants by user' do + get 'Retrieve participants for a specific user' do tags 'Participants' produces 'application/json' - parameter name: :user_id, in: :path, type: :integer, description: 'User ID', required: true - response '200', 'Participants found' do - let(:user) { create(:user) } - let(:user_id) { user.id } - let!(:participant) { create(:participant, user_id: user.id) } - run_test! + parameter name: :user_id, in: :path, type: :integer, description: 'ID of the user' + parameter name: 'Authorization', in: :header, type: :string, required: true, description: 'Bearer token' + + response '200', 'Returns participants' do + let(:user_id) { 4 } + let(:'Authorization') { "Bearer #{@token}" } + + run_test! do |response| + data = JSON.parse(response.body) + participant = data[0] + expect(participant).to be_a(Hash) + expect(participant['id']).to eq(1) + expect(participant['user_id']).to eq(4) + expect(participant['assignment_id']).to eq(1) + end end - response '404', 'User not found' do - let(:user_id) { 0 } # Nonexistent user ID - run_test! + response '404', 'Participant Not Found' do + let(:user_id) { 99 } + let(:'Authorization') { "Bearer #{@token}" } + + run_test! do |response| + expect(JSON.parse(response.body)['error']).to eql('User not found') + end + end + + response '401', 'Unauthorized' do + let(:user_id) { 1 } + let(:'Authorization') { 'Bearer invalid_token' } + + run_test! do |response| + expect(JSON.parse(response.body)['error']).to eql('Not Authorized') + end end end end - + path '/api/v1/participants/assignment/{assignment_id}' do - get 'Filter participants by assignment' do + get 'Retrieve participants for a specific assignment' do tags 'Participants' produces 'application/json' - parameter name: :assignment_id, in: :path, type: :integer, description: 'Assignment ID', required: true - response '200', 'Participants found' do - let(:assignment) { create(:assignment) } # Assuming FactoryBot - let(:assignment_id) { assignment.id } # Use the created assignment's ID - let!(:participant) { create(:participant, assignment_id: assignment.id) } - run_test! + parameter name: :assignment_id, in: :path, type: :integer, description: 'ID of the assignment' + parameter name: 'Authorization', in: :header, type: :string, required: true, description: 'Bearer token' + + response '200', 'Returns participants' do + let(:assignment_id) { 2 } + let(:'Authorization') { "Bearer #{@token}" } + + run_test! do |response| + data = JSON.parse(response.body) + participant = data[0] + expect(participant).to be_a(Hash) + expect(participant['id']).to eq(2) + expect(participant['user_id']).to eq(5) + expect(participant['assignment_id']).to eq(2) + end end - response '404', 'Assignment not found' do - let(:assignment_id) { 0 } # Nonexistent assignment ID - run_test! + response '404', 'Participant Not Found' do + let(:assignment_id) { 99 } + let(:'Authorization') { "Bearer #{@token}" } + + run_test! do |response| + expect(JSON.parse(response.body)['error']).to eql('Assignment not found') + end + end + + response '401', 'Unauthorized' do + let(:assignment_id) { 2 } + let(:'Authorization') { 'Bearer invalid_token' } + + run_test! do |response| + expect(JSON.parse(response.body)['error']).to eql('Not Authorized') + end end end end path '/api/v1/participants/{id}' do - get 'Retrieve a participant' do + get 'Retrieve a specific participant' do tags 'Participants' produces 'application/json' - parameter name: :id, in: :path, type: :integer, description: 'Participant ID', required: true - response '200', 'Participant found' do - let(:participant) { create(:participant) } - let(:id) { participant.id } + parameter name: :id, in: :path, type: :integer, description: 'ID of the participant' + parameter name: 'Authorization', in: :header, type: :string, required: true, description: 'Bearer token' + + response '201', 'Returns a participant' do + let(:id) { 2 } + let(:'Authorization') { "Bearer #{@token}" } + + run_test! do |response| + data = JSON.parse(response.body) + expect(data['user_id']).to eq(5) + expect(data['assignment_id']).to eq(2) + end + end + + response '404', 'Participant not found' do + let(:id) { 99 } + let(:'Authorization') { "Bearer #{@token}" } + + run_test! do |response| + expect(JSON.parse(response.body)['error']).to eql('Not Found') + end + end - run_test! + response '401', 'Unauthorized' do + let(:id) { 2 } + let(:'Authorization') { 'Bearer invalid_token' } + + run_test! do |response| + expect(JSON.parse(response.body)['error']).to eql('Not Authorized') + end end + end + + delete 'Delete a specific participant' do + tags 'Participants' + parameter name: :id, in: :path, type: :integer, description: 'ID of the participant' + parameter name: 'Authorization', in: :header, type: :string, required: true, description: 'Bearer token' + + response '200', 'Participant deleted' do + let(:id) { 2 } + let(:'Authorization') { "Bearer #{@token}" } + + run_test! do |response| + expect(JSON.parse(response.body)['message']).to include('Participant') + end + end + response '404', 'Participant not found' do - let(:participant_id) { 0 } # Nonexistent assignment ID - run_test! + let(:id) { 99 } + let(:'Authorization') { "Bearer #{@token}" } + + run_test! do |response| + expect(JSON.parse(response.body)['error']).to eql('Not Found') + end + end + + response '401', 'Unauthorized' do + let(:id) { 2 } + let(:'Authorization') { 'Bearer invalid_token' } + + run_test! do |response| + expect(JSON.parse(response.body)['error']).to eql('Not Authorized') + end + end + end + end + + path '/api/v1/participants/{id}/{authorization}' do + patch 'Update participant authorization' do + tags 'Participants' + consumes 'application/json' + produces 'application/json' + + parameter name: :id, in: :path, type: :integer, description: 'ID of the participant' + parameter name: :authorization, in: :path, type: :string, description: 'New authorization' + parameter name: 'Authorization', in: :header, type: :string, required: true, description: 'Bearer token' + + response '201', 'Participant updated' do + let(:id) { 2 } + let(:authorization) { 'mentor' } + let(:'Authorization') { "Bearer #{@token}" } + + run_test! do |response| + data = JSON.parse(response.body) + expect(data['authorization']).to eq('mentor') + end + end + + response '404', 'Participant not found' do + let(:id) { 99 } + let(:authorization) { 'mentor' } + let(:'Authorization') { "Bearer #{@token}" } + + run_test! do |response| + expect(JSON.parse(response.body)['error']).to eql('Participant not found') + end + end + + response '404', 'Participant not found' do + let(:id) { 99 } + let(:authorization) { 'teacher' } + let(:'Authorization') { "Bearer #{@token}" } + + run_test! do |response| + expect(JSON.parse(response.body)['error']).to eql('Participant not found') + end + end + + response '422', 'Role not found' do + let(:id) { 1 } + let(:authorization) { 'teacher' } + let(:'Authorization') { "Bearer #{@token}" } + + run_test! do |response| + expect(JSON.parse(response.body)['error']).to eql('authorization not valid. Valid authorizations are: Reader, Reviewer, Submitter, Mentor') + end + end + + response '401', 'Unauthorized' do + let(:id) { 2 } + let(:authorization) { 'mentor' } + let(:'Authorization') { 'Bearer invalid_token' } + + run_test! do |response| + expect(JSON.parse(response.body)['error']).to eql('Not Authorized') + end end end end - path '/api/v1/participants/{authorization}' do - post('add participant') do + post 'Add a participant' do tags 'Participants' consumes 'application/json' - parameter name: :authorization, in: :path, type: :string, description: 'authorization of the participant' + produces 'application/json' + + parameter name: :authorization, in: :path, type: :string, description: 'Authorization level (Reader, Reviewer, Submitter, Mentor)' + parameter name: 'Authorization', in: :header, type: :string, required: true, description: 'Bearer token' parameter name: :participant, in: :body, schema: { type: :object, properties: { - user_id: { type: :integer }, - assignment_id: { type: :integer }, - team_id: { type: :integer, nullable: true }, + user_id: { type: :integer, description: 'ID of the user' }, + assignment_id: { type: :integer, description: 'ID of the assignment' } }, - required: ['user_id', 'assignment_id'] # Add authorization to required fields + required: %w[user_id assignment_id] } - response(201, 'participant created') do - let(:user) { User.create(email: 'test@example.com', password: 'password') } - let(:assignment) { Assignment.create(title: 'Test Assignment') } - let(:authorization) { 'student' } - let(:participant) { { user_id: user.id, assignment_id: assignment.id } } + response '201', 'Participant successfully added' do + let(:authorization) { 'mentor' } + let(:'Authorization') { "Bearer #{@token}" } + let(:participant) { { user_id: 3, assignment_id: 1 } } - run_test! + run_test! do |response| + data = JSON.parse(response.body) + expect(data['user_id']).to eq(3) + expect(data['assignment_id']).to eq(1) + expect(data['authorization']).to eq('mentor') + end end + + response '500', 'Participant already exist' do + let(:authorization) { 'mentor' } + let(:'Authorization') { "Bearer #{@token}" } + let(:participant) { { user_id: 4, assignment_id: 1 } } - response(422, 'invalid request') do - let(:authorization) { 'invalid_authorization' } - let(:participant) { { user_id: nil, assignment_id: nil } } - run_test! + run_test! do |response| + expect(JSON.parse(response.body)['exception']).to eq('#') + end end - end - end - - path '/api/v1/participants/{id}/{authorization}' do - patch('update participant authorization') do - tags 'Participants' - consumes 'application/json' - parameter name: :id, in: :path, type: :integer, description: 'Participant ID' - parameter name: :authorization, in: :path, type: :string, description: 'New authorization for the participant' - response(201, 'participant authorization updated') do - let(:user) { User.create(email: 'test@example.com', password: 'password') } - let(:assignment) { Assignment.create(title: 'Test Assignment') } - let(:participant) { AssignmentParticipant.create(user_id: user.id, assignment_id: assignment.id, authorization: 'student') } - let(:id) { participant.id } + response '404', 'User not found' do let(:authorization) { 'mentor' } + let(:'Authorization') { "Bearer #{@token}" } + let(:participant) { { user_id: 99, assignment_id: 1 } } - run_test! + run_test! do |response| + expect(JSON.parse(response.body)['error']).to eq('User not found') + end end - response(422, 'invalid request') do - let(:id) { 0 } - let(:authorization) { 'invalid_authorization' } - run_test! + response '404', 'Assignment not found' do + let(:authorization) { 'mentor' } + let(:'Authorization') { "Bearer #{@token}" } + let(:participant) { { user_id: 3, assignment_id: 99 } } + + run_test! do |response| + expect(JSON.parse(response.body)['error']).to eq('Assignment not found') + end end - end - end - path '/api/v1/participants/{id}' do - delete('Delete participant') do - tags 'Participants' - parameter name: :id, in: :path, type: :integer, description: 'Participant ID', required: true + response '422', 'Role not found' do + let(:authorization) { 'teacher' } + let(:'Authorization') { "Bearer #{@token}" } + let(:participant) { { user_id: 3, assignment_id: 1 } } - response '200', 'Participant deleted successfully' do - let(:participant) { create(:participant) } - let(:id) { participant.id } - run_test! + run_test! do |response| + expect(JSON.parse(response.body)['error']).to eql('authorization not valid. Valid authorizations are: Reader, Reviewer, Submitter, Mentor') + end end - - response '404', 'Participant not found' do - let(:id) { 0 } - run_test! + + response '422', 'Invalid authorization' do + let(:authorization) { 'invalid_auth' } + let(:'Authorization') { "Bearer #{@token}" } + let(:participant) { { user_id: 3, assignment_id: 1 } } + + run_test! do |response| + expect(JSON.parse(response.body)['error']).to include('authorization not valid') + end end end end - end \ No newline at end of file From f500002cb16a59831e64018996ffc56c7e936278 Mon Sep 17 00:00:00 2001 From: mochi6 Date: Tue, 3 Dec 2024 11:59:42 -0500 Subject: [PATCH 87/91] "added one more test case" --- .../api/v1/participants_controller_spec.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/spec/requests/api/v1/participants_controller_spec.rb b/spec/requests/api/v1/participants_controller_spec.rb index 27ee7022b..11bfebc68 100644 --- a/spec/requests/api/v1/participants_controller_spec.rb +++ b/spec/requests/api/v1/participants_controller_spec.rb @@ -30,7 +30,18 @@ end end - response '404', 'Participant Not Found' do + response '200', 'Participant not found with user_id' do + let(:user_id) { 1 } + let(:'Authorization') { "Bearer #{@token}" } + + run_test! do |response| + data = JSON.parse(response.body) + expect(data).to be_an(Array) + expect(data).to be_empty + end + end + + response '404', 'User Not Found' do let(:user_id) { 99 } let(:'Authorization') { "Bearer #{@token}" } @@ -72,7 +83,7 @@ end end - response '404', 'Participant Not Found' do + response '404', 'Assignment Not Found' do let(:assignment_id) { 99 } let(:'Authorization') { "Bearer #{@token}" } From 5055f2bffe84c72afd6e604e85e0c62943d1939e Mon Sep 17 00:00:00 2001 From: mochi6 Date: Tue, 3 Dec 2024 13:23:17 -0500 Subject: [PATCH 88/91] "updated the names for a few tests" --- spec/requests/api/v1/participants_controller_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/requests/api/v1/participants_controller_spec.rb b/spec/requests/api/v1/participants_controller_spec.rb index 11bfebc68..571cd120a 100644 --- a/spec/requests/api/v1/participants_controller_spec.rb +++ b/spec/requests/api/v1/participants_controller_spec.rb @@ -216,7 +216,7 @@ end end - response '422', 'Role not found' do + response '422', 'Authorization not found' do let(:id) { 1 } let(:authorization) { 'teacher' } let(:'Authorization') { "Bearer #{@token}" } @@ -298,7 +298,7 @@ end end - response '422', 'Role not found' do + response '422', 'Authorization not found' do let(:authorization) { 'teacher' } let(:'Authorization') { "Bearer #{@token}" } let(:participant) { { user_id: 3, assignment_id: 1 } } From ecfe92d4023fdb53e64f3a0e96ffe3e0d09c1ddd Mon Sep 17 00:00:00 2001 From: mochi6 Date: Tue, 3 Dec 2024 16:04:37 -0500 Subject: [PATCH 89/91] "Updated the Post test, added a new method to dynamically find user name" --- spec/requests/api/v1/participants_controller_spec.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/spec/requests/api/v1/participants_controller_spec.rb b/spec/requests/api/v1/participants_controller_spec.rb index 571cd120a..36f933031 100644 --- a/spec/requests/api/v1/participants_controller_spec.rb +++ b/spec/requests/api/v1/participants_controller_spec.rb @@ -268,13 +268,19 @@ end end + def fetch_username(user_id) + User.find(user_id).name + end + response '500', 'Participant already exist' do let(:authorization) { 'mentor' } let(:'Authorization') { "Bearer #{@token}" } let(:participant) { { user_id: 4, assignment_id: 1 } } + let(:name) { User.find(participant[:user_id]).name } run_test! do |response| - expect(JSON.parse(response.body)['exception']).to eq('#') + + expect(JSON.parse(response.body)['exception']).to eq("#") end end From 89e93c19f3fc83442a09d8a71c99e7fb28b8d6f9 Mon Sep 17 00:00:00 2001 From: Sahithi Ammana <112002925+SahithiAmmana@users.noreply.github.com> Date: Thu, 23 Jan 2025 00:21:58 -0500 Subject: [PATCH 90/91] Add action_allowed and rename few methods --- app/controllers/api/v1/participants_controller.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index a3f68a304..d8b05dc68 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -1,10 +1,15 @@ class Api::V1::ParticipantsController < ApplicationController include ParticipantsHelper + # Returns true if the user has TA privileges; otherwise, denies access by returning false. + def action_allowed? + current_user_has_ta_privileges? + end + # Return a list of participants for a given user # params - user_id # GET /participants/user/:user_id - def user_index + def list_user_participants user = find_user if params[:user_id].present? return if params[:user_id].present? && user.nil? @@ -20,7 +25,7 @@ def user_index # Return a list of participants for a given assignment # params - assignment_id # GET /participants/assignment/:assignment_id - def assignment_index + def list_assignment_participants assignment = find_assignment if params[:assignment_id].present? return if params[:assignment_id].present? && assignment.nil? From 6d065254ce0de3c5b6454ce0951795d26f9dcef4 Mon Sep 17 00:00:00 2001 From: Sahithi Ammana <112002925+SahithiAmmana@users.noreply.github.com> Date: Fri, 24 Jan 2025 14:24:57 -0500 Subject: [PATCH 91/91] Update participants_controller.rb --- app/controllers/api/v1/participants_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/v1/participants_controller.rb b/app/controllers/api/v1/participants_controller.rb index d8b05dc68..675ae7a1e 100644 --- a/app/controllers/api/v1/participants_controller.rb +++ b/app/controllers/api/v1/participants_controller.rb @@ -3,7 +3,7 @@ class Api::V1::ParticipantsController < ApplicationController # Returns true if the user has TA privileges; otherwise, denies access by returning false. def action_allowed? - current_user_has_ta_privileges? + has_required_role?('Teaching Assistant') end # Return a list of participants for a given user