<commit_before>class Api::V1::PropositionsController < Api::V1::ApplicationController

  # POST /api/v1/propositions
  def create
    params[:proposition] ||= {}
    tags = Tag.any_in(_id: params[:proposition][:tagIds].split(',')) if params[:proposition][:tagIds]
    @proposition = Proposition.new(
      text:         params[:proposition][:text],
      candidacy_id: params[:proposition][:candidacyId],
      tags:          tags
    )

    if @proposition.save
      render 'api/v1/propositions/show.rabl'
    else
      render text: {errors: @proposition.errors}.to_json, status: :unprocessable_entity, layout: 'api_v1'
    end
  end
  
  # GET /api/v1/propositions/search
  # params electionIds, tagIds, candidacyIds
  def search
    # query
    conditions = {}
    conditions[:tag_ids.in] = params[:tagIds].split(',') unless params[:tagIds].blank?
    conditions[:candidacy_id.in] = params[:candidacyIds].split(',') unless params[:candidacyIds].blank?
    
    # pagination
    skip = params[:offset] || 0
    @propositions = Proposition.includes(:candidacy).where(conditions).limit(500).skip(skip)
  end
  
  # GET /api/v1/propositions
  def show
  end

  # PUT /api/v1/propositions/1
  def update
    params[:proposition] ||= {}
    if params[:proposition][:tagIds]
      @proposition.tags = Tag.any_in(_id: params[:proposition][:tagIds].split(','))
    end
    if params[:proposition][:text]
      @proposition.text = params[:proposition][:text]
    end

    if @proposition.save
      render 'api/v1/propositions/show.rabl'
    else
      render text: {errors: @proposition.errors}.to_json, status: :unprocessable_entity, layout: 'api_v1'
    end
  end

  # DELETE /api/v1/propositions/1
  def destroy
    @proposition.destroy
    head :ok
  end

  # POST /api/v1/propositions/1/addcomment
  def addcomment
    @comment = @proposition.comments.build params[:comment]
    @comment.user = current_user

    if @comment.save
      head :ok
    else
      render text: {errors: @comment.errors}.to_json, status: :unprocessable_entity, layout: 'api_v1'
    end
  end

  # GET /api/v1/propositions/1/comments
  def comments
    @comments = @proposition.comments.includes(:user)
  end

  # POST /api/v1/propositions/1/addembed
  def addembed
    embed = @proposition.embeds.build url: params[:url]
    if embed.save
      render 'api/v1/propositions/show.rabl'
    else
      render text: {errors: embed.errors}.to_json, status: :unprocessable_entity, layout: 'api_v1'
    end
  end

  # DELETE /api/v1/propositions/1/removeembed
  def removeembed
    embed = @proposition.embeds.find params[:embedId]
    if embed.destroy
      head :ok
    else
      render text: {errors: embed.errors}.to_json, status: :unprocessable_entity, layout: 'api_v1'
    end
  end
end<commit_msg>Patch youtube url shortner errors<commit_after>class Api::V1::PropositionsController < Api::V1::ApplicationController

  # POST /api/v1/propositions
  def create
    params[:proposition] ||= {}
    tags = Tag.any_in(_id: params[:proposition][:tagIds].split(',')) if params[:proposition][:tagIds]
    @proposition = Proposition.new(
      text:         params[:proposition][:text],
      candidacy_id: params[:proposition][:candidacyId],
      tags:          tags
    )

    if @proposition.save
      render 'api/v1/propositions/show.rabl'
    else
      render text: {errors: @proposition.errors}.to_json, status: :unprocessable_entity, layout: 'api_v1'
    end
  end
  
  # GET /api/v1/propositions/search
  # params electionIds, tagIds, candidacyIds
  def search
    # query
    conditions = {}
    conditions[:tag_ids.in] = params[:tagIds].split(',') unless params[:tagIds].blank?
    conditions[:candidacy_id.in] = params[:candidacyIds].split(',') unless params[:candidacyIds].blank?
    
    # pagination
    skip = params[:offset] || 0
    @propositions = Proposition.includes(:candidacy).where(conditions).limit(500).skip(skip)
  end
  
  # GET /api/v1/propositions
  def show
  end

  # PUT /api/v1/propositions/1
  def update
    params[:proposition] ||= {}
    if params[:proposition][:tagIds]
      @proposition.tags = Tag.any_in(_id: params[:proposition][:tagIds].split(','))
    end
    if params[:proposition][:text]
      @proposition.text = params[:proposition][:text]
    end

    if @proposition.save
      render 'api/v1/propositions/show.rabl'
    else
      render text: {errors: @proposition.errors}.to_json, status: :unprocessable_entity, layout: 'api_v1'
    end
  end

  # DELETE /api/v1/propositions/1
  def destroy
    @proposition.destroy
    head :ok
  end

  # POST /api/v1/propositions/1/addcomment
  def addcomment
    @comment = @proposition.comments.build params[:comment]
    @comment.user = current_user

    if @comment.save
      head :ok
    else
      render text: {errors: @comment.errors}.to_json, status: :unprocessable_entity, layout: 'api_v1'
    end
  end

  # GET /api/v1/propositions/1/comments
  def comments
    @comments = @proposition.comments.includes(:user)
  end

  # POST /api/v1/propositions/1/addembed
  def addembed
    transform_youtube_url_shortner_links
    embed = @proposition.embeds.build url: params[:url]
    if embed.save
      render 'api/v1/propositions/show.rabl'
    else
      render text: {errors: embed.errors}.to_json, status: :unprocessable_entity, layout: 'api_v1'
    end
  end

  # DELETE /api/v1/propositions/1/removeembed
  def removeembed
    embed = @proposition.embeds.find params[:embedId]
    if embed.destroy
      head :ok
    else
      render text: {errors: embed.errors}.to_json, status: :unprocessable_entity, layout: 'api_v1'
    end
  end

  protected
    def transform_youtube_url_shortner_links
      params[:url] = params[:url].gsub /^http:\/\/youtu.be\/(\w+)$/, 'http://www.youtube.com/watch?v=\\1'
    end
end
{'fraction_non_alphanumeric': 0.130810437186798, 'fraction_numerical': 0.010195265249697598, 'mean_word_length': 3.319402985074627, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, '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': '4257371', 'n_tokens_mistral': 1947, 'n_tokens_neox': 1934, 'n_words': 376}