diff --git a/lib/app/item.ex b/lib/app/item.ex index d8c97489..9280b4b4 100644 --- a/lib/app/item.ex +++ b/lib/app/item.ex @@ -207,21 +207,27 @@ defmodule App.Item do # 👩‍💻 Feedback/Pairing/Refactoring Welcome! 🙏 @doc """ - `items_with_timers/1` Returns a List of items with the latest associated timers. - This list is ordered with the position that is detailed inside the Items schema. + `items_with_timers/2` Returns a List of items with the latest associated timers. + The result set is ordered by the `list.seq`. + Accepts an optional second parameter `list_cid` which is the unique ID of the `list` + to retrieve `items` for. ## Examples iex> items_with_timers() [ - %{text: "hello", person_id: 1, status: 2, start: 2022-07-14 09:35:18}, - %{text: "world", person_id: 2, status: 7, start: 2022-07-15 04:20:42} + %{text: "hello", person_id: 0, status: 2, start: 2022-07-14 09:35:18}, + %{text: "world", person_id: 0, status: 7, start: 2022-07-15 04:20:42} ] """ - # - def items_with_timers(person_id \\ 0) do - all_list = App.List.get_all_list_for_person(person_id) - seq = App.List.get_list_seq(all_list) + def items_with_timers(person_id \\ 0, list_cid \\ nil) do + seq = if is_nil(list_cid) do + App.List.get_all_list_for_person(person_id) + |> App.List.get_list_seq() + else + App.List.get_list_by_cid!(list_cid) + |> App.List.get_list_seq() + end sql = """ SELECT i.id, i.cid, i.text, i.status, i.person_id, i.updated_at, diff --git a/lib/app_web/live/app_live.ex b/lib/app_web/live/app_live.ex index 593f146c..834d2ea1 100644 --- a/lib/app_web/live/app_live.ex +++ b/lib/app_web/live/app_live.ex @@ -16,6 +16,7 @@ defmodule AppWeb.AppLive do @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) diff --git a/test/app/item_test.exs b/test/app/item_test.exs index c75fa910..ef4aeecb 100644 --- a/test/app/item_test.exs +++ b/test/app/item_test.exs @@ -4,6 +4,7 @@ defmodule App.ItemTest do describe "items" do @valid_attrs %{text: "Buy Bananas", person_id: 1, status: 2} + @valid_attrs2 %{text: "Make Muffins", person_id: 1, status: 2} @update_attrs %{text: "some updated text", person_id: 1} @invalid_attrs %{text: nil} @@ -54,16 +55,6 @@ defmodule App.ItemTest do assert {:error, %Ecto.Changeset{}} = Item.create_item(@invalid_attrs) end - # test "list_items/0 returns a list of items stored in the DB" do - # {:ok, %{model: _item1}} = - # Item.create_item(@valid_attrs) - - # {:ok, %{model: _item2}} = - # Item.create_item(@valid_attrs) - - # assert Enum.count(Item.list_items()) == 2 - # end - test "update_item/2 with valid data updates the item" do {:ok, %{model: item}} = Item.create_item(@valid_attrs) @@ -201,5 +192,26 @@ defmodule App.ItemTest do item_timers = Item.items_with_timers(1) assert length(item_timers) > 0 end + + test "Item.items_with_timers/1 returns a list filtered by list_cid" do + person_id = 468 + {:ok, %{model: item1}} = Item.create_item(%{@valid_attrs | person_id: person_id}) + {:ok, %{model: item2}} = Item.create_item(%{@valid_attrs2 | person_id: person_id}) + + # Create a New List: + list_attrs = %{name: "Todo List", person_id: person_id, status: 2} + assert {:ok, %{model: list}} = App.List.create_list(list_attrs) + assert list.name == list_attrs.name + + # Add these items to the new list + App.List.update_list_seq(list.cid, person_id, "#{item1.cid},#{item2.cid}") + + # Confirm that both items are on the list: + items = Item.items_with_timers(person_id, list.cid) + first = List.first(items) + last = List.last(items) + assert first.cid == item1.cid + assert last.cid == item2.cid + end end end