CSC/ECE 517 Spring 2023 E2342: Reimplement Participants Model and Controller

From Expertiza_Wiki
Revision as of 00:16, 8 April 2023 by Spandey5 (talk | contribs)
Jump to navigation Jump to search

Background

In expertiza, there are two different models: Course and Assignment. Although an Assignment is a part of a Course, students can participate in a Course or an Assignment independently.

The ParticipantsController provides functionalities for listing the participants of a course or an assignment, adding a participant to a course or an assignment, updating the authorizations of a participant (can_submit, can_review, can_take_quiz), updating the handle of a participant for an assignment, inheriting course participants to its assignment, bequeathing assignment participants to its course and deleting a participant from a course or an assignment.

Project Purpose

The aim of our project is to integrate the reimplemented versions of Participants Model and Participants Controller while maintaining the existing functionality of the feature. We plan on restricting all the business logic to the model, which will also handle all of the interactions with the database. The controller, on the other hand, will be responsible only for accepting appropriate input and returning the results in a JSON format. The controller will also query the database but write operations would only be done in the model. The test suite will also be updated to reflect the above-mentioned development plan as we develop a comprehensive test plan for both the model as well as the controller.

Project Design

High-level Design Diagram

Use Case Diagram

The following Use Case Diagram demonstrates the primary functionality relevant to participants for different types of users in the system.

Implementation

As part of the re-implementation, the objective is to employ an API-first approach while maintaining all the existing functionality in an efficient, robust and simple manner. Specifically for the participants controller, we've added seven different API endpoints to extend the current functionalities while modifying the code to support these APIs. The details for these endpoints can be found below.

Assumptions

  1. The CourseParticipant and AssignmentParticipant models implement the copy method which copies participants from the course to assignment and vice-versa. This functionality is used in the inherit and bequeath methods.

Model documentation

The participants model file participant.rb was re-implemented to include only the necessary business logic and remove unnecessary functionality.

Replaced force_delete Method with leave_team Method

The original 'force_delete' method would delete the participant from the team and if there is only one participant in the team then it would delete the team.

A new method 'leave_team' was introduced in place of 'force_delete' as 'force_delete' method is supposed to be present in the teams.rb and not participant.rb. The difference between force_delete method and this is that even if there is only one participant it wouldn't delete the team.

mail_assigned_reviewers Method was Removed

The method 'Mail_assigned_reviewers' is responsible for sending an email to the assigned reviewer whenever a new submission is made. It would be more appropriate for this method to be included in teams.rb rather than participant.rb. Hence, this method was removed.

able_to_review Method was Removed

'able_to_review' method is just using can_review value. This method can be eliminated.

email Method was Removed

'email' method was removed as it was not required.

Merged export and export_fields Methods

We didn't implement this task as merging two functions became a hassle for debugging and testing and fixing errors present in the code. Instead, it is generally considered good programming practice to break down large functions into smaller, more manageable ones. This principle is commonly known as the "Single Responsibility Principle," which states that a function or module should have only one reason to change. By breaking down a large function into smaller functions, made our code more modular and easier to read, understand, and maintain.

Controller API documentation

 #  Method Endpoint Description
1 index GET /participants/index/:model/:id returns a list of participants of an assignment or a course
2 create POST /participants/:model/:id creates a participant in an assignment or a course
3 update_handle PATCH /participants/change_handle/:id updates the participant's handle for an assignment
4 update_authorizations PATCH /participants/update_authorizations/:id updates the participant's permissions for an assignment or a course depending on the role
5 delete DELETE /participants/:id deletes a participant from an assignment or a course
6 inherit GET /participants/inherit/:id copies existing participants from a course down to its assignment
7 bequeath GET /participants/bequeath/:id copies existing participants from an assignment up to its course

Index

Description: This endpoint allows the user to request a list of all participants for the provided assignment ID or course ID. A success response renders a JSON with a list of participants that can be parsed by the front-end.

