<commit_before>class Api::V1::ItemsController < Api::V1::BaseController
  expose(:collection)

  expose(:items, ancestor: :collection) do
    collection.items.includes(:collection, :hosts, :creators, :interviewers, :interviewees, :producers, :guests, :contributors, :entities, :storage_configuration).includes(audio_files:[:tasks, :transcripts], contributions:[:person])
  end

  expose(:item)
  expose(:contributions, ancestor: :item)
  expose(:users_item, ancestor: :current_users_items)

  expose(:searched_item) do
    query_builder = QueryBuilder.new({query:"id:#{params[:id].to_i}"}, current_user)
    search_query = Search.new(items_index_name) do
      query_builder.query do |q|
        query &q
      end
      query_builder.filters do |f|
        filter f.type, f.value
      end
    end
    Item.search(search_query).response.first.tap do |item|
      if item.blank?
        raise ActiveRecord::RecordNotFound
      end 
    end
  end

  authorize_resource decent_exposure: true

  def update
    item.save
    respond_with :api, item
  end

  def show
    respond_with :api, searched_item
  end

  def create
    item.valid?
    item.save
    respond_with :api, item
  end

  def destroy
    users_item.destroy
    respond_with :api, users_item
  end

  private

  def current_users_items
    current_user.items
  end
end<commit_msg>/collections/:cid/items/:iid already does db lookup/authz with CanCan<commit_after>class Api::V1::ItemsController < Api::V1::BaseController
  expose(:collection)

  expose(:items, ancestor: :collection) do
    collection.items.includes(:collection, :hosts, :creators, :interviewers, :interviewees, :producers, :guests, :contributors, :entities, :storage_configuration).includes(audio_files:[:tasks, :transcripts], contributions:[:person])
  end

  expose(:item)
  expose(:contributions, ancestor: :item)
  expose(:users_item, ancestor: :current_users_items)

  authorize_resource decent_exposure: true

  def update
    item.save
    respond_with :api, item
  end

  def show
    respond_with :api, item
  end

  def create
    item.valid?
    item.save
    respond_with :api, item
  end

  def destroy
    users_item.destroy
    respond_with :api, users_item
  end

  private

  def current_users_items
    current_user.items
  end
end
{'fraction_non_alphanumeric': 0.10078878177037687, 'fraction_numerical': 0.0017528483786152498, 'mean_word_length': 3.9630434782608694, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '662040', 'n_tokens_mistral': 748, 'n_tokens_neox': 724, 'n_words': 167}