<commit_before>#
# == connection_pools.ex
#
# This module contains the controllers for managing MessagingBrokers
#
require Logger

defmodule OpenAperture.Manager.Controllers.MessagingBrokers do
  use OpenAperture.Manager.Web, :controller

  require Repo

  alias OpenAperture.Manager.Endpoint
  alias OpenAperture.Manager.DB.Models.MessagingBroker
  alias OpenAperture.Manager.DB.Models.MessagingBrokerConnection
  alias OpenAperture.Manager.DB.Models.MessagingExchangeBroker
  
  alias OpenAperture.Manager.Controllers.FormatHelper
  
  import Ecto.Query

  # TODO: Add authentication

  plug :action

  @moduledoc """
  This module contains the controllers for managing MessagingBrokers
  """  

  @sendable_broker_fields [:id, :name, :inserted_at, :failover_broker_id, :updated_at]
  @updatable_broker_fields ["name", "failover_broker_id"]

	@sendable_broker_connection_fields [:id, :messaging_broker_id, :username, :password, :host, :port, :virtual_host, :inserted_at, :updated_at]

  @doc """
  GET /messaging/brokers - Retrieve all MessagingBrokers

  ## Options
  The `conn` option defines the underlying HTTP connection.
  The `params` option defines an array of arguments.

  ## Return Values

  Underlying HTTP connection
  """
  @spec index(term, [any]) :: term
  def index(conn, _params) do
    json conn, Repo.all(MessagingBroker)
  end

  @doc """
  GET /messaging/brokers/:id - Retrieve a MessagingBroker

  ## Options
  The `conn` option defines the underlying HTTP connection.
  The `params` option defines an array of arguments.

  ## Return Values

  Underlying HTTP connection
  """
  @spec show(term, [any]) :: term
  def show(conn, %{"id" => id}) do
    case Repo.get(MessagingBroker, id) do
      nil -> resp(conn, :not_found, "")
      broker -> json conn, broker |> FormatHelper.to_sendable(@sendable_broker_fields)
    end
  end

  @doc """
  POST /messaging/brokers - Create a MessagingBroker

  ## Options
  The `conn` option defines the underlying HTTP connection.
  The `params` option defines an array of arguments.

  ## Return Values

  Underlying HTTP connection
  """
  @spec create(term, [any]) :: term
  def create(conn, %{"name" => name} = _params) when name != "" do
    query = from b in MessagingBroker,
      where: b.name == ^name,
      select: b
    case Repo.all(query) do
      [] ->
        changeset = MessagingBroker.new(%{"name" => name})
        if changeset.valid? do
          try do
            broker = Repo.insert(changeset)
            path = OpenAperture.Manager.Router.Helpers.messaging_brokers_path(Endpoint, :show, broker.id)

            # Set location header
            conn
            |> put_resp_header("location", path)
            |> resp(:created, "")
          rescue
            e ->
              Logger.error("Error inserting broker record for #{name}: #{inspect e}")
              resp(conn, :internal_server_error, "")
          end
        else
          conn
          |> put_status(:bad_request)
          |> json FormatHelper.keywords_to_map(changeset.errors)
        end
      _ ->
        conn |> resp(:conflict, "")
    end
  end

  # This action only matches if a param is missing
  def create(conn, _params) do
    Plug.Conn.resp(conn, :bad_request, "name is required")
  end

  @doc """
  PUT/PATCH /messaging/brokers/:id - Update a MessagingBroker

  ## Options
  The `conn` option defines the underlying HTTP connection.
  The `params` option defines an array of arguments.

  ## Return Values

  Underlying HTTP connection
  """
  @spec update(term, [any]) :: term
  def update(conn, %{"id" => id} = params) do
    broker = Repo.get(MessagingBroker, id)

    if broker == nil do
      resp(conn, :not_found, "")
    else
    	changeset = MessagingBroker.new(%{"name" => params["name"]})
      if changeset.valid? do
        # Check to see if there is another broker with the same name
    		query = from b in MessagingBroker,
          where: b.name == ^params["name"],
          select: b

        case Repo.all(query) do
          [] ->
          	changeset = MessagingBroker.changeset(broker, Map.take(params, @updatable_broker_fields))

          	try do
	            Repo.update(changeset)
	            path = OpenAperture.Manager.Router.Helpers.messaging_brokers_path(Endpoint, :show, id)
	            conn
	            |> put_resp_header("location", path)
	            |> resp(:no_content, "")
	          rescue
	            e ->
	              Logger.error("Error inserting broker record for #{params["name"]}: #{inspect e}")
	              resp(conn, :internal_server_error, "")
	          end	            
          _ ->
            resp(conn, :conflict, "")
        end
      else
        conn
        |> put_status(:bad_request)<commit_msg>make sure to save failover_broker_id when provided<commit_after>#
# == connection_pools.ex
#
# This module contains the controllers for managing MessagingBrokers
#
require Logger

