Skip to content

Commit

Permalink
tests passing again. reorg with cids works a charm. #145 #356 #410
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Aug 30, 2023
1 parent e54e3d1 commit c8c9c5c
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 53 deletions.
50 changes: 31 additions & 19 deletions lib/app/item.ex
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,10 @@ defmodule App.Item do
def items_with_timers(person_id \\ 0) do
all_list = App.List.get_all_list_for_person(person_id)
# dbg(all_list)

# |> Enum.join(",")
seq = App.ListItems.get_list_items(all_list.cid)
# dbg(seq)

sql = """
SELECT i.id, i.cid, i.text, i.status, i.person_id, i.updated_at,
Expand All @@ -247,9 +249,13 @@ defmodule App.Item do
list_person_items(person_id)
|> Enum.reduce(%{}, fn i, acc -> Map.put(acc, i.id, i) end)

# dbg(items_tags)

accumulate_item_timers(values, seq)
|> Enum.map(fn t ->
Map.put(t, :tags, items_tags[t.id].tags)
if t != nil do
Map.put(t, :tags, items_tags[t.id].tags)
end
end)
end

Expand Down Expand Up @@ -388,16 +394,17 @@ defmodule App.Item do
end)

# creates a nested map: %{ item.cid: %{id: 1, text: "my item", etc.}}
cid_item_map = Map.new(items_with_timers, fn item ->
time_elapsed = Map.get(item_id_timer_diff_map, item.id)
cid_item_map =
Map.new(items_with_timers, fn item ->
time_elapsed = Map.get(item_id_timer_diff_map, item.id)

start =
if is_nil(item.start),
do: nil,
else: NaiveDateTime.add(item.start, -time_elapsed)
start =
if is_nil(item.start),
do: nil,
else: NaiveDateTime.add(item.start, -time_elapsed)

{item.cid, %{item | start: start}}
end)
{item.cid, %{item | start: start}}
end)

# return the list of items in the order of seq
Enum.map(seq, fn cid -> cid_item_map[cid] end)
Expand All @@ -416,16 +423,21 @@ defmodule App.Item do
items = list_items()

Enum.each(items, fn i ->
item = %{
person_id: i.person_id,
status: i.status,
text: i.text,
id: i.id
}

i
|> changeset(Map.put(item, :cid, Cid.cid(item)))
|> Repo.update()
# coveralls-ignore-start
unless Map.has_key?(i, :cid) do
item = %{
person_id: i.person_id,
status: i.status,
text: i.text,
id: i.id
}

i
|> changeset(Map.put(item, :cid, Cid.cid(item)))
|> Repo.update()
end

# coveralls-ignore-stop
end)
end
end
6 changes: 4 additions & 2 deletions lib/app/list_items.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ defmodule App.ListItems do
seq: seq
})
|> Repo.insert()

# |> dbg()
end

def list_items_seq_sql do
Expand All @@ -53,13 +55,13 @@ defmodule App.ListItems do
`get_list_items/2` retrieves the *latest* `list_items` record for a given `list_cid`.
"""
def get_list_items(list_cid) do
# dbg(list_cid)
# dbg(list_items_seq_sql())
# IO.puts(" = = = = = = = = = = = = = = = > get_list_items(#{list_cid})")
result = Ecto.Adapters.SQL.query!(Repo, list_items_seq_sql(), [list_cid])
# dbg(result.rows)
if is_nil(result.rows) or result.rows == [] do
[]
else
# dbg(result.rows)
result.rows |> List.first() |> List.first() |> String.split(",")
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/app_web/controllers/init_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ defmodule AppWeb.InitController do
@env_required ~w/AUTH_API_KEY ENCRYPTION_KEYS SECRET_KEY_BASE DATABASE_URL/

def index(conn, _params) do
Logger.info("attempeting to update cid of all items ...")
App.Item.update_all_items_cid()

Logger.info("init attempting to check environment variables ... ")

conn
Expand Down
15 changes: 8 additions & 7 deletions lib/app_web/live/app_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ defmodule AppWeb.AppLive do
person_id = get_person_id(socket.assigns)
# Create the "all" list for the person_id
all_list = App.List.get_all_list_for_person(person_id)
# dbg(all_list)
# Temporary function to add All *existing* items to the "All" list:
App.ListItems.add_all_items_to_all_list_for_person_id(person_id)
# App.ListItems.add_all_items_to_all_list_for_person_id(person_id)

items = Item.items_with_timers(person_id)
tags = Tag.list_person_tags(person_id)
Expand Down Expand Up @@ -305,9 +306,9 @@ defmodule AppWeb.AppLive do
list_cid = get_list_cid(socket.assigns)
person_id = get_person_id(socket.assigns)

IO.puts(
"updateIndexes -> seq: #{seq} | list_id: #{list_cid} | person_id: #{person_id}"
)
# IO.puts(
# "updateIndexes -> seq: #{seq} | list_cid: #{list_cid} | person_id: #{person_id}"
# )

App.ListItems.create_list_items_seq(list_cid, person_id, seq)
{:noreply, socket}
Expand All @@ -321,9 +322,9 @@ defmodule AppWeb.AppLive do
},
socket
) do
IO.puts(
"cur_item_id: #{current_item_id}, selected_item_id: #{selected_item_id}"
)
# IO.puts(
# "cur_item_id: #{current_item_id}, selected_item_id: #{selected_item_id}"
# )

