Skip to content
Snippets Groups Projects
Commit df30a5d1 authored by ahcv19's avatar ahcv19
Browse files

Issue #3: Add curator route

parent 9b9cc0c7
No related branches found
No related tags found
2 merge requests!17Curatorship and Gamification done,!11Issue #3: Add curator route
Pipeline #30302 failed
class V1::ComplaintVotesController < ApplicationController
include ::DeletedObjectsController
include ::Paginator
before_action :set_complaint_vote, only: [:show, :update, :destroy]
# GET /v1/complaint_votes
def index
complaint_votes = paginate ComplaintVote
render json: complaint_votes
end
# GET /v1/complaint_votes/{id}
def show
render json: @complaint_vote
end
# POST /v1/complaint_votes
def create
@complaint_vote = ComplaintVote.where(complainable_id: params[:complainable_id]).first
# if object does not exists create
if @complaint_vote.blank?
@complaint_vote = ComplaintVote.new(complaint_vote_params)
if @complaint_vote.save
render json: @complaint_vote, status: :created
else
render json: @complaint_vote.errors, status: :unprocessable_entity
end
# if object already exists update the pros and cons votes by addding the new vote on the current votes
else
vote_pros = @complaint_vote.pros + params[:pros]
vote_cons = @complaint_vote.cons + params[:cons]
@complaint_vote.update(pros: vote_pros, cons: vote_cons)
verify_votes(@complaint_vote)
end
end
# Set the params required for creating a complaint vote
def complaint_vote_params
params.require(:complaint_vote).permit(:complainable_type, :complainable_id, :pros, :cons)
end
# PUT/PATCH /v1/complaint_votes/{id}
def update
if @complaint_vote.update(complaint_vote_params)
render json: @complaint_vote
else
render json: @complaint_vote.errors, status: :unprocessable_entity
end
end
# DELETE /v1/complaint_votes/{id}
def destroy
@complaint_vote.destroy
response = { 'status': 'deleted' }
render status: :ok, json: response
end
def set_complaint_vote
@complaint_vote = ComplaintVote.where(id: params[:id]).first
render status: :not_found if @complaint_vote.blank?
@complaint_vote
end
# if the difference between the pros and cons votes is more than 2, destroy the learning object
def verify_votes(complaint_vote)
if complaint_vote.pros - complaint_vote.cons > 2
LearningObject.find_by_id(complaint_vote.complainable_id).destroy
Complaint.find_by_complainable_id(complaint_vote.complainable_id).destroy
ComplaintVote.find_by_complainable_id(complaint_vote.complainable_id).destroy
elsif complaint_vote.cons - complaint_vote.pros > 2
@current_complaint = Complaint.where(complainable_id: @complaint_vote.complainable_id).first
@current_complaint.update(deleted_at: Time.now)
ComplaintVote.find_by_complainable_id(complaint_vote.complainable_id).destroy
end
end
end
\ No newline at end of file
class ComplaintVote < ApplicationRecord
include Trackable
belongs_to :complainable, polymorphic: true
validates_presence_of :complainable_id
acts_as_paranoid
has_paper_trail
end
......@@ -6,15 +6,15 @@ development:
adapter: postgresql
encoding: unicode
database: portalmec_dev
username: luan
password: luanmatheus
username: arthur
password: senhadopostgres
test:
adapter: postgresql
encoding: unicode
database: portalmec_test
username: luan
password: luanmatheus
username: arthur
password: senhadopostgres
# host: postgres
production:
......
......@@ -223,6 +223,8 @@ Rails.application.routes.draw do
end
end
resources :complaints, only: [:index, :show, :create], concerns: [:deletable, :evaluable, :relatable]
resources :learning_objects_complaints, only: [:index, :show, :create], concerns: [:deletable, :evaluable, :relatable]
resources :complaint_votes, except: [:destroy]
resources :languages, except: [:new, :edit]
resources :licenses, except: [:new, :edit]
resources :mime_types, except: [:new, :edit]
......
class ModifyUsersTable < ActiveRecord::Migration[7.0]
def change
change_table(:users) do |t|
## Lockable
t.integer :failed_attempts, :default => 0, :null => false # Only if lock strategy is :failed_attempts
t.string :unlock_token # Only if unlock strategy is :email or :both
t.datetime :locked_at
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment