<commit_before>defmodule FarmHandler do
  @moduledoc """
    Handles farm-related http requests
  """

  require EEx
  require Farm

  # HTML page template functions
  EEx.function_from_file :defp, :farm_index, "priv/templates/farms/index.html.eex", [:farms, :user_filters]
  EEx.function_from_file :defp, :farm_new,   "priv/templates/farms/new.html.eex",   []
  EEx.function_from_file :defp, :farm_edit,  "priv/templates/farms/edit.html.eex",  []
  EEx.function_from_file :defp, :farm_model, "priv/templates/farms/model.html.eex", [:id, :user_id, :username, :name, :description]
  EEx.function_from_file :defp, :filter,     "priv/templates/search/user_filter.html.eex", [:user_id, :username]

  @doc """
    Initializes the http handler
  """
  def init({ _any, :http }, req, []) do
    { :ok, req, :undefined }
  end

  @doc """
    handles page requests
  """
  def handle(req, state) do
    {user_id, req} = :cowboy_req.cookie(<<"userid">>, req, nil)
    { id, req} = :cowboy_req.binding(:id, req, :all)
    case id do
      "new" ->
          content = farm_new()
          options = [
            title: <<"Create New Farm">>,
            controller: :farms,
            user_id: user_id,
            maps: true,
            scripts: ["/js/farm_editor.js"]
          ]
          {:ok, req} = :cowboy_req.reply 200, [{"Content-Type", "text/html"}], Layout.page(content, options), req
      :all ->
         {xhr, req} = :cowboy_req.header(<<"x-requested-with">>, req)
         if(xhr == "XMLHttpRequest") do
           {query, req} = :cowboy_req.qs_vals(req)
           reply = Database.list_farms(query, 0)
             |> Enum.map(fn {id, user_id, username, name, desc} -> farm_model(id, user_id, username, name, desc) end)
           {:ok, req} = :cowboy_req.reply 200, [{"Content-Type", "text/html"}], reply, req
         else
           if user_id do
             user_filters = [{user_id, Database.user(user_id).username}] 
               |> Enum.map(fn {uid, uname} -> filter(uid, uname) end)
               |> Enum.join
           else
             user_filters = nil
           end
           content = Database.list_farms 
             |> Enum.map(fn {id, user_id, username, name, desc} -> farm_model(id, user_id, username, name, desc) end)
             |> farm_index(user_filters)
           options = [
             title: <<"Farm Models">>,
             controller: :farms,
             user_id: user_id,
             scripts: ["js/search.js"]
           ]
           {:ok, req} = :cowboy_req.reply 200, [{"Content-Type", "text/html"}], Layout.page(content, options), req
         end
      _ ->
         options = [
           title: <<"Farm Model Editor">>, 
           controller: :plants, 
           user_id: user_id,
           maps: true,
           scripts: ["/js/farm_editor.js"]
         ]
         content = farm_edit()
         {:ok, req} = :cowboy_req.reply 200, [{"Content-Type", "text/html"}], Layout.page(content, options), req
    end
    {:ok, req, state}
  end

  @doc """
    Shuts down the handler
  """
  def terminate(_reason, _request, _state) do
    :ok
  end

end
<commit_msg>Changed farm/edit controller to farms<commit_after>defmodule FarmHandler do
  @moduledoc """
    Handles farm-related http requests
  """

  require EEx
  require Farm

  # HTML page template functions
  EEx.function_from_file :defp, :farm_index, "priv/templates/farms/index.html.eex", [:farms, :user_filters]
  EEx.function_from_file :defp, :farm_new,   "priv/templates/farms/new.html.eex",   []
  EEx.function_from_file :defp, :farm_edit,  "priv/templates/farms/edit.html.eex",  []
  EEx.function_from_file :defp, :farm_model, "priv/templates/farms/model.html.eex", [:id, :user_id, :username, :name, :description]
  EEx.function_from_file :defp, :filter,     "priv/templates/search/user_filter.html.eex", [:user_id, :username]

  @doc """
    Initializes the http handler
  """
  def init({ _any, :http }, req, []) do
    { :ok, req, :undefined }
  end

  @doc """
    handles page requests
  """
  def handle(req, state) do
    {user_id, req} = :cowboy_req.cookie(<<"userid">>, req, nil)
    { id, req} = :cowboy_req.binding(:id, req, :all)
    case id do
      "new" ->
          content = farm_new()
          options = [
            title: <<"Create New Farm">>,
            controller: :farms,
            user_id: user_id,
            maps: true,
            scripts: ["/js/farm_editor.js"]
          ]
          {:ok, req} = :cowboy_req.reply 200, [{"Content-Type", "text/html"}], Layout.page(content, options), req
      :all ->
         {xhr, req} = :cowboy_req.header(<<"x-requested-with">>, req)
         if(xhr == "XMLHttpRequest") do
           {query, req} = :cowboy_req.qs_vals(req)
           reply = Database.list_farms(query, 0)
             |> Enum.map(fn {id, user_id, username, name, desc} -> farm_model(id, user_id, username, name, desc) end)
           {:ok, req} = :cowboy_req.reply 200, [{"Content-Type", "text/html"}], reply, req
         else
           if user_id do
             user_filters = [{user_id, Database.user(user_id).username}] 
               |> Enum.map(fn {uid, uname} -> filter(uid, uname) end)
               |> Enum.join
           else
             user_filters = nil
           end
           content = Database.list_farms 
             |> Enum.map(fn {id, user_id, username, name, desc} -> farm_model(id, user_id, username, name, desc) end)
             |> farm_index(user_filters)
           options = [
             title: <<"Farm Models">>,
             controller: :farms,
             user_id: user_id,
             scripts: ["js/search.js"]
           ]
           {:ok, req} = :cowboy_req.reply 200, [{"Content-Type", "text/html"}], Layout.page(content, options), req
         end
      _ ->
         options = [
           title: <<"Farm Model Editor">>, 
           controller: :farms, 
           user_id: user_id,
           maps: true,
           scripts: ["/js/farm_editor.js"]
         ]
         content = farm_edit()
         {:ok, req} = :cowboy_req.reply 200, [{"Content-Type", "text/html"}], Layout.page(content, options), req
    end
    {:ok, req, state}
  end

  @doc """
    Shuts down the handler
  """
  def terminate(_reason, _request, _state) do
    :ok
  end

end

{'fraction_non_alphanumeric': 0.15120549257544308, 'fraction_numerical': 0.00415136516046623, 'mean_word_length': 2.215605749486653, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 45, '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': '5659728', 'n_tokens_mistral': 2159, 'n_tokens_neox': 2115, 'n_words': 474}