From 78e61cb2b36f2adb02cc80e520c1674eab2f4e7c Mon Sep 17 00:00:00 2001 From: Jean-Louis Huynen Date: Fri, 10 Jan 2025 10:26:52 +0100 Subject: [PATCH] chg: [connections] specify whether connections can be full duplex - continued --- lib/cocktailparty/input.ex | 19 ++++++-- lib/cocktailparty/input/connection_type.ex | 2 +- .../admin/connection_controller.ex | 7 ++- .../controllers/admin/connection_html.ex | 1 - .../connection_html/connection_form.html.heex | 47 +++++++++---------- .../admin/connection_html/edit.html.heex | 5 +- 6 files changed, 45 insertions(+), 36 deletions(-) diff --git a/lib/cocktailparty/input.ex b/lib/cocktailparty/input.ex index c9bdeba..09a17ef 100644 --- a/lib/cocktailparty/input.ex +++ b/lib/cocktailparty/input.ex @@ -134,6 +134,11 @@ defmodule Cocktailparty.Input do |> Repo.preload(:sources) end + def get_fullduplex!(id) do + Repo.one(from c in Connection, where: c.id == ^id, select: c.type) + |> ConnectionTypes.get_full_duplex() + end + @doc """ Switch Connection's config representation between YAML string and map """ @@ -180,8 +185,8 @@ defmodule Cocktailparty.Input do end end - defp validate_full_duplex(changeset) do - if ConnectionTypes.get_full_duplex (get_field(changeset, :type)) == true do + def validate_full_duplex(changeset) do + if ConnectionTypes.get_full_duplex(get_field(changeset, :type)) == true do changeset else add_error(changeset, :sink, "This connection type does not support fullduplex connections") @@ -205,7 +210,15 @@ defmodule Cocktailparty.Input do # We restart related processes if needed if changed?(changeset, :config) do - case Repo.update(changeset) do + connection = + if get_change(changeset, :sink) do + changeset + |> validate_full_duplex() + else + changeset + end + + case Repo.update(connection) do {:ok, connection} -> Connection.terminate(connection) ConnectionManager.start_connection(connection) diff --git a/lib/cocktailparty/input/connection_type.ex b/lib/cocktailparty/input/connection_type.ex index 6f05439..723df0e 100644 --- a/lib/cocktailparty/input/connection_type.ex +++ b/lib/cocktailparty/input/connection_type.ex @@ -81,7 +81,7 @@ defmodule Cocktailparty.Input.ConnectionTypes do return whether the connection type support fullduplex connections """ def get_full_duplex(type) do - Map.get(@connection_types, type) + Map.get(@connection_types, type).fullduplex end def validate_config(type, config) do diff --git a/lib/cocktailparty_web/controllers/admin/connection_controller.ex b/lib/cocktailparty_web/controllers/admin/connection_controller.ex index 501d1ec..7476590 100644 --- a/lib/cocktailparty_web/controllers/admin/connection_controller.ex +++ b/lib/cocktailparty_web/controllers/admin/connection_controller.ex @@ -72,13 +72,13 @@ defmodule CocktailpartyWeb.Admin.ConnectionController do Input.change_edit_connection(connection_map) |> Map.put(:data, Input.switch_config_repr!(connection_map)) - connection_types = ConnectionTypes.all() + fullduplex = Input.get_fullduplex!(id) + dbg(fullduplex) render(conn, :edit, connection: connection_map, changeset: changeset, - show_connection_types: false, - connection_types: connection_types + fullduplex: fullduplex ) end @@ -103,7 +103,6 @@ defmodule CocktailpartyWeb.Admin.ConnectionController do new_changeset = Map.put(changeset, :changes, new_changes) connection_types = ConnectionTypes.all() - dbg(new_changeset) render(conn, :edit, connection: connection, diff --git a/lib/cocktailparty_web/controllers/admin/connection_html.ex b/lib/cocktailparty_web/controllers/admin/connection_html.ex index e2a36c0..24c3a09 100644 --- a/lib/cocktailparty_web/controllers/admin/connection_html.ex +++ b/lib/cocktailparty_web/controllers/admin/connection_html.ex @@ -8,7 +8,6 @@ defmodule CocktailpartyWeb.Admin.ConnectionHTML do """ attr :changeset, Ecto.Changeset, required: true attr :connection_types, :list, required: true - attr :show_connection_types, :boolean, default: true attr :action, :string, required: true def connection_form(assigns) diff --git a/lib/cocktailparty_web/controllers/admin/connection_html/connection_form.html.heex b/lib/cocktailparty_web/controllers/admin/connection_html/connection_form.html.heex index 11f73a3..e698533 100644 --- a/lib/cocktailparty_web/controllers/admin/connection_html/connection_form.html.heex +++ b/lib/cocktailparty_web/controllers/admin/connection_html/connection_form.html.heex @@ -4,7 +4,6 @@ <.input field={f[:name]} type="text" label="Name" /> <.input - :if={@show_connection_types} field={f[:type]} type="select" label="Type" @@ -30,35 +29,35 @@ diff --git a/lib/cocktailparty_web/controllers/admin/connection_html/edit.html.heex b/lib/cocktailparty_web/controllers/admin/connection_html/edit.html.heex index 9fd3304..08a3a35 100644 --- a/lib/cocktailparty_web/controllers/admin/connection_html/edit.html.heex +++ b/lib/cocktailparty_web/controllers/admin/connection_html/edit.html.heex @@ -3,11 +3,10 @@ <:subtitle>Use this form to manage connection records in your database. -<.connection_form +<.connection_edit_form changeset={@changeset} action={~p"/admin/connections/#{@connection}"} - connection_types={@connection_types} - show_connection_types={@show_connection_types} + fullduplex={@fullduplex} /> <.back navigate={~p"/admin/connections"}>Back to connections