|
|
@@ -23,9 +23,7 @@ defmodule Vaccins.LocationStore do
|
|
|
when params != [],
|
|
|
do: %{l | availability_query: params |> provider.new}
|
|
|
|
|
|
- def query_availability(
|
|
|
- l = %Location{id: id, availability_query: q, booking_page: bp, provider: provider}
|
|
|
- ) do
|
|
|
+ def query_availability(%Location{availability_query: q, provider: provider}) do
|
|
|
with url <- q |> provider.to_url() |> URI.to_string(),
|
|
|
{:ok, result} <- url |> Vaccins.Scraper.get_json() do
|
|
|
case result |> provider.analyze_result() do
|
|
|
@@ -120,29 +118,28 @@ defmodule Vaccins.LocationStore do
|
|
|
def handle_call({:add_location, params}, _, state = %{locations: locations}) do
|
|
|
cs = params |> LocationRaw.changeset()
|
|
|
|
|
|
- resp =
|
|
|
- with {:ok, raw_location} <- cs |> apply_action(:insert),
|
|
|
- processed <-
|
|
|
- %Location{
|
|
|
- name: raw_location.name,
|
|
|
- location: raw_location.location,
|
|
|
- booking_page: raw_location.booking_page,
|
|
|
- availability_query_params: raw_location |> LocationRaw.to_query_params(),
|
|
|
- provider: Doctolib
|
|
|
- }
|
|
|
- |> Location.set_id(),
|
|
|
- locations <- locations |> Map.put(processed.id, processed),
|
|
|
- new_state <- %{locations: locations},
|
|
|
- :ok <- new_state |> dump_state do
|
|
|
- :ok
|
|
|
- {:reply, :ok, new_state}
|
|
|
- else
|
|
|
- e = {:error, _} -> {:reply, e, state}
|
|
|
- end
|
|
|
+ with {:ok, raw_location} <- cs |> apply_action(:insert),
|
|
|
+ processed <-
|
|
|
+ %Location{
|
|
|
+ name: raw_location.name,
|
|
|
+ location: raw_location.location,
|
|
|
+ booking_page: raw_location.booking_page,
|
|
|
+ availability_query_params: raw_location |> LocationRaw.to_query_params(),
|
|
|
+ provider: Doctolib
|
|
|
+ }
|
|
|
+ |> Location.set_id(),
|
|
|
+ locations <- locations |> Map.put(processed.id, processed),
|
|
|
+ new_state <- %{locations: locations},
|
|
|
+ :ok <- new_state |> dump_state do
|
|
|
+ :ok
|
|
|
+ {:reply, :ok, new_state}
|
|
|
+ else
|
|
|
+ e = {:error, _} -> {:reply, e, state}
|
|
|
+ end
|
|
|
end
|
|
|
|
|
|
@impl true
|
|
|
- def handle_call({:delete_location, id}, _, state = %{locations: locations}) do
|
|
|
+ def handle_call({:delete_location, id}, _, %{locations: locations}) do
|
|
|
with locations <- locations |> Map.delete(id),
|
|
|
new_state <- %{locations: locations},
|
|
|
:ok <- new_state |> dump_state,
|
|
|
@@ -159,7 +156,7 @@ 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 dump_state(%{locations: locations}), do: locations |> Map.values() |> write_file()
|
|
|
|
|
|
defp write_file(locations),
|
|
|
do:
|