|
| 1 | +defmodule Data.Task do |
| 2 | + alias Data.Task |
| 3 | + alias Data.Action |
| 4 | + |
| 5 | + defstruct actions: %{} |
| 6 | + |
| 7 | + def empty, do: %Data.Task{} |
| 8 | + |
| 9 | + def add_action(%Task{actions: actions} = task, id, description) do |
| 10 | + %{task | actions: Map.put(actions, id, Action.new(description))} |
| 11 | + end |
| 12 | + |
| 13 | + |
| 14 | + def fetch(%Task{actions: actions} = _, id) do |
| 15 | + Map.fetch(actions, id) |
| 16 | + end |
| 17 | +end |
| 18 | + |
| 19 | +defmodule Data.Action do |
| 20 | + alias Data.Action |
| 21 | + |
| 22 | + defstruct description: "" |
| 23 | + |
| 24 | + def empty, do: %Action{} |
| 25 | + |
| 26 | + def new(description) do |
| 27 | + %Action{ |
| 28 | + description: description |
| 29 | + } |
| 30 | + end |
| 31 | + |
| 32 | + def update_description(action, description) do |
| 33 | + %{action | description: description} |
| 34 | + end |
| 35 | +end |
| 36 | + |
| 37 | +Data.Task.empty() |
| 38 | +|> Data.Task.add_action(1, "asdad") |
| 39 | +|> Data.Task.add_action(2, "d3asd") |
| 40 | +|> Data.Task.add_action(3, "2s2eq") |
| 41 | +|> IO.inspect() |
| 42 | + |
| 43 | +Data.Task.empty() |
| 44 | +|> Data.Task.add_action(1, "asdad") |
| 45 | +|> Data.Task.add_action(2, "d3asd") |
| 46 | +|> Data.Task.add_action(3, "2s2eq") |
| 47 | +|> Data.Task.fetch(1) |
| 48 | +|> case do |
| 49 | + {:ok, data} -> data.description |> String.upcase |
| 50 | + :error -> raise :error |
| 51 | + end |
| 52 | +|> IO.inspect |
| 53 | + |
| 54 | +Data.Action.new("asdad3q") |
| 55 | +|> Data.Action.update_description("dadwqasad") |
| 56 | +|> IO.inspect() |
0 commit comments