From df30a5d167ca152bd145f54b3c227005c6a316f4 Mon Sep 17 00:00:00 2001 From: Arthur Vilar <ahcv19@inf.ufpr.br> Date: Tue, 23 Aug 2022 11:11:36 -0300 Subject: [PATCH] Issue #3: Add curator route --- .../v1/complaint_votes_controller.rb | 83 +++++++++++++++++++ app/models/complaint_vote.rb | 10 +++ config/database.yml | 8 +- config/routes.rb | 2 + .../20220729140004_modify_users_table.rb | 13 --- 5 files changed, 99 insertions(+), 17 deletions(-) create mode 100644 app/controllers/v1/complaint_votes_controller.rb create mode 100644 app/models/complaint_vote.rb delete mode 100644 db/migrate/20220729140004_modify_users_table.rb diff --git a/app/controllers/v1/complaint_votes_controller.rb b/app/controllers/v1/complaint_votes_controller.rb new file mode 100644 index 00000000..9a158ddf --- /dev/null +++ b/app/controllers/v1/complaint_votes_controller.rb @@ -0,0 +1,83 @@ +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 diff --git a/app/models/complaint_vote.rb b/app/models/complaint_vote.rb new file mode 100644 index 00000000..364fad35 --- /dev/null +++ b/app/models/complaint_vote.rb @@ -0,0 +1,10 @@ +class ComplaintVote < ApplicationRecord + include Trackable + belongs_to :complainable, polymorphic: true + + validates_presence_of :complainable_id + + acts_as_paranoid + has_paper_trail + +end diff --git a/config/database.yml b/config/database.yml index 28a1dd5e..efe8ff8b 100644 --- a/config/database.yml +++ b/config/database.yml @@ -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: diff --git a/config/routes.rb b/config/routes.rb index bc40d692..31ad68eb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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] diff --git a/db/migrate/20220729140004_modify_users_table.rb b/db/migrate/20220729140004_modify_users_table.rb deleted file mode 100644 index d77eaad8..00000000 --- a/db/migrate/20220729140004_modify_users_table.rb +++ /dev/null @@ -1,13 +0,0 @@ -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 -- GitLab