CSC/ECE 517 Fall 2023 - E2370. Reimplement join team requests controller

From Expertiza_Wiki
Jump to navigation Jump to search

Expertiza

Expertiza is a Ruby on Rails based open source project. Instructors have the ability to add new projects, assignments, etc., as well as edit existing ones. Later on, they can view student submissions and grade them. Students can also use Expertiza to organize into teams to work on different projects and assignments and submit their work. They can also review other students' submissions.

Problem Statement

The controller join_team_requests_controller.rb contains functions to create, list, update, delete, and decline requests to join teams. This is a fairly straightforward controller with basic functionalities which is easily tested using Postman. The objective of this project was to change the operation of this controller to suit our API.

New Files

#app/controllers/join_team_requests_controller.rb

The original code has variables like 'P', 'D' and 'A' which lead to ambiguity and they had to be changed to

class Api::V1::JoinTeamRequestsController < ApplicationController
  # Constants used to indicate status for the request
  # The datatype for status is string
  PENDING = 'P'
  DECLINED = 'D'
  ACCEPTED = 'A'

We also added new methods to the controller for Create, Read, Update and Delete functionality.
For example

def create

    @join_team_request = JoinTeamRequest.new
    @join_team_request.comments = params[:comments]
    @join_team_request.status = PENDING
    @join_team_request.team_id = params[:team_id]


    participant = Participant.where(user_id: @current_user.id, assignment_id: params[:assignment_id]).first

    team = Team.find(params[:team_id])
    if team.participants.include?(participant)
      render json: { error: 'You already belong to the team' }, status: :unprocessable_entity
    else
    if participant
      @join_team_request.participant_id = participant.id
      if @join_team_request.save
        render json: @join_team_request, status: :created
      else
        render json: { errors: @join_team_request.errors.full_messages }, status: :unprocessable_entity
      end
    else
      render json: { errors: "Participant not found" }, status: :unprocessable_entity
    end

    end
  end
def show
    render json: @join_team_request
  end
def update
    if @join_team_request.update(join_team_request_params)
      render json: { message: 'JoinTeamRequest was successfully updated' }, status: :ok
    else
      render json: { errors: @join_team_request.errors.full_messages }, status: :unprocessable_entity
    end
  end
def destroy
     @join_team_request.destroy
  end

Apart from this, we added the 'decline' methods to handle accept/decline requests

def decline
    @join_team_request.status = DECLINED

    if @join_team_request.save
      render json: { message: 'JoinTeamRequest declined successfully' }, status: :ok
    else
      render json: { errors: @join_team_request.errors.full_messages }, status: :unprocessable_entity
    end
  end

We have added additional functionality like
1. Checking if the team is already full

def check_team_status
    team = Team.find(params[:team_id])
    if team.full?
      render json: { message: 'This team is full.' }, status: :unprocessable_entity
    end
  end

2. Checking if the student is already a part of some team

if team.participants.include?(participant)
      render json: { error: 'You already belong to the team' }, status: :unprocessable_entity

#app/models/join_team_requests.rb
We have added the model file to handle join team requests coming from one student to the other.

#spec/requests/api/v1/join_team_requests_spec.rb
This spec file was added to handle the testing for our controller. More information about testing is given in the testing section.

File Changes

#app/models/participant.rb
Original code

class Participant < ApplicationRecord
  belongs_to :user
  belongs_to :assignment, foreign_key: 'assignment_id', inverse_of: false

Modified code

class Participant < ApplicationRecord
  belongs_to :user
  belongs_to :assignment, foreign_key: 'assignment_id', inverse_of: false
  has_many   :join_team_requests, dependent: :destroy

#config/routes.rb
Original code

Rails.application.routes.draw do

  mount Rswag::Api::Engine => 'api-docs'
  mount Rswag::Ui::Engine => 'api-docs'
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
          post '/sign_up_student', to: 'signed_up_teams#sign_up_student'
        end
      end

Modified code

Rails.application.routes.draw do

  mount Rswag::Api::Engine => 'api-docs'
  mount Rswag::Ui::Engine => 'api-docs'
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
          post '/sign_up_student', to: 'signed_up_teams#sign_up_student'
        end
      end

      resources :join_team_requests do
        collection do
          post 'decline/:id', to:'join_team_requests#decline'
          post 'accept/:id', to:'join_team_requests#accept'
        end
      end

Testing

We have thoroughly tested our code using Postman and the controller works flawlessly as shown below.
Here is the video which shows testing the controller using Postman -
https://drive.google.com/file/d/1hS8vjWwHznWks9Qj7S-YIWdCfqqcsyhe/view?usp=sharing

Here is the video for Rswag testing
https://drive.google.com/file/d/1SizhiJhaTIC1bZPgxVoHN8MtfhC3Lcbc/view?usp=sharing


Following is a link to the Swagger test file -
https://github.com/manoj-ayyappan/csc517_program3_E2370/blob/main/spec/requests/api/v1/join_team_requests_spec.rb


Modifications

- Created new methods for the Join Team Requests Controller to support CRUD functionality.
- Added an accept and decline method.
- Modified the status to use constants such as 'PENDING', 'ACCEPTED', and 'DECLINED' instead of 'P', 'D', 'A'.

Results

We've successfully reimplemented the Join Teams Request Controller, achieving a more comprehensive feature set by introducing new methods for CRUD functionality, facilitating the acceptance and rejection of join requests, and adopting meaningful status constants like 'PENDING,' 'ACCEPTED,' and 'DECLINED' in place of less intuitive single-character codes. These improvements not only expand the controller's capabilities but also enhance its readability and maintainability,

Team

Mentor
Renji Joseph Sabu <rsabu@ncsu.edu>

Students
Manoj Ayyappan <mayyapp@ncsu.edu>
Pradeep Patil <papatil@ncsu.edu>
Maya Patel <mdpatel2@ncsu.edu>

Pull Request

Changes for this project are under Expertiza Pull Request https://github.com/expertiza/reimplementation-back-end/pull/50