diff --git a/justfile b/justfile new file mode 100644 index 0000000..da94f0d --- /dev/null +++ b/justfile @@ -0,0 +1,3 @@ +dev: + #!/bin/env bash + docker compose -f .dev/docker-compose.yml up --build diff --git a/lib/back/automatons/automaton.ex b/lib/back/automatons/automaton.ex index f685866..53b4205 100644 --- a/lib/back/automatons/automaton.ex +++ b/lib/back/automatons/automaton.ex @@ -19,7 +19,7 @@ defmodule Back.Automatons.Automaton do @doc false def changeset(automaton, attrs) do automaton - |> cast(attrs, [:contents, :name, :description, :assets_link]) + |> cast(attrs, [:contents, :name, :description, :assets_link, :posted_by]) |> validate_required([:contents, :name, :description]) end end diff --git a/lib/back/plugins/manager.ex b/lib/back/plugins/manager.ex index 63600d9..6864a29 100644 --- a/lib/back/plugins/manager.ex +++ b/lib/back/plugins/manager.ex @@ -6,7 +6,10 @@ defmodule Back.Plugins.Manager do import Ecto.Query, warn: false alias Back.Repo + alias Back.Automatons.Automaton alias Back.Plugins.Manager.PluginManager + alias Back.Visuals.Visual + alias Back.Users.User @doc """ Returns the list of plugin_manager. @@ -101,4 +104,57 @@ defmodule Back.Plugins.Manager do def change_plugin_manager(%PluginManager{} = plugin_manager, attrs \\ %{}) do PluginManager.changeset(plugin_manager, attrs) end + + def get_visuals_by_automaton_id!(id) do + query = + from pm in PluginManager, + join: v in Visual, + on: v.id == pm.visual, + left_join: u in User, + on: v.posted_by == u.user_id, + where: pm.automaton == ^id, + select: %{ + id: v.id, + name: v.name, + description: v.description, + assets_link: v.assets_link, + posted_by: %{ + user_id: u.user_id, + username: u.username, + email: u.email, + phone: u.phone, + created_at: u.created_at, + user_role: u.user_role + } + } + + Repo.all(query) + end + + def get_automaton_by_visuals_id!(id) do + query = + from pm in PluginManager, + join: a in Automaton, + on: a.automaton_id == pm.automaton, + left_join: u in User, + on: a.posted_by == u.user_id, + where: pm.visual == ^id, + select: %{ + id: a.automaton_id, + name: a.name, + description: a.description, + contents: a.contents, + assets_link: a.assets_link, + posted_by: %{ + user_id: u.user_id, + username: u.username, + email: u.email, + phone: u.phone, + created_at: u.created_at, + user_role: u.user_role + } + } + + Repo.all(query) + end end diff --git a/lib/back/plugins/manager/plugin_manager.ex b/lib/back/plugins/manager/plugin_manager.ex index f38f186..c3d19a7 100644 --- a/lib/back/plugins/manager/plugin_manager.ex +++ b/lib/back/plugins/manager/plugin_manager.ex @@ -14,7 +14,7 @@ defmodule Back.Plugins.Manager.PluginManager do @doc false def changeset(plugin_manager, attrs) do plugin_manager - |> cast(attrs, []) - |> validate_required([]) + |> cast(attrs, [:automaton, :visual]) + |> validate_required([:automaton, :visual]) end end diff --git a/lib/back/visuals/visual.ex b/lib/back/visuals/visual.ex index baff92c..38aea51 100644 --- a/lib/back/visuals/visual.ex +++ b/lib/back/visuals/visual.ex @@ -15,7 +15,7 @@ defmodule Back.Visuals.Visual do @doc false def changeset(visual, attrs) do visual - |> cast(attrs, [:name, :description, :assets_link]) + |> cast(attrs, [:name, :description, :assets_link, :posted_by]) |> validate_required([:name, :description, :assets_link]) end end diff --git a/lib/back_web/controllers/plugin_manager_controller.ex b/lib/back_web/controllers/plugin_manager_controller.ex index 0129e15..cd4c3a0 100644 --- a/lib/back_web/controllers/plugin_manager_controller.ex +++ b/lib/back_web/controllers/plugin_manager_controller.ex @@ -6,10 +6,10 @@ defmodule BackWeb.PluginManagerController do action_fallback BackWeb.FallbackController - def index(conn, _params) do - plugin_manager = Manager.list_plugin_manager() - render(conn, :index, plugin_manager: plugin_manager) - end + # def index(conn, _params) do + # plugin_manager = Manager.list_plugin_manager() + # render(conn, :index, plugin_manager: plugin_manager) + # end def create(conn, %{"plugin_manager" => plugin_manager_params}) do with {:ok, %PluginManager{} = plugin_manager} <- @@ -21,11 +21,6 @@ defmodule BackWeb.PluginManagerController do end end - def show(conn, %{"id" => id}) do - plugin_manager = Manager.get_plugin_manager!(id) - render(conn, :show, plugin_manager: plugin_manager) - end - def update(conn, %{"id" => id, "plugin_manager" => plugin_manager_params}) do plugin_manager = Manager.get_plugin_manager!(id) @@ -42,4 +37,22 @@ defmodule BackWeb.PluginManagerController do send_resp(conn, :no_content, "") end end + + @doc """ + Get a visual id and return all automaton associated with it + the user that created each automaton + """ + def show_automatons(conn, %{"id" => id}) do + data = Manager.get_automaton_by_visuals_id!(id) + + render(conn, :index_automaton, plugin_manager: data) + end + + @doc """ + Get an automaton id and return all visuals associated with it + the user that created each visual + """ + def show_visuals(conn, %{"id" => id}) do + data = Manager.get_visuals_by_automaton_id!(id) + + render(conn, :index_visual, plugin_manager: data) + end end diff --git a/lib/back_web/controllers/plugin_manager_json.ex b/lib/back_web/controllers/plugin_manager_json.ex index 341fcc4..abe44d0 100644 --- a/lib/back_web/controllers/plugin_manager_json.ex +++ b/lib/back_web/controllers/plugin_manager_json.ex @@ -1,23 +1,65 @@ defmodule BackWeb.PluginManagerJSON do alias Back.Plugins.Manager.PluginManager + # misc + def show(%{plugin_manager: plugin_manager}) do + %{data: data(plugin_manager)} + end + + defp data(%PluginManager{} = data) do + %{ + id: data.id, + automaton: data.automaton, + visual: data.visual + } + end + @doc """ Renders a list of plugin_manager. """ - def index(%{plugin_manager: plugin_manager}) do - %{data: for(plugin_manager <- plugin_manager, do: data(plugin_manager))} + def index_visual(%{plugin_manager: plugin_manager}) do + %{data: for(plugin_manager <- plugin_manager, do: data_visual(plugin_manager))} end @doc """ Renders a single plugin_manager. """ - def show(%{plugin_manager: plugin_manager}) do - %{data: data(plugin_manager)} + def show_visual(%{plugin_manager: plugin_manager}) do + %{data: data_visual(plugin_manager)} + end + + defp data_visual(%{} = data) do + %{ + id: data.id, + name: data.name, + description: data.description, + assets_link: data.assets_link, + posted_by: data.posted_by + } + end + + @doc """ + Renders a list of plugin_manager. + """ + def index_automaton(%{plugin_manager: plugin_manager}) do + %{data: for(plugin_manager <- plugin_manager, do: data_automaton(plugin_manager))} + end + + @doc """ + Renders a single plugin_manager. + """ + def show_automaton(%{plugin_manager: plugin_manager}) do + %{data: data_automaton(plugin_manager)} end - defp data(%PluginManager{} = plugin_manager) do + defp data_automaton(%{} = data) do %{ - id: plugin_manager.id + id: data.id, + name: data.name, + description: data.description, + contents: data.contents, + assets_link: data.assets_link, + posted_by: data.posted_by } end end diff --git a/lib/back_web/router.ex b/lib/back_web/router.ex index ca53e80..12f6dd7 100644 --- a/lib/back_web/router.ex +++ b/lib/back_web/router.ex @@ -29,6 +29,8 @@ defmodule BackWeb.Router do get "/automaton/images/:id", AutomatonController, :show_img get "/automaton/recent/:nb", AutomatonController, :get_recents get "/automaton_comment/automaton/:id", AutomatonCommentsController, :get_by_automaton + get "/plugin_manager/automaton/:id", PluginManagerController, :show_visuals + get "/plugin_manager/visual/:id", PluginManagerController, :show_automatons # user get "/user/pictures", UserController, :index_pic @@ -45,8 +47,8 @@ defmodule BackWeb.Router do resources "/post", PostController, except: [:new, :edit] resources "/automaton", AutomatonController, except: [:new, :edit, :index] - resources "/visuals", VisualController, except: [:new, :edit, :index] - resources "/plugin_manager", PluginManagerController, except: [:new, :edit, :index] + resources "/visuals", VisualController, except: [:new, :edit] + resources "/plugin_manager", PluginManagerController, except: [:new, :edit, :index, :show] post "/automaton/image", AutomatonController, :create_image resources "/comment", CommentController, except: [:new, :edit] resources "/blocked", BlockedController, except: [:new, :edit]