<commit_before>defmodule BaumeisterWeb.ProjectController do
  use BaumeisterWeb.Web, :controller

  alias BaumeisterWeb.Project
  alias BaumeisterWeb.ProjectBridge

  def index(conn, _params) do
    projects = Repo.all(Project)
    render(conn, "index.html", projects: projects)
  end

  def new(conn, _params) do
    changeset = Project.changeset(%Project{})
    render(conn, "new.html", changeset: changeset)
  end

  def create(conn, %{"project" => project_params}) do
    changeset = Project.changeset(%Project{}, project_params)

    case insert(changeset) do
      {:ok, project} ->
        conn
        |> put_flash(:info, "Project created successfully.")
        |> redirect(to: project_path(conn, :index))
      {:error, changeset} ->
        render(conn, "new.html", changeset: changeset)
    end
  end

  @doc """
  Inserts the project into the database and the baumeister coordinator.
  """
  @spec insert(Project.t | %{}) :: {:ok, Project.t} | {:error, any}
  def insert(project) do
    case Repo.insert(project) do
      {:ok, project} ->
        :ok = ProjectBridge.add_project_to_coordinator(project)
        enabled = project.enabled
        ^enabled = ProjectBridge.set_status(project)
        {:ok, project}
      {:error, changeset} -> {:error, changeset}
    end
  end

  def show(conn, %{"id" => id}) do
    project = Repo.get!(Project, id)<commit_msg>Ensure that the name of a project is unique in both databases<commit_after>defmodule BaumeisterWeb.ProjectController do
  use BaumeisterWeb.Web, :controller

  alias BaumeisterWeb.Project
  alias BaumeisterWeb.ProjectBridge

  require Logger

  def index(conn, _params) do
    projects = Repo.all(Project)
    render(conn, "index.html", projects: projects)
  end

  def new(conn, _params) do
    changeset = Project.changeset(%Project{})
    render(conn, "new.html", changeset: changeset)
  end

  def create(conn, %{"project" => project_params}) do
    changeset = Project.changeset(%Project{}, project_params)

    case insert(changeset) do
      {:ok, _project} ->
        conn
        |> put_flash(:info, "Project created successfully.")
        |> redirect(to: project_path(conn, :index))
      {:error, changeset} ->
        Logger.error("Error inserting project: #{inspect changeset}")
        render(conn, "new.html", changeset: changeset)
    end
  end

  @doc """
  Inserts the project into the database and the baumeister coordinator.
  """
  @spec insert(Project.t | %{}) :: {:ok, Project.t} | {:error, any}
  def insert(changeset) do
    case Repo.insert(changeset) do
      {:ok, project} ->
        case ProjectBridge.add_project_to_coordinator(project) do
          :ok -> enabled = project.enabled
                 ^enabled = ProjectBridge.set_status(project)
                 {:ok, project}
          {:error, msg} ->
            Logger.error("Error inserting project into core: #{inspect changeset}")
            Repo.delete!(project)
            {:error, %{changeset | action: :insert}
            |> Ecto.Changeset.add_error(:name, msg)
          }
        end
      {:error, new_changeset} -> {:error, new_changeset}
    end
  end

  def show(conn, %{"id" => id}) do
    project = Repo.get!(Project, id)
{'fraction_non_alphanumeric': 0.1278524538918412, 'fraction_numerical': 0.0, 'mean_word_length': 3.04040404040404, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, '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': '6436920', 'n_tokens_mistral': 1005, 'n_tokens_neox': 968, 'n_words': 252}