<commit_before>require "json"

module RailsRestVote
  class Api::VotesController < ApplicationController
    #No record found exception handled
    rescue_from ActiveRecord::RecordNotFound, with: :not_found

    #This would turn off the CSRF check for json api posts/puts
    skip_before_filter :verify_authenticity_token

    #check current_user existance before voting actions
    before_action :current_user?, :only=>[:up, :down , :like, :exists]

    #check user already voted or not
    before_action :exists?, :only=>[:up, :down, :like]

    #intialize user
    before_action :initialize_user, :only=>[:user_vote_count, :user_like_count]

    #intialize votable object
    before_action :initialize_votable_object, :only=>[:model_vote_count, :model_like_count]


    #up vote is created form current user reference.
    #vote value should be true.
    #return success json i.e {"status":200,"message":"upvoted successfully."}
    #
    def up
      upvote = current_user.votes.new(vote_params)
      upvote.vote = true
      send_post_response(upvote)
    end

    #down vote is created form current user reference.
    #vote value should be false.
    #return success json i.e {"status":200,"message":"downvoted successfully."}
    #
    def down
      downvote = current_user.votes.new(vote_params)
      downvote.vote = false
      send_post_response(downvote)
    end

    #return vote up and down count of particular user.
    #{"status":200,"upcount":1,"upvotes":[{"id":1,...}], "downcount":2,"downvotes":[{"id":3,...},{"id":2,...}]}
    #
    def user_vote_count
      upvotes = @user.votes.where(:vote => true)
      downvotes = @user.votes.where(:vote => false)
      send_get_response(upvotes,downvotes)
    end

    #return vote up and down count on particular model.
    #{"status":200,"upcount":1,"upvotes":[{"id":1,...}], "downcount":2,"downvotes":[{"id":3,...},{"id":2,...}]}
    #
    def model_vote_count
      upvotes = @votable.votes.where(:vote => true)
      downvotes = @votable.votes.where(:vote => false)
      send_get_response(upvotes,downvotes)
    end

    #like and unlike feature handled from this method
    #if record already present it just delete.
    #response { status: :ok, message: "unliked successfully." })
    #else response { status: :ok, message: "liked successfully." })
    #
    def like
      like = current_user.votes.new(vote_params)
      send_post_response(like)
    end

    #return like count of particular user.
    #{"status":200,"likecount":1,"likes":[{"id":1,...}]}
    #
    def user_like_count
      likes = @user.votes
      send_like_response(likes)
    end

    #return like count on particular model.
    #{"status":200,"likecount":1,"likes":[{"id":1,...}]}
    #
    def model_like_count
      likes = @votable.votes
      send_like_response(likes)
    end

    private

    #check existance of record before creation
    def exists?
      params[:vote][:votable_type] = params[:vote][:votable_type].capitalize
      vote = params[:action] == "like" ? nil :  params[:action] == "up" ? true : false
      uservotes = current_user.votes.where("votable_id = ? AND votable_type = ? AND vote = ?",params[:vote][:votable_id],params[:vote][:votable_type],vote)
      return uservotes.size > 0 ? voted(uservotes) : true
    end

    #intialize user object from params[:user_id]
    def initialize_user
      @user = User.find(params[:user_id])
    end

    #intialize votable object from params[:votable_id]<commit_msg>check exsistance of voted objected<commit_after>require "json"

module RailsRestVote
  class Api::VotesController < ApplicationController
    #No record found exception handled
    rescue_from ActiveRecord::RecordNotFound, with: :not_found

    #This would turn off the CSRF check for json api posts/puts
    skip_before_filter :verify_authenticity_token

    #check current_user existance before voting actions
    before_action :current_user?, :only=>[:up, :down , :like, :exists]

    #check user already voted or not
    before_action :exists?, :only=>[:up, :down, :like]

    #intialize user
    before_action :initialize_user, :only=>[:user_vote_count, :user_like_count]

    #intialize votable object
    before_action :initialize_votable_object, :only=>[:model_vote_count, :model_like_count]


    #up vote is created form current user reference.
    #vote value should be true.
    #return success json i.e {"status":200,"message":"upvoted successfully."}
    #
    def up
      upvote = current_user.votes.new(vote_params)
      upvote.vote = true
      send_post_response(upvote)
    end

    #down vote is created form current user reference.
    #vote value should be false.
    #return success json i.e {"status":200,"message":"downvoted successfully."}
    #
    def down
      downvote = current_user.votes.new(vote_params)
      downvote.vote = false
      send_post_response(downvote)
    end

    #return vote up and down count of particular user.
    #{"status":200,"upcount":1,"upvotes":[{"id":1,...}], "downcount":2,"downvotes":[{"id":3,...},{"id":2,...}]}
    #
    def user_vote_count
      upvotes = @user.votes.where(:vote => true)
      downvotes = @user.votes.where(:vote => false)
      send_get_response(upvotes,downvotes)
    end

    #return vote up and down count on particular model.
    #{"status":200,"upcount":1,"upvotes":[{"id":1,...}], "downcount":2,"downvotes":[{"id":3,...},{"id":2,...}]}
    #
    def model_vote_count
      upvotes = @votable.votes.where(:vote => true)
      downvotes = @votable.votes.where(:vote => false)
      send_get_response(upvotes,downvotes)
    end

    #like and unlike feature handled from this method
    #if record already present it just delete.
    #response { status: :ok, message: "unliked successfully." })
    #else response { status: :ok, message: "liked successfully." })
    #
    def like
      like = current_user.votes.new(vote_params)
      send_post_response(like)
    end

    #return like count of particular user.
    #{"status":200,"likecount":1,"likes":[{"id":1,...}]}
    #
    def user_like_count
      likes = @user.votes
      send_like_response(likes)
    end

    #return like count on particular model.
    #{"status":200,"likecount":1,"likes":[{"id":1,...}]}
    #
    def model_like_count
      likes = @votable.votes
      send_like_response(likes)
    end

    private

    #check existance of record before creation
    def exists?
      initialize_votable_object
      params[:vote][:votable_type] = params[:vote][:votable_type].capitalize
      vote = params[:action] == "like" ? nil :  params[:action] == "up" ? true : false
      uservotes = current_user.votes.where("votable_id = ? AND votable_type = ? AND vote = ?",params[:vote][:votable_id],params[:vote][:votable_type],vote)
      return uservotes.size > 0 ? voted(uservotes) : true
    end

    #intialize user object from params[:user_id]
    def initialize_user
      @user = User.find(params[:user_id])
    end

    #intialize votable object from params[:votable_id]
{'fraction_non_alphanumeric': 0.13257305773342837, 'fraction_numerical': 0.009408410548823949, 'mean_word_length': 3.514800514800515, 'pattern_counts': {'":': 56, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '673375', 'n_tokens_mistral': 2316, 'n_tokens_neox': 2224, 'n_words': 540}