Skip to content

Commit

Permalink
simplify add_all_items_to_all_list_for_person_id/1 to only accept per…
Browse files Browse the repository at this point in the history
…son_id as argument #356
  • Loading branch information
nelsonic committed Aug 23, 2023
1 parent 2f43ab9 commit e4aebcf
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 68 deletions.
14 changes: 6 additions & 8 deletions lib/app/item.ex
Original file line number Diff line number Diff line change
Expand Up @@ -220,23 +220,21 @@ defmodule App.Item do
"""
#
def items_with_timers(person_id \\ 0) do

all_list = App.List.get_all_list_for_person(person_id)
item_ids = App.ListItems.get_list_items(all_list.id)
sql = """
SELECT i.id, i.text, i.status, i.person_id,
t.start, t.stop, t.id as timer_id,
li.id as li_id, li.position, li.list_id, li.inserted_at
SELECT i.id, i.text, i.status, i.person_id, i.updated_at,
t.start, t.stop, t.id as timer_id
FROM items i
FULL JOIN timers AS t ON t.item_id = i.id
FULL JOIN list_items AS li ON li.item_id = i.id
WHERE i.person_id = $1
WHERE i.id IN ($1)
AND i.status IS NOT NULL
AND i.text IS NOT NULL
AND li.position != 999999.999
ORDER BY timer_id ASC;
"""

values =
Ecto.Adapters.SQL.query!(Repo, sql, [person_id])
Ecto.Adapters.SQL.query!(Repo, sql, [item_ids])
|> map_columns_to_values()

items_tags =
Expand Down
21 changes: 10 additions & 11 deletions lib/app/list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -105,29 +105,28 @@ defmodule App.List do
end

@doc """
get_all_list_for_person()
get_all_list_for_person/1 gets or creates the "all" list for a given `person_id`
"""
def get_all_list_for_person(person_id) do
all_list = get_list_by_name!("all", person_id)
dbg(all_list)

all_list = if all_list == nil do
# doesn't exist, create it:
{:ok, %{model: list}} = create_list(%{name: "all", person_id: person_id, status: 2})
all_list =
if all_list == nil do
# doesn't exist, create it:
{:ok, %{model: list}} =
create_list(%{name: "all", person_id: person_id, status: 2})

# add all items to the newly created list:
# add all items to the newly created list:

# return the list:
list
end
# return the list:
list
end

dbg(all_list)
end


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Below this point is Lists transition code that will be DELETED! #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #


end
94 changes: 78 additions & 16 deletions lib/app/list_items.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,45 +47,107 @@ defmodule App.ListItems do
@doc """
`add_list_item/3` adds an `item` to a `list` for the given `person_id`.
"""
def add_list_item(item, list, person_id) do
# dbg(item)
# dbg(list)
def add_list_item(item_id, list_id, person_id) do
# Get latest list_items.seq for this list.id and person_id combo.
prev_seq = get_list_items(list.id)
prev_seq = get_list_items(list_id)
# Add the `item.id` to the sequence
seq = [item.id | prev_seq] |> Enum.join(",")
seq = [item_id | prev_seq] |> Enum.join(",")

%ListItems{}
|> changeset(%{
list_id: list.id,
list_id: list_id,
person_id: person_id,
seq: seq
})
|> Repo.insert()
end


@doc """
`add_all_items_to_all_list_for_person_id/1` does *exactly* what its' name suggests.
Adds *all* the person's `items` to the `list_items.seq` for the given `list_id`.
Adds *all* the person's `items` to the `list_items.seq`.
"""
def add_all_items_to_all_list_for_person_id(list_id, person_id) do
# IO.inspect("add_all_items_to_all_list_for_person_id(list_id: #{list_id}, person_id: #{person_id})")
def add_all_items_to_all_list_for_person_id(person_id) do
all_list = App.List.get_list_by_name!("all", person_id)
# IO.inspect("add_all_items_to_all_list_for_person_id(person_id: #{person_id})")
all_items = App.Item.all_items_for_person(person_id)
# The previous sequence of items if there is any:
prev_seq = get_list_items(list_id)
prev_seq = get_list_items(all_list.id)
# Add add each `item.id` to the sequence of item ids:
seq = Enum.reduce(all_items, prev_seq, fn i, acc ->
[i.id | acc]
end)
|> Enum.join(",")
seq =
Enum.reduce(all_items, prev_seq, fn i, acc ->
[i.id | acc]
end)
|> Enum.join(",")

%ListItems{}
|> changeset(%{
list_id: list_id,
list_id: all_list.id,
person_id: person_id,
seq: seq
})
|> Repo.insert()
end


# @doc """
# `move_item/3` updates the position of the `item` in a `list`.
# This is used for drag and drop.
# The `item_id` is the `item.id` for the `item` being repositioned e.g: "42"
# and `item_ids_str` is the String of ids currently visible in the interface.
# e.g: "1 2 3 42 71 93". so we can easily determine the precise position of the item_id.
# """
# def move_item(item_id, item_ids_str, list_id \\ 0) do
# IO.inspect(
# "move_item/3 -> item_id: #{item_id} | typeof item_id #{Useful.typeof(item_id)}"
# )