{:noreply,
push_event(socket, "dragover-item", %{
Expand Down
4 changes: 1 addition & 3 deletions priv/repo/migrations/20230824153220_add_cid.exs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
defmodule App.Repo.Migrations.AddCid do
use Ecto.Migration



def up do
alter table(:items) do
add(:cid, :string)
end
# https://stackoverflow.com/questions/36723407/how-to-run-updating-in-migration-for-ecto
flush()

App.Item.update_all_items_cid()
end
end
54 changes: 32 additions & 22 deletions test/app_web/live/app_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -685,31 +685,37 @@ defmodule AppWeb.AppLiveTest do
end

test "filter items by tag name", %{conn: conn} do
person_id = 0
{:ok, tag1} =
Tag.create_tag(%{person_id: 0, text: "tag1", color: "#FCA5A5"})
Tag.create_tag(%{person_id: person_id, text: "tag1", color: "#FCA5A5"})

{:ok, tag2} =
Tag.create_tag(%{person_id: 0, text: "tag2", color: "#FCA5A5"})
Tag.create_tag(%{person_id: person_id, text: "tag2", color: "#FCA5A5"})

{:ok, tag3} =
Tag.create_tag(%{person_id: 0, text: "tag3", color: "#FCA5A5"})
Tag.create_tag(%{person_id: person_id, text: "tag3", color: "#FCA5A5"})

{:ok, %{model: _item}} =
{:ok, %{model: item1}} =
Item.create_item_with_tags(%{
text: "Item1 to do",
person_id: 0,
status: 2,
tags: [tag1, tag2]
})

{:ok, %{model: _item}} =
{:ok, %{model: item2}} =
Item.create_item_with_tags(%{
text: "Item2 to do",
person_id: 0,
status: 2,
tags: [tag1, tag3]
})

# The items need to be in the latest seq to appear on the page:
list = App.List.get_all_list_for_person(person_id)
App.ListItems.create_list_items_seq(list.cid, person_id, "#{item1.cid},#{item2.cid}")


{:ok, view, _html} = live(conn, "/?filter_by=all")
assert render(view) =~ "Item1 to do"
assert render(view) =~ "Item2 to do"
Expand Down Expand Up @@ -815,19 +821,20 @@ defmodule AppWeb.AppLiveTest do
end

test "Drag and Drop item", %{conn: conn} do
person_id = 42
person_id = 0
# Creating Three items
{:ok, %{model: item}} =
Item.create_item(%{text: "Learn Elixir", person_id: person_id})
Item.create_item(%{text: "Learn Elixir", person_id: person_id, status: 2})

{:ok, %{model: item2}} =
Item.create_item(%{ text: "Build Awesome App", person_id: person_id})
Item.create_item(%{ text: "Build Awesome App", person_id: person_id, status: 2})

{:ok, %{model: item3}} =
Item.create_item(%{ text: "Profit", person_id: person_id})
Item.create_item(%{ text: "Profit", person_id: person_id, status: 2})

# Create "all" list for this person_id:
list = App.List.get_all_list_for_person(person_id)

# Add all items to "all" list:
App.ListItems.add_all_items_to_all_list_for_person_id(person_id)

Expand All @@ -848,24 +855,27 @@ defmodule AppWeb.AppLiveTest do

assert render_hook(view, "removeHighlight", %{"id" => item.id})

# Switch items (update indexes)
# reorder items:
render_hook(view, "updateIndexes", %{
"sec" => "#{item.id},#{item2.id},#{item3.id}"
"seq" => "#{item.cid},#{item2.cid},#{item3.cid}"
})

sec = App.ListItems.get_list_items(list.id)
dbg(sec)
seq = App.ListItems.get_list_items(list.cid)
pos1 = Enum.find_index(seq, fn x -> x == "#{item.cid}" end)
pos2 = Enum.find_index(seq, fn x -> x == "#{item2.cid}" end)
# IO.puts("#{pos1}: #{item.cid}")
# IO.puts("#{pos2}: #{item2.cid}")

{pos1, _} = :binary.match sec, "#{item.id}"
{pos2, _} = :binary.match sec, "#{item2.id}"
assert pos1 < pos2

# Update list_item.seq:
App.ListItems.create_list_items_seq(list.id, person_id, "#{item.id},#{item3.id},#{item2.id}")
new_sec = App.ListItems.get_list_items(list.id)
dbg(new_sec)
{pos2, _} = :binary.match new_sec, "#{item2.id}"
{pos3, _} = :binary.match new_sec, "#{item3.id}"
App.ListItems.create_list_items_seq(list.cid, person_id, "#{item.cid},#{item3.cid},#{item2.cid}")
new_seq = App.ListItems.get_list_items(list.cid)
# dbg(new_seq)
pos2 = Enum.find_index(new_seq, fn x -> x == "#{item2.cid}" end)
pos3 = Enum.find_index(new_seq, fn x -> x == "#{item3.cid}" end)
# IO.puts("#{pos2}: #{item2.cid}")
# IO.puts("#{pos3}: #{item3.cid}")
assert pos3 < pos2
end

Expand All @@ -883,8 +893,8 @@ defmodule AppWeb.AppLiveTest do
color: "#FCA5A5"
})

assert render_submit(view, :create, %{text: "tag enter pressed"})
assert render(view) =~ "tag enter pressed"
assert render_submit(view, :create, %{text: "baking"})
assert render(view) =~ "baking"
end

test "don't select tag if other keydown is pressed", %{conn: conn} do
Expand Down

0 comments on commit c8c9c5c

Please sign in to comment.