Class: V1::SpinCandidatesController

Inherits:
ApiController show all
Defined in:
app/controllers/v1/spin_candidates_controller.rb

Overview

Spin Candidates controller Provides actions on the Spin Candidates

Constant Summary

Constants included from ApiController::Parameters

ApiController::Parameters::PARAMS

Instance Method Summary collapse

Methods inherited from ApiController

#check_params_required, #return_response, #version

Methods included from ApiController::Metadata

#meta_attributes, #meta_pagination

Methods included from ApiController::Pagination

#compile_page, #select_page

Methods included from ApiController::Parameters

#check_params, #expand_resources?, #numeric?, #true?

Instance Method Details

#indexObject

@api get /spin_candidates Request list of spin_candidates for the user

@apiVersion 1.0.0
@apiName GetSpinCandidate
@apiGroup SpinCandidate
@apiPermission user
@apiSuccess {String} version Version of the API
@apiSuccess {Object[]} providers Authentication endpoints
@apiSuccess {String=["github"]} providers.type Type of provider oauth
@apiSuccess {Boolean} providers.enabled Whether the provide is enabled or not
@apiSuccess {String} providers.id_application Id of the application in the oauth provider.
@apiSuccess {String} providers.server Server Address
@apiSuccess {String} providers.version API Prefix
@apiSuccess {Boolean} providers.verify Does it need verify?

31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/v1/spin_candidates_controller.rb', line 31

def index
  @spins = SpinCandidate.where(user_id: current_user)
  @spins = @spins.where('name ILIKE ?', "%#{params[:query]}%") if params[:query]
  @spins = @spins.order("#{params[:sort]} #{params[:order] || 'DESC'}") if params[:sort]
  if @spins.count.positive?
    return_response @spins, :ok, {}
  else
    render status: :no_content
  end
end

#publishObject

Publish the SpinCandidate into a Spin


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/controllers/v1/spin_candidates_controller.rb', line 102

def publish
  #puts "User is"+ current_user.inspect
  sc = SpinCandidate.find(params[:spin_candidate_id])
  spin = sc.spin || Spin.new(full_name: sc.full_name, user: sc.user, spin_candidate: sc)
  unless current_user == sc.user
    render_error_exchange(:spin_candidate_not_owner, :not_allowed)
    return
  end
  if sc
    if sc.publish_spin user: current_user
      return_response sc, :ok, {}
    else
      render_error_exchange(:spin_candidate_not_validated, :precondition_failed)
    end
  else
    render_error_exchange(:spin_candidate_not_found, :not_found)
  end
  # If valid create or update the Spin
  # If not valid, the log should be updated.
end

#refreshObject

Refresh Authenticated only Refresh the list of providers for the user. Connects to github, gets all repos of the user, and search for spins


62
63
64
65
66
67
68
69
70
# File 'app/controllers/v1/spin_candidates_controller.rb', line 62

def refresh
  user = current_user.admin? ? User.find(params[:user_id]) : current_user
  if user.nil?
    render json: { error: 'No user found' }, status: :not_found
    return
  end
  job = RefreshSpinCandidatesJob.perform_later(user: user, token: request.headers['HTTP_X_USER_TOKEN'])
  render json: { data: job.job_id, metadata: { queue: job.queue_name, priority: job.priority } }, status: :accepted
end

#showObject

Show (id: identification of the spin) Provides a view of the Spin Candidate


47
48
49
50
51
52
53
54
# File 'app/controllers/v1/spin_candidates_controller.rb', line 47

def show
  @spin = SpinCandidate.find_by(user_id: current_user.id, id: params[:id])
  unless @spin
    render_error_exchange(:spin_candidate_not_found, :not_found)
    return
  end
  return_response @spin, :ok, {}
end

#unpublishObject


123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/controllers/v1/spin_candidates_controller.rb', line 123

def unpublish
  sc = SpinCandidate.find(params[:spin_candidate_id])
  if sc.spin
    if sc.unpublish
      return_response sc, :ok, {}
    else
      render_error_exchange(:spin_candidate_not_published, :precondition_failed)
    end
  else
    render_error_exchange(:spin_candidate_not_published, :not_found)
  end
end

#validateObject

Validates the SpinCandidate updates the log


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/v1/spin_candidates_controller.rb', line 75

def validate
  # Create Spin with metadata
  # Validate the Spin
  # Write result in log
  # Return true or false
  sc = SpinCandidate.find(params[:spin_candidate_id])
  unless current_user == sc.user
    render_error_exchange(:spin_candidate_not_owner, :not_allowed)
    return
  end
  if sc
    render_error_exchange(:spin_not_owner, :not_allowed) unless @current_user == sc.user

      # TODO test sc
      spin = sc.spin || Spin.new(full_name: sc.full_name, user: sc.user, spin_candidate: sc)
      if(spin.check current_user)
        return_response sc, :ok, {}
      else
        render_error_exchange(:spin_candidate_not_validated, :not_found, {log: sc.validation_log})
      end
  else
    render_error_exchange(:spin_candidate_not_found, :not_found)
  end
end