<commit_before>defmodule PhxOembed.Api.SiteController do
  use PhxOembed.Web, :controller
  alias PhxOembed.{Site, Authorization}

  require IEx
  plug Guardian.Plug.EnsureAuthenticated, handler: PhxOembed.Api.SessionController
  plug :scrub_params, "site" when action in [:create]

  def show(conn, %{"id" => id}) do
    site = Repo.get(Site, id)
    user = Guardian.Plug.current_resource(conn)

    case Authorization.authorize(:site, :show, user, site) do
      true ->
        conn
        |> put_status(:ok)
        |> render("show.json", site: site)

      false ->
        conn
        |> put_status(:forbidden)
        |> render("error.json", error: "Not authorized")
    end
  end

  def index(conn, %{}) do
    user = Guardian.Plug.current_resource(conn)
    |> Repo.preload(:sites)

    sites = user.sites

    case Authorization.authorize(:site, :index, user) do
      true ->
        conn
        |> put_status(:ok)
        |> render("index.json", sites: sites)

      false ->
        conn
        |> put_status(:forbidden)
        |> render(PhxOembed.SessionView, "error.json", error: "Not authorized")
    end
  end

  def create(conn, %{"site" => site}) do
    user = Guardian.Plug.current_resource(conn)
    changeset = user
    |> build_assoc(:sites)
    |> Site.changeset(site)

    case Authorization.authorize(:site, :index, user) do
      true ->
        case Repo.insert(changeset) do
          {:ok, site} ->
            conn
            |> put_status(:ok)
            |> render("show.json", site: site)

          {:error, _} ->
            conn
            |> put_status(:unproccessable_entity)
            |> render("error.json", error: "Problem adding new site")
        end

      false ->
        conn
        |> put_status(:forbidden)
        |> render("error.json", error: "Not authorized")

    end
  end
end
<commit_msg>namespacing bugfix<commit_after>defmodule PhxOembed.Api.SiteController do
  use PhxOembed.Web, :controller
  alias PhxOembed.{Site, Authorization}

  require IEx
  plug Guardian.Plug.EnsureAuthenticated, handler: PhxOembed.Api.SessionController
  plug :scrub_params, "site" when action in [:create]

  def show(conn, %{"id" => id}) do
    site = Repo.get(Site, id)
    user = Guardian.Plug.current_resource(conn)

    case Authorization.authorize(:site, :show, user, site) do
      true ->
        conn
        |> put_status(:ok)
        |> render("show.json", site: site)

      false ->
        conn
        |> put_status(:forbidden)
        |> render("error.json", error: "Not authorized")
    end
  end

  def index(conn, %{}) do
    user = Guardian.Plug.current_resource(conn)
    |> Repo.preload(:sites)

    sites = user.sites

    case Authorization.authorize(:site, :index, user) do
      true ->
        conn
        |> put_status(:ok)
        |> render("index.json", sites: sites)

      false ->
        conn
        |> put_status(:forbidden)
        |> render(PhxOembed.Api.SessionView, "error.json", error: "Not authorized")
    end
  end

  def create(conn, %{"site" => site}) do
    user = Guardian.Plug.current_resource(conn)
    changeset = user
    |> build_assoc(:sites)
    |> Site.changeset(site)

    case Authorization.authorize(:site, :index, user) do
      true ->
        case Repo.insert(changeset) do
          {:ok, site} ->
            conn
            |> put_status(:ok)
            |> render("show.json", site: site)

          {:error, _} ->
            conn
            |> put_status(:unproccessable_entity)
            |> render("error.json", error: "Problem adding new site")
        end

      false ->
        conn
        |> put_status(:forbidden)
        |> render("error.json", error: "Not authorized")

    end
  end
end

{'fraction_non_alphanumeric': 0.13310029577843507, 'fraction_numerical': 0.0, 'mean_word_length': 2.3066666666666666, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 57, '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': '7546240', 'n_tokens_mistral': 1223, 'n_tokens_neox': 1146, 'n_words': 259}