CSC/ECE 517 Fall 2023 - E2386. Reimplement teams users backend: Difference between revisions
(→Model) |
|||
Line 188: | Line 188: | ||
===Testing Plan=== | ===Testing Plan=== | ||
The TeamsUsers model and controller will be tested using Rspec | The TeamsUsers model and controller will be tested using Rspec. As the methods in the controller are defined as RESTful endpoints. The test take into consideration that the methods return the correct status codes for the output. Based on the current functionality of TeamsUsers model and controller we have defined the following test which are implemented to test the reimplemented code of teams_users model and controller. | ||
'''Tests for teams_users model:''' | |||
{| class="wikitable" style="margin-left:40px" | {| class="wikitable" style="margin-left:40px" | ||
Line 212: | Line 215: | ||
| 9 || Tests if `#delete_selected` deletes selected users and redirects to 'http://test.host/teams_users/list'. | | 9 || Tests if `#delete_selected` deletes selected users and redirects to 'http://test.host/teams_users/list'. | ||
|} | |} | ||
===Relevant Links=== | ===Relevant Links=== |
Revision as of 09:30, 4 December 2023
Description of Project
The focal point of the project is the teams_users_controller.rb, teams_user.rb model capturing information about team members, and affiliations with other models such as Users and Teams. This model undertakes various responsibilities, including the formation of teams with enrolled students and the display of team members. Oversight of team management falls under the purview of the teams_user_controller, which orchestrates CRUD operations on the teams_user model. Its duties span the creation, modification, and deletion of teams, along with the administration of team members through additions or removals.
Problem Statement
The main aim of the project is to create a backend for teams_users_controller. The team needs to write appropriate test cases for the models as well as refactor the code to make sure the correct JSON response is being returned. The project involves creating a backend system for seamless CRUD operations on a relational database, specifically focusing on interactions between teams and users. This requires defining "teams_users" model with suitable attributes and relationships, establishing controllers to manage CRUD actions, and setting up corresponding routes. Emphasis is placed on rigorous testing, covering model validations and associations, as well as controller actions to ensure accurate execution of CRUD operations. A key project aspect involves refining code to produce precise JSON responses. Adhering to RESTful conventions, the re-implementation includes appropriate HTTP response codes. RSpec tests are mandatory for both the teams_users model and TeamUsersController, covering all endpoints and incorporating HTTP response codes. These controller tests are designed to integrate seamlessly with RSwag for generating API documentation and testing REST APIs through the Swagger UI.
Objectives
- Craft RESTful endpoints for creating, displaying, updating and deleting teams users.
- Develop and integrate methods within the teams_users_controller to facilitate CRUD operations
- Enforce the use of proper status codes and robust validation mechanisms for all RESTful endpoints.
- Construct models and controllers for the teams_user model, incorporating contemporary approaches to writing Ruby code. This involves leveraging language features and adhering to best practices for object-oriented design.
- Compose comprehensive RSpec tests covering both the models, and controllers to ensure the robustness and reliability of the entire system.
Development Strategy
Our development strategy adheres to the principles of Test-Driven Development (TDD) for the implementation of both the teams_user model and the TeamsUsersController. The process begins with the creation of a failing test case, followed by the addition of code to address the identified issues and ensure the successful passage of the test.
Project Design and Implementation
We have added the teams_user model and TeamsUserController and have outlined a set of RESTful endpoints along with their corresponding request types for implementation to facilitate CRUD operations on teams_users.
Functionality
In this project, we aim to add functions for the following:
1. Retrieve teams_users of a particular team from the database.
2. Display all teams_users.
3. Add a new teams_user.
4. Update an existing teams_user.
4. Delete a specific teams_user.
Code Snippets
Model
The `TeamsUser` model in the Expertiza code represents a user's association with a team within the context of a collaborative assignment or project. It utilizes ActiveRecord associations to establish relationships with the `User` and `Team` models, indicating that a user belongs to a team. The model includes methods to return the name of the user based on ip_address. Then it includes a method that removes entry in the TeamUsers table for the given user and given team id. Another method returns the first entry in the TeamUsers table for a given team id and the last method determines whether a team is empty or not.
class TeamsUser < ApplicationRecord belongs_to :user belongs_to :team def name(ip_address = nil) name = user.name(ip_address) end def get_team_members(team_id); end # Removes entry in the TeamUsers table for the given user and given team id def self.remove_team(user_id, team_id) team_user = TeamsUser.where('user_id = ? and team_id = ?', user_id, team_id).first team_user&.destroy end # Returns the first entry in the TeamUsers table for a given team id def self.first_by_team_id(team_id) TeamsUser.where('team_id = ?', team_id).first end # Determines whether a team is empty of not def self.team_empty?(team_id) team_members = TeamsUser.where('team_id = ?', team_id) team_members.blank? end end
Controller
The `TeamsUsersController` handles various actions related to team management within the application. It includes methods that lists all teams_users, gets the teams_users of a particular team, renders a form to create a new teams_user for a specific team, creates a new teams_user, updates an existing teams_user, removes a teams_user and calls methods on the TeamsUser model.
class Api::V1::TeamsUsersController < ApplicationController before_action :find_teams_user, only: [:update, :destroy] rescue_from ActiveRecord::RecordNotFound, with: :teams_user_not_found rescue_from ActionController::ParameterMissing, with: :parameter_missing # GET /teams_users # List all the teams_users def index teams_users = TeamsUser.all render json: teams_users, status: :ok end # GET /teams/1 # Get the team_users of a particular team def show team = Team.find(params[:id]) teams_users = TeamsUser.where(team_id: params[:id]) render json: teams_users, status: :ok end # GET /teams/1/teams_users/new # Render form to create a new teams_user for a specific team def new @team = Team.find(params[:id]) render json: team, status: :ok end # POST /teams_users # Create a new teams_user def create teams_user = TeamsUser.new(teams_user_params) if teams_user.save render json: teams_user, status: :created else render json: { error: teams_user.errors.full_messages }, status: :unprocessable_entity end end # PATCH/PUT /teams_users/1 # Update an existing teams_user def update if @teams_user.update(teams_user_params) render json: @teams_user, status: :ok else render json: { error: @teams_user.errors.full_messages }, status: :unprocessable_entity end end # DELETE /teams_users/1 # Remove a teams_user def destroy @teams_user = TeamsUser.find(params[:id]) if @teams_user.destroy render json: { message: 'TeamsUser removed successfully' }, status: :no_content else render json: { error: 'Failed to remove TeamsUser' }, status: :unprocessable_entity end end private def find_teams_user @teams_user = TeamsUser.find(params[:id]) end def teams_user_params params.require(:teams_user).permit(:team_id, :user_id, :duty_id, :pair_programming_status, :participant_id) end def teams_user_not_found render json: { error: "TeamsUser with id #{params[:id]} not found" }, status: :not_found end def parameter_missing render json: { error: "Parameter missing" }, status: :unprocessable_entity end def redirect_to_teams_list(parent_id, flash_message) redirect_to controller: 'teams', action: 'list', id: parent_id, flash: { success: flash_message } end end
Schema
create_table "teams_users", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.integer "team_id" t.integer "user_id" t.integer "duty_id" t.string "pair_programming_status", limit: 1 t.integer "participant_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["duty_id"], name: "index_teams_participants_on_duty_id" t.index ["participant_id"], name: "fk_rails_f4d20198de" t.index ["team_id"], name: "fk_users_teams" t.index ["user_id"], name: "fk_teams_users" end
The following image shows the tables referencing teams_users table is referencing to
Testing Plan
The TeamsUsers model and controller will be tested using Rspec. As the methods in the controller are defined as RESTful endpoints. The test take into consideration that the methods return the correct status codes for the output. Based on the current functionality of TeamsUsers model and controller we have defined the following test which are implemented to test the reimplemented code of teams_users model and controller.
Tests for teams_users model:
Test No. | Description |
---|---|
1 | Tests if `#update_duties` updates duties for a participant successfully and redirects to '/student_teams/view?student_id=1'. |
2 | Tests if `#list` renders the list of users under Assignment team teams#users. |
3 | Tests if `#new` sets the Team object to an instance variable. |
4 | Tests if `#create` (User Not Defined) throws an error and redirects to 'http://test.host/teams/list?id=1' when a user is not defined. |
5 | Tests if `#create` (User Not a Participant) throws an error and redirects to 'http://test.host/teams/list?id=1' when the user added is not a participant of the current assignment. |
6 | Tests if `#create` (Assignment Team Full) throws an error 'This team already has the maximum number of members.' and redirects to 'http://test.host/teams/list?id=1'. |
7 | Tests if `#create` (User Successfully Added) new user gets successfully added to the assignment and redirects to 'http://test.host/teams/list?id=1'. |
8 | Tests if `#delete` deletes a user and redirects to 'http://test.host/teams/list?id=1'. |
9 | Tests if `#delete_selected` deletes selected users and redirects to 'http://test.host/teams_users/list'. |
Relevant Links
Github Repository: https://github.com/Kashika08/reimplementation-back-end
Team Members
- Jay Shah (github: jayjshah2000)
- Kashika Malick (github: kashika08)
- Riya Gori (github: riyagori1203)
Mentor
Kartiki Bhandakkar