Skip to content

Commit

Permalink
#423 update items_with_timers/1 to accept list_cid as second param to…
Browse files Browse the repository at this point in the history
… allow displaying lists
  • Loading branch information
nelsonic committed Sep 16, 2023
1 parent d70ea74 commit 81c7227
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
22 changes: 14 additions & 8 deletions lib/app/item.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions lib/app_web/live/app_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 22 additions & 10 deletions test/app/item_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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

0 comments on commit 81c7227

Please sign in to comment.