From e4aebcf1b085a5867c06a123de02136dbb482fe8 Mon Sep 17 00:00:00 2001 From: nelsonic Date: Tue, 22 Aug 2023 09:25:05 +0100 Subject: [PATCH] simplify add_all_items_to_all_list_for_person_id/1 to only accept person_id as argument #356 --- lib/app/item.ex | 14 +++--- lib/app/list.ex | 21 ++++---- lib/app/list_items.ex | 94 ++++++++++++++++++++++++++++++------ priv/repo/seeds.exs | 26 +++------- test/app/list_items_test.exs | 20 +++----- 5 files changed, 107 insertions(+), 68 deletions(-) diff --git a/lib/app/item.ex b/lib/app/item.ex index 4cc830dc..a06ac165 100644 --- a/lib/app/item.ex +++ b/lib/app/item.ex @@ -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 = diff --git a/lib/app/list.ex b/lib/app/list.ex index 81389c3b..a0061cc2 100644 --- a/lib/app/list.ex +++ b/lib/app/list.ex @@ -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 diff --git a/lib/app/list_items.ex b/lib/app/list_items.ex index b6e503fa..c1b1bf11 100644 --- a/lib/app/list_items.ex +++ b/lib/app/list_items.ex @@ -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 diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs index 2af2f683..41d9a7f2 100644 --- a/priv/repo/seeds.exs +++ b/priv/repo/seeds.exs @@ -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 diff --git a/test/app/list_items_test.exs b/test/app/list_items_test.exs index 294aef34..edd6e1aa 100644 --- a/test/app/list_items_test.exs +++ b/test/app/list_items_test.exs @@ -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) == [] @@ -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: @@ -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)