|
|
@@ -79,6 +79,7 @@ defmodule Vaccins.LocationStore do
|
|
|
|
|
|
use GenServer
|
|
|
require Ex2ms
|
|
|
+ require Logger
|
|
|
import Ecto.Changeset
|
|
|
alias Vaccins.Queries.Doctolib
|
|
|
@name Vaccins.LocationStore
|
|
|
@@ -148,14 +149,22 @@ defmodule Vaccins.LocationStore do
|
|
|
do: {:reply, :ok, new_state}
|
|
|
end
|
|
|
|
|
|
- defp load_state(), do: %{locations: read_file() |> Map.new(&{&1.id, &1})}
|
|
|
+ defp load_state() do
|
|
|
+ with {:ok, locations} <- read_file() do
|
|
|
+ %{locations: locations |> Map.new(&{&1.id, &1})}
|
|
|
+ else
|
|
|
+ {:error, reason} ->
|
|
|
+ Logger.error("Could not read file #{@file_path} : #{reason}")
|
|
|
+ %{locations: %{}}
|
|
|
+ end
|
|
|
+ end
|
|
|
|
|
|
defp read_file(),
|
|
|
do:
|
|
|
with(
|
|
|
{:ok, content} <- File.read(@file_path),
|
|
|
{:ok, decoded} <- Jason.decode(content, keys: :atoms),
|
|
|
- do: decoded |> Enum.map(&Location.from_json/1)
|
|
|
+ do: {:ok, decoded |> Enum.map(&Location.from_json/1)}
|
|
|
)
|
|
|
|
|
|
defp dump_state(%{locations: locations}), do: locations |> Map.values() |> write_file()
|