|
|
@@ -88,8 +88,7 @@ defmodule Vaccins.LocationStore do
|
|
|
|
|
|
@impl true
|
|
|
def init(_opts) do
|
|
|
- locations = read_file() |> Map.new(&{&1.id, &1})
|
|
|
- {:ok, %{locations: locations}}
|
|
|
+ {:ok, load_state()}
|
|
|
end
|
|
|
|
|
|
def get_locations() do
|
|
|
@@ -124,9 +123,10 @@ defmodule Vaccins.LocationStore do
|
|
|
}
|
|
|
|> Location.set_id(),
|
|
|
locations <- locations |> Map.put(processed.id, processed),
|
|
|
- :ok <- locations |> Map.values() |> write_file() do
|
|
|
+ new_state <- %{locations: locations},
|
|
|
+ :ok <- new_state |> dump_state do
|
|
|
:ok
|
|
|
- {:reply, :ok, %{locations: locations}}
|
|
|
+ {:reply, :ok, new_state}
|
|
|
else
|
|
|
e = {:error, _} -> {:reply, e, state}
|
|
|
end
|
|
|
@@ -135,10 +135,13 @@ defmodule Vaccins.LocationStore do
|
|
|
@impl true
|
|
|
def handle_call({:delete_location, id}, _, state = %{locations: locations}) do
|
|
|
with locations <- locations |> Map.delete(id),
|
|
|
- :ok <- locations |> Map.values() |> write_file(),
|
|
|
- do: {:reply, :ok, %{locations: locations}}
|
|
|
+ new_state <- %{locations: locations},
|
|
|
+ :ok <- new_state |> dump_state,
|
|
|
+ do: {:reply, :ok, new_state}
|
|
|
end
|
|
|
|
|
|
+ defp load_state(), do: %{locations: read_file() |> Map.new(&{&1.id, &1})}
|
|
|
+
|
|
|
defp read_file(),
|
|
|
do:
|
|
|
with(
|
|
|
@@ -147,6 +150,8 @@ defmodule Vaccins.LocationStore do
|
|
|
do: decoded |> Enum.map(&Location.from_json/1)
|
|
|
)
|
|
|
|
|
|
+ defp dump_state(state = %{locations: locations}), do: locations |> Map.values() |> write_file()
|
|
|
+
|
|
|
defp write_file(locations),
|
|
|
do:
|
|
|
with(
|