diff --git a/app/controllers/v1/complaint_votes_controller.rb b/app/controllers/v1/complaint_votes_controller.rb new file mode 100644 index 0000000000000000000000000000000000000000..9a158ddf332e09dfc67c61f1365872e49252d0d3 --- /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 0000000000000000000000000000000000000000..364fad3560cb60de94cc7f254f0bc2813fc6bb96 --- /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 28a1dd5ec120c436f9feb8ead10f1b666d7088c0..efe8ff8bba66a0ddb4282beca6915ad9b7294a48 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 bc40d6920867908bfb49d5b915ef36968ccae62a..31ad68eb4bb5bad97c80701f9bd54a59b0564943 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 d77eaad8fa0cae1f9e9f92a4c70a2dd5bed3cc29..0000000000000000000000000000000000000000 --- 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