# item = App.Item.get_item!(item_id)
# item_ids = String.split(item_ids_str, " ")

# IO.inspect(
# "item_ids_str: #{item_ids_str} | length(item_ids): #{length(item_ids)} "
# )

# # Get index of item_id in the item_ids list:
# index = Enum.find_index(item_ids, &(&1 == "#{item_id}"))
# IO.inspect("index: #{index}")

# # Get the list by the list_id or text "all":
# list =
# if list_id == 0 do
# # For now we only have the "all" list, but soon we will have "PARA" 😉
# App.List.get_list_by_text!(item.person_id, "all")
# else
# App.List.get_list!(list_id)
# end

# # Derive the New Position
# # Moved somewhere else in the list including the Bottom:
# new_pos =
# if index == 0 do
# # Moved to Top of List:
# # Find the *Next* item_id in the list:
# next_id = Enum.at(item_ids, index + 1)
# # Get the position of the *Next* item:
# next_pos = get_list_item_position(next_id, list.id)

# next_pos
# |> Decimal.from_float()
# |> Decimal.sub("0.000001")
# |> Decimal.round(6)
# |> Decimal.to_float()
# else
# # Find the *Previous* item_id in the list:
# prev_id = Enum.at(item_ids, index - 1)
# prev_pos = get_list_item_position(prev_id, list.id)

# prev_pos
# |> Decimal.from_float()
# |> Decimal.add("0.000001")
# |> Decimal.round(6)
# |> Decimal.to_float()
# end

# add_list_item(item, list, item.person_id, new_pos)
# end
end
26 changes: 6 additions & 20 deletions priv/repo/seeds.exs
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,10 @@ if env == :test || env == :dev do
{:ok, _timer1} =
Timer.start(%{item_id: item0.id, person_id: person_id, start: started})

# List!
# list1_data = %{text: "Shopping", person_id: person_id, status: 2}
# {:ok, %{model: list1, version: _version}} = List.create_list(list1_data)

# list2_data = %{text: "Meals", person_id: person_id, status: 2}
# {:ok, %{model: list2, version: _version}} = List.create_list(list2_data)

# # Add items to lists:
# {:ok, _list_item} = ListItem.add_list_item(item1, list1, person_id, 1.0)
# {:ok, _list_item} = ListItem.add_list_item(item2, list2, person_id, 1.0)
# {:ok, _list_item} = ListItem.add_list_item(item3, list2, person_id, 1.0)
# {:ok, _list_item} = ListItem.add_list_item(item4, list1, person_id, 1.0)

# Create Default Lists
# App.List.create_default_lists(person_id)
# App.ListItem.add_items_to_all_list(person_id)
# Re-order items:

# Remove "Go to Shops" from list1:
# ListItem.remove_list_item(item4, list1, person_id)
# Create "all"" List!
{:ok, %{model: _all_list}} =
App.List.create_list(%{name: "all", person_id: person_id, status: 2})

# Add items to lists:
App.ListItems.add_all_items_to_all_list_for_person_id(person_id)
end
20 changes: 7 additions & 13 deletions test/app/list_items_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ defmodule App.ListItemsTest do

describe "add items to list" do
@person_id 1
@valid_item_attrs %{text: "some text", person_id: @person_id, status: 2}
@valid_list_attrs %{name: "some list", person_id: @person_id, sort: 1, status: 2}
@valid_item_attrs %{text: "do 20 pushups", person_id: @person_id, status: 2}
@valid_list_attrs %{name: "Health", person_id: @person_id, sort: 1, status: 2}



test "get_list_items/1 retrieves the list of items (seq) from list_items for the list_id" do
test "add_list_item/3 adds a list_item & get_list_items/1 retrieves the list of items (seq)" do
# No list No list_items:
assert App.ListItems.get_list_items(0) == []

Expand All @@ -19,19 +19,13 @@ defmodule App.ListItemsTest do
assert {:ok, %{model: list}} = List.create_list(@valid_list_attrs)

# add the item to the lists_items:
ListItems.add_list_item(item, list, @person_id)
ListItems.add_list_item(item.id, list.id, @person_id)

# Confirm the item.id is in the list_items.seq:
assert Enum.member?(ListItems.get_list_items(list.id), "#{item.id}")


seq = ListItems.get_list_items(list.id)
assert Enum.member?(seq, "#{item.id}")
end

# test "add_list_item/3 adds an item to the list for the person_id" do

# end


test "add_all_items_to_all_list_for_person_id/2 adds all items to all list for person_id" do
person_id = 42
# create an item for the person but do NOT add it to any list:
Expand All @@ -45,7 +39,7 @@ defmodule App.ListItemsTest do
assert {:ok, %{model: all_list}} =
List.create_list(%{name: "all", person_id: person_id, status: 2})
# Invoke the biz logic:
App.ListItems.add_all_items_to_all_list_for_person_id(all_list.id, person_id)
App.ListItems.add_all_items_to_all_list_for_person_id(person_id)

# Confirm that the item.id is in the squence of item ids for the "all" list:
all_items_seq = ListItems.get_list_items(all_list.id)
Expand Down

0 comments on commit e4aebcf

Please sign in to comment.