Skip to content

Commit

Permalink
remove list_items schema while maintaining all functionality #413
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Sep 5, 2023
1 parent a386f98 commit 39a758c
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 334 deletions.
6 changes: 3 additions & 3 deletions lib/app/item.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ defmodule App.Item do
%Item{}
|> changeset(attrs)
|> PaperTrail.insert(originator: %{id: Map.get(attrs, :person_id, 0)})
|> App.ListItems.add_papertrail_item_to_all_list()
|> App.List.add_papertrail_item_to_all_list()
end

@doc """
Expand All @@ -75,7 +75,7 @@ defmodule App.Item do
%Item{}
|> changeset_with_tags(attrs)
|> PaperTrail.insert(originator: %{id: Map.get(attrs, :person_id, 0)})
|> App.ListItems.add_papertrail_item_to_all_list()
|> App.List.add_papertrail_item_to_all_list()
end

@doc """
Expand Down Expand Up @@ -227,7 +227,7 @@ 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)
seq = App.ListItems.get_list_items(all_list.cid)
seq = App.List.get_list_seq(all_list)
# dbg(seq)

sql = """
Expand Down
70 changes: 69 additions & 1 deletion lib/app/list.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule App.List do
require Logger
use Ecto.Schema
import Ecto.{Changeset, Query}
alias App.{Repo}
Expand All @@ -9,6 +10,7 @@ defmodule App.List do
field :cid, :string
field :name, :string
field :person_id, :integer
field :seq, :string
field :sort, :integer
field :status, :integer

Expand All @@ -18,7 +20,7 @@ defmodule App.List do
@doc false
def changeset(list, attrs) do
list
|> cast(attrs, [:name, :person_id, :sort, :status])
|> cast(attrs, [:name, :person_id, :seq, :sort, :status])
|> validate_required([:name, :person_id])
|> App.Cid.put_cid()
end
Expand Down Expand Up @@ -144,7 +146,73 @@ defmodule App.List do
end
end

def add_item_to_list(item_cid, list_cid, person_id) do
list = get_list_by_cid!(list_cid)
# dbg(list)
prev_seq = get_list_seq(list)
seq = [item_cid | prev_seq] |> Enum.join(",")
# dbg(seq)
update_list(list, %{seq: seq, person_id: person_id})
end

def update_list_seq(list_cid, person_id, seq) do
list = get_list_by_cid!(list_cid)
update_list(list, %{seq: seq, person_id: person_id})
end

# feel free to refactor this to use pattern matching:
def add_papertrail_item_to_all_list(tuple) do
# extract the item from the tuple:
try do
{:ok, %{model: item}} = tuple
all_list = App.List.get_all_list_for_person(item.person_id)
add_item_to_list(item.cid, all_list.cid, item.person_id)
rescue
e ->
Logger.error(Exception.format(:error, e, __STACKTRACE__))
end

# return the original tuple as expected downstream:
tuple
end

def get_list_seq(list) do
if is_nil(list.seq) do
[]
else
list.seq |> String.split(",")
end
end

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

@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`.
"""
def add_all_items_to_all_list_for_person_id(person_id) do
all_list = App.List.get_all_list_for_person(person_id)
# dbg(all_list)
all_items = App.Item.all_items_for_person(person_id)
# dbg(all_items)
prev_seq = get_list_seq(all_list)
# dbg(prev_seq)
# Add add each `item.id` to the sequence of item ids:
seq =
Enum.reduce(all_items, prev_seq, fn i, acc ->
# Avoid adding duplicates
if Enum.member?(acc, i.cid) do
acc
else
[i.cid | acc]
end
end)
|> Enum.uniq()
|> Enum.filter(fn cid -> cid != nil && cid != "" end)
|> Enum.join(",")

update_list(all_list, %{seq: seq})
end
end
125 changes: 0 additions & 125 deletions lib/app/list_items.ex

This file was deleted.

4 changes: 2 additions & 2 deletions lib/app_web/live/app_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ defmodule AppWeb.AppLive do
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.List.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 @@ -316,7 +316,7 @@ defmodule AppWeb.AppLive do
# "updateIndexes -> seq: #{seq} | list_cid: #{list_cid} | person_id: #{person_id}"
# )

App.ListItems.create_list_items_seq(list_cid, person_id, seq)
App.List.update_list_seq(list_cid, person_id, seq)
{:noreply, socket}
end

Expand Down
1 change: 1 addition & 0 deletions priv/repo/migrations/20230416001029_create_lists.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule App.Repo.Migrations.CreateLists do
add :cid, :string
add :name, :string
add :person_id, :integer
add :seq, :text
add :sort, :integer
add :status, :integer

Expand Down
17 changes: 0 additions & 17 deletions priv/repo/migrations/20230416001045_create_list_items.exs

This file was deleted.

2 changes: 1 addition & 1 deletion priv/repo/seeds.exs
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,5 @@ if env == :test || env == :dev do
App.Item.update_all_items_cid()

# Add items to lists:
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)
end
2 changes: 1 addition & 1 deletion test/app/item_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ defmodule App.ItemTest do
assert NaiveDateTime.diff(timer1.start, started) == 0

# Item must be on a list ...
App.ListItems.add_all_items_to_all_list_for_person_id(item1.person_id)
App.List.add_all_items_to_all_list_for_person_id(item1.person_id)
# list items with timers:
item_timers = Item.items_with_timers(1)
assert length(item_timers) > 0
Expand Down
Loading

0 comments on commit 39a758c

Please sign in to comment.