From 29edbb63674d4ab05befafee4ad4c3321794c287 Mon Sep 17 00:00:00 2001 From: panoramix360 Date: Tue, 5 Sep 2023 20:51:15 -0300 Subject: [PATCH 1/2] feat: creates the person module and refactors the code to use the get_person_id method --- lib/app/person.ex | 6 ++++++ lib/app_web/controllers/tag_controller.ex | 6 +++--- lib/app_web/live/app_live.ex | 22 ++++++++++------------ lib/app_web/live/stats_live.ex | 6 ++---- 4 files changed, 21 insertions(+), 19 deletions(-) create mode 100644 lib/app/person.ex diff --git a/lib/app/person.ex b/lib/app/person.ex new file mode 100644 index 00000000..b0cd5e92 --- /dev/null +++ b/lib/app/person.ex @@ -0,0 +1,6 @@ +defmodule App.Person do + @doc """ + Gets the Person Id from the assigns on connection or socket. + """ + def get_person_id(assigns), do: assigns[:person][:id] || 0 +end diff --git a/lib/app_web/controllers/tag_controller.ex b/lib/app_web/controllers/tag_controller.ex index 965b5361..c8b525e8 100644 --- a/lib/app_web/controllers/tag_controller.ex +++ b/lib/app_web/controllers/tag_controller.ex @@ -1,6 +1,6 @@ defmodule AppWeb.TagController do use AppWeb, :controller - alias App.Tag + alias App.{Person, Tag} plug :permission_tag when action in [:edit, :update, :delete] def index(conn, _params) do @@ -16,7 +16,7 @@ defmodule AppWeb.TagController do end def create(conn, %{"tag" => tag_params}) do - person_id = conn.assigns[:person][:id] || 0 + person_id = Person.get_person_id(conn.assigns) tag_params = Map.put(tag_params, "person_id", person_id) case Tag.create_tag(tag_params) do @@ -63,7 +63,7 @@ defmodule AppWeb.TagController do defp permission_tag(conn, _opts) do tag = Tag.get_tag!(conn.params["id"]) - person_id = conn.assigns[:person][:id] || 0 + person_id = Person.get_person_id(conn.assigns) if tag.person_id == person_id do conn diff --git a/lib/app_web/live/app_live.ex b/lib/app_web/live/app_live.ex index d233b324..7be788fa 100644 --- a/lib/app_web/live/app_live.ex +++ b/lib/app_web/live/app_live.ex @@ -2,7 +2,7 @@ defmodule AppWeb.AppLive do require Logger use AppWeb, :live_view use Timex - alias App.{Item, Tag, Timer} + alias App.{Item, Person, Tag, Timer} # run authentication on mount on_mount(AppWeb.AuthController) alias Phoenix.Socket.Broadcast @@ -10,15 +10,13 @@ defmodule AppWeb.AppLive do @topic "live" @stats_topic "stats" - defp get_person_id(assigns), do: assigns[:person][:id] || 0 - @impl true def mount(_params, _session, socket) do # subscribe to the channel if connected?(socket), do: AppWeb.Endpoint.subscribe(@topic) AppWeb.Endpoint.subscribe(@stats_topic) - person_id = get_person_id(socket.assigns) + person_id = Person.get_person_id(socket.assigns) items = Item.items_with_timers(person_id) tags = Tag.list_person_tags(person_id) selected_tags = [] @@ -43,7 +41,7 @@ defmodule AppWeb.AppLive do @impl true def handle_event("validate", %{"text" => text}, socket) do - person_id = get_person_id(socket.assigns) + person_id = Person.get_person_id(socket.assigns) draft = Item.get_draft_item(person_id) Item.update_draft(draft, %{text: text}) # only save draft if person id != 0 (ie not guest) @@ -52,7 +50,7 @@ defmodule AppWeb.AppLive do @impl true def handle_event("create", %{"text" => text}, socket) do - person_id = get_person_id(socket.assigns) + person_id = Person.get_person_id(socket.assigns) Item.create_item_with_tags(%{ text: text, @@ -77,7 +75,7 @@ defmodule AppWeb.AppLive do @impl true def handle_event("toggle", data, socket) do - person_id = get_person_id(socket.assigns) + person_id = Person.get_person_id(socket.assigns) # Toggle the status of the item between 3 (:active) and 4 (:done) status = if Map.has_key?(data, "value"), do: 4, else: 3 @@ -93,7 +91,7 @@ defmodule AppWeb.AppLive do @impl true def handle_event("toggle_tag", value, socket) do - person_id = get_person_id(socket.assigns) + person_id = Person.get_person_id(socket.assigns) selected_tags = socket.assigns.selected_tags tag = Tag.get_tag!(value["tag_id"]) tags = Tag.list_person_tags(person_id) @@ -105,7 +103,7 @@ defmodule AppWeb.AppLive do @impl true def handle_event("filter-tags", %{"key" => _key, "value" => value}, socket) do - person_id = get_person_id(socket.assigns) + person_id = Person.get_person_id(socket.assigns) tags = Tag.list_person_tags(person_id) @@ -154,7 +152,7 @@ defmodule AppWeb.AppLive do @impl true def handle_event("start", data, socket) do item = Item.get_item!(Map.get(data, "id")) - person_id = get_person_id(socket.assigns) + person_id = Person.get_person_id(socket.assigns) {:ok, _timer} = Timer.start(%{ @@ -199,7 +197,7 @@ defmodule AppWeb.AppLive do %{"id" => item_id, "text" => text}, socket ) do - person_id = get_person_id(socket.assigns) + person_id = Person.get_person_id(socket.assigns) current_item = Item.get_item!(item_id) Item.update_item(current_item, %{ @@ -252,7 +250,7 @@ defmodule AppWeb.AppLive do @impl true def handle_info(%Broadcast{event: "update", payload: payload}, socket) do - person_id = get_person_id(socket.assigns) + person_id = Person.get_person_id(socket.assigns) items = Item.items_with_timers(person_id) isEditingItem = socket.assigns.editing diff --git a/lib/app_web/live/stats_live.ex b/lib/app_web/live/stats_live.ex index 0003c4d8..ef365323 100644 --- a/lib/app_web/live/stats_live.ex +++ b/lib/app_web/live/stats_live.ex @@ -1,7 +1,7 @@ defmodule AppWeb.StatsLive do require Logger use AppWeb, :live_view - alias App.{Stats, DateTimeHelper} + alias App.{Stats, DateTimeHelper, Person} alias Phoenix.Socket.Broadcast # run authentication on mount @@ -9,14 +9,12 @@ defmodule AppWeb.StatsLive do @stats_topic "stats" - defp get_person_id(assigns), do: assigns[:person][:id] || 0 - @impl true def mount(_params, _session, socket) do # subscribe to the channel if connected?(socket), do: AppWeb.Endpoint.subscribe(@stats_topic) - person_id = get_person_id(socket.assigns) + person_id = Person.get_person_id(socket.assigns) metrics = Stats.person_with_item_and_timer_count() {:ok, From 72e866061ef468bdbba7696c29aa6d63bdd6cbf2 Mon Sep 17 00:00:00 2001 From: panoramix360 Date: Tue, 5 Sep 2023 20:56:36 -0300 Subject: [PATCH 2/2] test: add tests to person --- test/app/person_test.exs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 test/app/person_test.exs diff --git a/test/app/person_test.exs b/test/app/person_test.exs new file mode 100644 index 00000000..107d392d --- /dev/null +++ b/test/app/person_test.exs @@ -0,0 +1,29 @@ +defmodule App.PersonTest do + use ExUnit.Case + alias App.Person + + @doc """ + Test for `get_person_id/1` + """ + describe "get_person_id/1" do + test "returns id when person is present in assigns" do + assigns = %{person: %{id: 1}} + assert Person.get_person_id(assigns) == 1 + end + + test "returns 0 when person is not present in assigns" do + assigns = %{} + assert Person.get_person_id(assigns) == 0 + end + + test "returns 0 when id is not present in person" do + assigns = %{person: %{}} + assert Person.get_person_id(assigns) == 0 + end + + test "returns 0 when id is nil in person" do + assigns = %{person: %{id: nil}} + assert Person.get_person_id(assigns) == 0 + end + end +end