Path: GET /participants/index/:model/:id

Parameters:

 #  Parameter Expected Value
1 model One of these two strings: ['Assignment', 'Course']
2 id The unique identifier for an assignment or a course depending on the value of :model

Response:

{
  "model_object": model_object,
  "participants": participants,
}
status: :ok

{ 
  error: "Missing or invalid required parameters" 
}
status: :unprocessable_entity

Create

Description: This endpoint allows the user to create a new participant for the provided assignment ID or course ID. A success response renders a JSON with the participant details. The user is required to provide a valid username as well as the appropriate permissions for the new participant. These permissions are [can_submit, can_review, can_take_quiz]. Depending on whether the request is for an Assignment or a Course, a participant type is determined between an AssignmentParticipant or a CourseParticipant and an entry is made into the database accordingly.

Path: POST /participants/:model/:id

Parameters:

 #  Parameter Expected Value
1 model One of ['Assignment', 'Course']
2 id The id of the course or assignment

Request Body:

content:
  application/json:
    schema:
      type: object
      properties:
        user:
          type: object
          properties:
          name: string
        participant:
          type: object
          properties:
            can_submit:
              type: boolean
            can_review:
              type: boolean
            can_take_quiz:
              type: boolean

Response:

Success
{
  "participant": participant,
}
status: :created

When user does not exist
{ 
  error: "User #{params[:user][:name]} does not exist" 
}
status: :not_found

When participant already exists in the assignment/course
{ 
  error: "Participant #{params[:user][:name]} already exists for this #{params[:model]}" 
}
status: :unprocessable_entity

Update Handle

Description: This endpoint allows the user to change their handle name for a given AssignmentParticipant. An AssignmentParticipant is one who is already enrolled in the assignment. A success response renders a JSON with the updated participant details. The user is required to provide a valid and available handle name.

Path: POST /participants/change_handle/:id

Parameters:

 #  Parameter Expected Value
1 id The id of the AssignmentParticipant

Request Body:

content:
  application/json:
    schema:
      type: object
      properties:
        participant:
          type: object
          properties:
          handle: 
            type: integer

Response:

When handle change was successful
{
  "participant": participant,
}
status: :ok

When handle is already in use
{ 
  note: "Handle already in use" 
}
status: :ok

Failure
{ 
  error: participant.errors
}
status: :unprocessable_entity

Update authorizations

Description: This endpoint allows the user to change the permissions for a given Participant. The appropriate permissions for any participant are [can_submit, can_review, can_take_quiz]. A Participant is one who is already enrolled in an assignment or a course. A success response renders a JSON with the updated participant details and permissions.

Path: POST /participants/update_authorizations/:id

Parameters:

 #  Parameter Expected Value
1 id The id of the Participant

Request Body:

content:
  application/json:
    schema:
      type: object
      properties:
        participant:
          type: object
          properties:
            can_submit:
              type: boolean
            can_review:
              type: boolean
            can_take_quiz:
              type: boolean

Response:

Success
{
  "participant": participant,
}
status: :ok

Failure
{ 
  error: participant.errors
}
status: :unprocessable_entity

Delete

Description: This endpoint allows the user to delete an existing participant from an assignment or a course. The user is expected to provide the valid participant ID. The participant is only deleted if they are not part of another team for a different assignment/course. This is to ensure that a participant enrolled in two different teams is not deleted from the list of participants. Deletion is only possible if the participant is only enrolled in only one team. A success response renders a JSON with a confirmation of the deletion.

Path: DELETE /participants/:id

Parameters:

 #  Parameter Expected Value
1 id The id of the Participant

Response:

Success
{
  message: "#{participant.user.name} was successfully removed as a participant"
}
status: :ok

When the participant is on another team
{ 
  error: "This participant is on a team" 
}
status: :unprocessable_entity

Failure
{ 
  error: "Failed to remove participant" 
}
status: :unprocessable_entity

