<commit_before>defmodule WhistlerNewsReader.Api.EntryView do
  use WhistlerNewsReader.Web, :view
  alias WhistlerNewsReader.UnreadEntry
  alias WhistlerNewsReader.Repo

  # TODO: refactor current_user handling
  def render("index.json", %{entries: entries, current_user: current_user}) do
    %{
      entries: Enum.map(entries, fn(entry) ->
        %{
          id: entry.id,
          title: entry.title,
          published: entry.published,
          author: entry.author,
          summary: entry.summary,
          content: entry.content,
          url: entry.url,
          unread: unread?(entry, current_user),
          feed: %{
            title: entry.feed.title
          }
        }
      end)
    }
  end

  def render("entry.json", %{entry: entry, current_user: current_user}) do
    %{
      id: entry.id,
      title: entry.title,
      published: entry.published,
      author: entry.author,
      summary: entry.summary,
      content: entry.content,
      url: entry.url,
      unread: unread?(entry, current_user),
      feed: %{
        title: entry.feed.title
      }
    }
  end

  defp unread?(entry, current_user) do
    unread_entries = UnreadEntry |> UnreadEntry.for_entry(entry.id) |> UnreadEntry.for_user(current_user.id) |> Repo.all
    Enum.any?(unread_entries, fn(e) -> e.entry_id == entry.id end)
  end

end<commit_msg>add todo for n+1 query<commit_after>defmodule WhistlerNewsReader.Api.EntryView do
  use WhistlerNewsReader.Web, :view
  alias WhistlerNewsReader.UnreadEntry
  alias WhistlerNewsReader.Repo

  # TODO: refactor current_user handling
  def render("index.json", %{entries: entries, current_user: current_user}) do
    %{
      entries: Enum.map(entries, fn(entry) ->
        %{
          id: entry.id,
          title: entry.title,
          published: entry.published,
          author: entry.author,
          summary: entry.summary,
          content: entry.content,
          url: entry.url,
          unread: unread?(entry, current_user),
          feed: %{
            title: entry.feed.title
          }
        }
      end)
    }
  end

  def render("entry.json", %{entry: entry, current_user: current_user}) do
    %{
      id: entry.id,
      title: entry.title,
      published: entry.published,
      author: entry.author,
      summary: entry.summary,
      content: entry.content,
      url: entry.url,
      unread: unread?(entry, current_user),
      feed: %{
        title: entry.feed.title
      }
    }
  end

  # TODO: n+1 query! Refactor using eager loading of unread_entries association instead
  defp unread?(entry, current_user) do
    unread_entries = UnreadEntry |> UnreadEntry.for_entry(entry.id) |> UnreadEntry.for_user(current_user.id) |> Repo.all
    Enum.any?(unread_entries, fn(e) -> e.entry_id == entry.id end)
  end

end
{'fraction_non_alphanumeric': 0.11237959329290047, 'fraction_numerical': 0.0007135212272565109, 'mean_word_length': 2.4320685434516522, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '6483444', 'n_tokens_mistral': 895, 'n_tokens_neox': 890, 'n_words': 194}