forked from expertiza/reimplementation-back-end
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
E2370: Reimplement join team requests controller (expertiza#95)
* Reimplement join team requests controller * Delete swagger.yaml file * updated join team request controller with requested changes
- Loading branch information
1 parent
94c3c15
commit c060103
Showing
16 changed files
with
386 additions
and
4 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
app/controllers/api/v1/join_team_requests_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
class Api::V1::JoinTeamRequestsController < ApplicationController | ||
# Constants used to indicate status for the request | ||
PENDING = 'PENDING' | ||
DECLINED = 'DECLINED' | ||
ACCEPTED = 'ACCEPTED' | ||
|
||
# This filter runs before the create action, checking if the team is full | ||
before_action :check_team_status, only: [:create] | ||
|
||
# This filter runs before the specified actions, finding the join team request | ||
before_action :find_request, only: %i[show update destroy decline] | ||
|
||
#checks if the current user is a student | ||
def action_allowed? | ||
@current_user.student? | ||
end | ||
|
||
# GET api/v1/join_team_requests | ||
# gets a list of all the join team requests | ||
def index | ||
unless @current_user.administrator? | ||
return render json: { errors: 'Unauthorized' }, status: :unauthorized | ||
end | ||
join_team_requests = JoinTeamRequest.all | ||
render json: join_team_requests, status: :ok | ||
end | ||
|
||
# GET api/v1join_team_requests/1 | ||
# show the join team request that is passed into the route | ||
def show | ||
render json: @join_team_request, status: :ok | ||
end | ||
|
||
# POST api/v1/join_team_requests | ||
# Creates a new join team request | ||
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 | ||
elsif 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 | ||
|
||
# PATCH/PUT api/v1/join_team_requests/1 | ||
# Updates a join team request | ||
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 | ||
|
||
# DELETE api/v1/join_team_requests/1 | ||
# delete a join team request | ||
def destroy | ||
if @join_team_request.destroy | ||
render json: { message: 'JoinTeamRequest was successfully deleted' }, status: :ok | ||
else | ||
render json: { errors: 'Failed to delete JoinTeamRequest' }, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
# decline a join team request | ||
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 | ||
|
||
private | ||
# checks if the team is full already | ||
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 | ||
|
||
# Finds the join team request by ID | ||
def find_request | ||
@join_team_request = JoinTeamRequest.find(params[:id]) | ||
end | ||
|
||
# Permits specified parameters for join team requests | ||
def join_team_request_params | ||
params.require(:join_team_request).permit(:comments, :status) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class JoinTeamRequest < ApplicationRecord | ||
# TODO Uncomment the following line when Team and Team Controller is thoroughly implemented | ||
# belongs_to :team | ||
has_one :participant, dependent: :nullify | ||
ACCEPTED_STATUSES = %w[ACCEPTED DECLINED PENDING] | ||
validates :status, inclusion: { in: ACCEPTED_STATUSES } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class CreateJoinTeamRequests < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :join_team_requests do |t| | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
8 changes: 8 additions & 0 deletions
8
db/migrate/20231019195109_add_fields_to_join_team_requests.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class AddFieldsToJoinTeamRequests < ActiveRecord::Migration[7.0] | ||
def change | ||
add_column :join_team_requests, :participant_id, :integer | ||
add_column :join_team_requests, :team_id, :integer | ||
add_column :join_team_requests, :comments, :text | ||
add_column :join_team_requests, :status, :string | ||
end | ||
end |
5 changes: 5 additions & 0 deletions
5
db/migrate/20231030174450_add_join_team_request_to_participants.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddJoinTeamRequestToParticipants < ActiveRecord::Migration[6.0] | ||
def change | ||
add_reference :participants, :join_team_request, foreign_key: true | ||
end | ||
end |
5 changes: 5 additions & 0 deletions
5
db/migrate/20231102173153_create_participants_teams_relationship.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class CreateParticipantsTeamsRelationship < ActiveRecord::Migration[7.0] | ||
def change | ||
add_reference :participants, :team, foreign_key: true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe JoinTeamRequest, type: :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
require 'swagger_helper' | ||
|
||
RSpec.describe 'Join Team Requests Controller', type: :request do | ||
|
||
# API endpoint to decline a join team request | ||
path '/api/v1/join_team_requests/decline/{id}' do | ||
parameter name: 'id', in: :path, type: :string, description: 'id' | ||
|
||
post('decline join_team_request') do | ||
tags 'Join Team Requests' | ||
response(200, 'successful') do | ||
# Include response example in Swagger documentation | ||
after do |example| | ||
example.metadata[:response][:content] = { | ||
'application/json' => { | ||
example: JSON.parse(response.body, symbolize_names: true) | ||
} | ||
} | ||
end | ||
run_test! | ||
end | ||
end | ||
end | ||
|
||
# API endpoint to list join team requests | ||
path '/api/v1/join_team_requests' do | ||
|
||
get('list join_team_requests') do | ||
tags 'Join Team Requests' | ||
response(200, 'successful') do | ||
# Include response example in Swagger documentation | ||
after do |example| | ||
example.metadata[:response][:content] = { | ||
'application/json' => { | ||
example: JSON.parse(response.body, symbolize_names: true) | ||
} | ||
} | ||
end | ||
run_test! | ||
end | ||
end | ||
|
||
# API endpoint to create a join team request | ||
post('create join_team_request') do | ||
parameter name: 'comments', in: :query, type: :string, description: 'comments' | ||
parameter name: 'team_id', in: :query, type: :integer, description: 'team_id' | ||
parameter name: 'assignment_id', in: :query, type: :integer, description: 'assignment_id' | ||
tags 'Join Team Requests' | ||
|
||
# Success response | ||
response(200, 'success') do | ||
after do |example| | ||
example.metadata[:response][:content] = { | ||
'application/json' => { | ||
example: JSON.parse(response.body, symbolize_names: true) | ||
} | ||
} | ||
end | ||
run_test! | ||
end | ||
|
||
# Created response with example data | ||
response(201, 'created') do | ||
let(:join_team_request) { JoinTeamRequest.create(valid_join_team_request_params) } | ||
run_test! do | ||
expect(response.body).to include('"comments":"comment"') | ||
end | ||
end | ||
|
||
# Unprocessable Entity response with example data | ||
response(422, 'unprocessable entity') do | ||
let(:join_team_request) { JoinTeamRequest.create(invalid_join_team_request_params) } | ||
run_test! | ||
end | ||
end | ||
end | ||
|
||
# API endpoint to show a specific join team request | ||
path '/api/v1/join_team_requests/{id}' do | ||
parameter name: 'id', in: :path, type: :string, description: 'id' | ||
|
||
get('show join_team_request') do | ||
tags 'Join Team Requests' | ||
response(200, 'successful') do | ||
# Include response example in Swagger documentation | ||
after do |example| | ||
example.metadata[:response][:content] = { | ||
'application/json' => { | ||
example: JSON.parse(response.body, symbolize_names: true) | ||
} | ||
} | ||
end | ||
# 404 response when the requested join team request is not found | ||
response(404, 'not_found') do | ||
let(:id) { 'invalid' } | ||
run_test! do | ||
expect(response.body).to include("Couldn't find JoinTeamRequest") | ||
end | ||
end | ||
run_test! | ||
end | ||
end | ||
|
||
# API endpoint to update a join team request using PATCH | ||
patch('update join_team_request') do | ||
parameter name: 'join_team_request[comments]', in: :query, type: :string, description: 'comments' | ||
parameter name: 'join_team_request[status]', in: :query, type: :string, description: 'status' | ||
tags 'Join Team Requests' | ||
response(200, 'successful') do | ||
# Include response example in Swagger documentation | ||
after do |example| | ||
example.metadata[:response][:content] = { | ||
'application/json' => { | ||
example: JSON.parse(response.body, symbolize_names: true) | ||
} | ||
} | ||
end | ||
run_test! | ||
end | ||
end | ||
|
||
# API endpoint to update a join team request using PUT | ||
put('update join_team_request') do | ||
parameter name: 'join_team_request[comments]', in: :query, type: :string, description: 'comments' | ||
parameter name: 'join_team_request[status]', in: :query, type: :string, description: 'status' | ||
tags 'Join Team Requests' | ||
|
||
# Include request body parameter schema in Swagger documentation | ||
parameter name: :body_params, in: :body, schema: { | ||
type: :object, | ||
properties: { | ||
comments: { type: :string } | ||
} | ||
} | ||
response(200, 'successful') do | ||
# Include response example in Swagger documentation | ||
after do |example| | ||
example.metadata[:response][:content] = { | ||
'application/json' => { | ||
example: JSON.parse(response.body, symbolize_names: true) | ||
} | ||
} | ||
end | ||
run_test! | ||
end | ||
|
||
# Unprocessable Entity response with example data | ||
response(422, 'unprocessable entity') do | ||
let(:body_params) { { comments: -1 } } | ||
schema type: :string | ||
run_test! do | ||
expect(response.body).to_not include('"comments":-1') | ||
end | ||
end | ||
end | ||
|
||
# API endpoint to delete a join team request | ||
delete('delete join_team_request') do | ||
tags 'Join Team Requests' | ||
|
||
# Successful response with example | ||
response(204, 'successful') do | ||
run_test! do | ||
expect(JoinTeamRequest.exists?(id)).to eq(false) | ||
end | ||
end | ||
|
||
# Not Found response with example | ||
response(404, 'not found') do | ||
run_test! do | ||
expect(response.body).to include("Couldn't find JoinTeamRequest") | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.