Inherit

Description: This endpoint allows the user to copy the existing participants from a course to the provided assignment in a single operation. Before this is carried out, validations are performed on 1) the existence of a course for the provided assignment and 2) non-empty list of participants already registered in the course. After these checks, the participants are copied from the course to the given assignment with the help of a private method copy_participants_from_source_to_target. A success response renders a JSON with a confirmation of the copy.

Path: GET /participants/inherit/:id

Parameters:

 #  Parameter Expected Value
1 id The id of the Assignment

Response:

When copy was successful
{
  message: "The participants from #{source.name} were copied to #{target.name}"
}
status: :created

When all the participants already existed in the assignment(no-op)
{
  note: "All of #{source.name} participants are already in #{target.name}"
}
status: :ok

When there were no participants for the course
{
  note: "No participants were found for this #{source.name}"
}
status: :ok

When there is no course for the given assignment
{ 
  error: "No course was found for this assignment"
}
status: :unprocessable_entity

Bequeath

Description: This endpoint allows the user to copy the existing participants from the provided assignment to its associated course in a single operation. Before this is carried out, validations are performed on 1) the existence of a course for the provided assignment and 2) non-empty list of participants already registered in the assignment. After these checks, the participants are copied from the given assignment to its course with the help of a private method copy_participants_from_source_to_target. A success response renders a JSON with a confirmation of the copy.

Path: GET /participants/bequeath/:id

Parameters:

 #  Parameter Expected Value
1 id The id of the Assignment

Response:

When copy was successful
{
  message: "The participants from #{source.name} were copied to #{target.name}"
}
status: :created

When all the participants already existed in the course(no-op)
{
  note: "All of #{source.name} participants are already in #{target.name}"
}
status: :ok

When there were no participants for the assignment
{
  note: "No participants were found for this #{source.name}"
}
status: :ok

When there is no course for the given assignment
{ 
  error: "No course was found for this assignment"
}
status: :unprocessable_entity

Test Plan

So far we've written individual tests for the model and controller files of the participants functionality. With the goal of this project being the integration of the two, we will look to add some tests which facilitate the combined working of the two files. This might include modifying existing test scenarios as well as writing new ones. We expect the number of tests to go up as well as the overall code coverage to improve after this project. The existing test suites for the model and controller can be found below.

Model test suite

The testing framework used here is RSpec. The participants.rb contains many methods and associations with other models. Hence, to test the methods in participants, the other dependent methods have to be stubbed. To deal with the models, dummy models/dummy app can be created which will act as the associated classes. We have created all the required dummy classes by scaffolding them.

To create test fixtures and to build the required models, Factory Bot has been used. Two separate files, factories.rb and factory_bot.rb was created to build the factories. Four participants and other required fields were created as fixtures after which the tests were written.

To run the tests:
1. git clone https://github.com/amarthyasa/reimplementation-back-end/tree/testing_files
2. cd reimplementation-back-end/
3. bundle install
4. bundle exec rspec spec/models/participants_spec.rb

More details on the individual tests can be found here

Controller test suite

The testing framework used here is RSpec. The participants_controller.rb has dependencies on models and methods which are yet to be implemented. In the meantime, to test the API, we have used stubs and mocks. We have created some new models and added some associations in them which were required for stubbing.

To run the tests:
1. git clone https://github.com/devashish-vachhani/reimplementation-back-end
2. cd reimplementation-back-end/
3. bundle install
4. bundle exec rspec spec/requests/api/v1/participants_spec.rb

More details on the individual tests can be found here

Contributors

  1. Saksham Pandey (spandey5@ncsu.edu)
  2. Devashish Vachhani (dvachha@ncsu.edu)
  3. Karthik K Jayakumar (kkunnum@ncsu.edu)

Mentor: Rucha Kolekar (rbkoleka@ncsu.edu)

Thanks to participants model re-implementation team

Relevant Links