defmodule OpenAperture.Manager.Controllers.MessagingBrokers do
  use OpenAperture.Manager.Web, :controller

  require Repo

  alias OpenAperture.Manager.Endpoint
  alias OpenAperture.Manager.DB.Models.MessagingBroker
  alias OpenAperture.Manager.DB.Models.MessagingBrokerConnection
  alias OpenAperture.Manager.DB.Models.MessagingExchangeBroker
  
  alias OpenAperture.Manager.Controllers.FormatHelper
  
  import Ecto.Query

  # TODO: Add authentication

  plug :action

  @moduledoc """
  This module contains the controllers for managing MessagingBrokers
  """  

  @sendable_broker_fields [:id, :name, :inserted_at, :failover_broker_id, :updated_at]
  @updatable_broker_fields ["name", "failover_broker_id"]

	@sendable_broker_connection_fields [:id, :messaging_broker_id, :username, :password, :host, :port, :virtual_host, :inserted_at, :updated_at]

  @doc """
  GET /messaging/brokers - Retrieve all MessagingBrokers

  ## Options
  The `conn` option defines the underlying HTTP connection.
  The `params` option defines an array of arguments.

  ## Return Values

  Underlying HTTP connection
  """
  @spec index(term, [any]) :: term
  def index(conn, _params) do
    json conn, Repo.all(MessagingBroker)
  end

  @doc """
  GET /messaging/brokers/:id - Retrieve a MessagingBroker

  ## Options
  The `conn` option defines the underlying HTTP connection.
  The `params` option defines an array of arguments.

  ## Return Values

  Underlying HTTP connection
  """
  @spec show(term, [any]) :: term
  def show(conn, %{"id" => id}) do
    case Repo.get(MessagingBroker, id) do
      nil -> resp(conn, :not_found, "")
      broker -> json conn, broker |> FormatHelper.to_sendable(@sendable_broker_fields)
    end
  end

  @doc """
  POST /messaging/brokers - Create a MessagingBroker

  ## Options
  The `conn` option defines the underlying HTTP connection.
  The `params` option defines an array of arguments.

  ## Return Values

  Underlying HTTP connection
  """
  @spec create(term, [any]) :: term
  def create(conn, %{"name" => name} = params) when name != "" do
    query = from b in MessagingBroker,
      where: b.name == ^name,
      select: b
    case Repo.all(query) do
      [] ->
        changeset = MessagingBroker.new(%{
          "name" => name,
          "failover_broker_id" => params["failover_broker_id"]
        })
        if changeset.valid? do
          try do
            broker = Repo.insert(changeset)
            path = OpenAperture.Manager.Router.Helpers.messaging_brokers_path(Endpoint, :show, broker.id)

            # Set location header
            conn
            |> put_resp_header("location", path)
            |> resp(:created, "")
          rescue
            e ->
              Logger.error("Error inserting broker record for #{name}: #{inspect e}")
              resp(conn, :internal_server_error, "")
          end
        else
          conn
          |> put_status(:bad_request)
          |> json FormatHelper.keywords_to_map(changeset.errors)
        end
      _ ->
        conn |> resp(:conflict, "")
    end
  end

  # This action only matches if a param is missing
  def create(conn, _params) do
    Plug.Conn.resp(conn, :bad_request, "name is required")
  end

  @doc """
  PUT/PATCH /messaging/brokers/:id - Update a MessagingBroker

  ## Options
  The `conn` option defines the underlying HTTP connection.
  The `params` option defines an array of arguments.

  ## Return Values

  Underlying HTTP connection
  """
  @spec update(term, [any]) :: term
  def update(conn, %{"id" => id} = params) do
    broker = Repo.get(MessagingBroker, id)

    if broker == nil do
      resp(conn, :not_found, "")
    else
    	changeset = MessagingBroker.new(%{
        "name" => params["name"],
        "failover_broker_id" => params["failover_broker_id"]
      })
      if changeset.valid? do
        # Check to see if there is another broker with the same name
    		query = from b in MessagingBroker,
          where: b.name == ^params["name"],
          select: b

        case Repo.all(query) do
          [] ->
          	changeset = MessagingBroker.changeset(broker, Map.take(params, @updatable_broker_fields))

          	try do
	            Repo.update(changeset)
	            path = OpenAperture.Manager.Router.Helpers.messaging_brokers_path(Endpoint, :show, id)
	            conn
	            |> put_resp_header("location", path)
	            |> resp(:no_content, "")
	          rescue
	            e ->
	              Logger.error("Error inserting broker record for #{params["name"]}: #{inspect e}")
	              resp(conn, :internal_server_error, "")
	          end	            
          _ ->
            resp(conn, :conflict, "")
        end
      else
        conn
        |> put_status(:bad_request)
{'fraction_non_alphanumeric': 0.10762239206775459, 'fraction_numerical': 0.0, 'mean_word_length': 2.6608695652173915, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 49, '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': '126319', 'n_tokens_mistral': 3015, 'n_tokens_neox': 2893, 'n_words': 781}