2018-10-11 17:37:39 +02:00
|
|
|
defmodule MobilizonWeb.EventController do
|
2018-01-14 17:56:50 +01:00
|
|
|
@moduledoc """
|
|
|
|
Controller for Events
|
|
|
|
"""
|
2018-10-11 17:37:39 +02:00
|
|
|
use MobilizonWeb, :controller
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Events
|
|
|
|
alias Mobilizon.Events.Event
|
|
|
|
alias Mobilizon.Export.ICalendar
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2018-07-09 14:19:24 +02:00
|
|
|
require Logger
|
2018-06-14 17:25:55 +02:00
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
action_fallback(MobilizonWeb.FallbackController)
|
2018-01-13 23:33:03 +01:00
|
|
|
|
2017-12-08 09:58:14 +01:00
|
|
|
def index(conn, _params) do
|
2018-07-09 14:19:24 +02:00
|
|
|
ip = "88.161.154.97"
|
2018-07-27 10:45:35 +02:00
|
|
|
Logger.debug(inspect(Geolix.lookup(ip), pretty: true))
|
|
|
|
|
|
|
|
with %{
|
2018-10-09 16:07:17 +02:00
|
|
|
city: %Geolix.Adapter.MMDB2.Result.City{
|
2018-07-27 10:45:35 +02:00
|
|
|
city: city,
|
|
|
|
country: country,
|
2018-10-09 16:07:17 +02:00
|
|
|
location: %Geolix.Adapter.MMDB2.Record.Location{
|
|
|
|
latitude: latitude,
|
|
|
|
longitude: longitude
|
|
|
|
}
|
2018-07-27 10:45:35 +02:00
|
|
|
}
|
|
|
|
} <- Geolix.lookup(ip) do
|
|
|
|
distance =
|
|
|
|
case city do
|
|
|
|
nil -> 500_000
|
|
|
|
_ -> 50_000
|
|
|
|
end
|
|
|
|
|
2018-07-09 14:19:24 +02:00
|
|
|
events = Events.find_close_events(longitude, latitude, distance)
|
2018-07-27 10:45:35 +02:00
|
|
|
|
|
|
|
render(
|
|
|
|
conn,
|
|
|
|
"index.json",
|
|
|
|
events: events,
|
|
|
|
coord: %{longitude: longitude, latitude: latitude, distance: distance},
|
|
|
|
city: city,
|
|
|
|
country: country
|
|
|
|
)
|
2018-07-09 14:19:24 +02:00
|
|
|
end
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
def index_all(conn, _params) do
|
|
|
|
events = Events.list_events()
|
|
|
|
render(conn, "index_all.json", events: events)
|
|
|
|
end
|
|
|
|
|
2017-12-08 09:58:14 +01:00
|
|
|
def create(conn, %{"event" => event_params}) do
|
2018-07-04 14:29:17 +02:00
|
|
|
event_params = process_event_address(event_params)
|
|
|
|
Logger.debug("creating event with")
|
2018-07-27 10:45:35 +02:00
|
|
|
Logger.debug(inspect(event_params))
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
with {:ok, %Event{} = event} <- Events.create_event(event_params) do
|
|
|
|
conn
|
|
|
|
|> put_status(:created)
|
2018-06-06 17:42:53 +02:00
|
|
|
|> put_resp_header("location", event_path(conn, :show, event.uuid))
|
2018-01-16 19:45:09 +01:00
|
|
|
|> render("show_simple.json", event: event)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-04 14:29:17 +02:00
|
|
|
defp process_event_address(event) do
|
2018-07-04 16:23:52 +02:00
|
|
|
cond do
|
|
|
|
Map.has_key?(event, "address_type") && event["address_type"] !== :physical ->
|
|
|
|
event
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2018-07-04 16:23:52 +02:00
|
|
|
Map.has_key?(event, "physical_address") ->
|
|
|
|
address = event["physical_address"]
|
2018-10-11 17:37:39 +02:00
|
|
|
geom = MobilizonWeb.AddressController.process_geom(address["geom"])
|
2018-07-27 10:45:35 +02:00
|
|
|
|
|
|
|
address =
|
|
|
|
case geom do
|
|
|
|
nil ->
|
|
|
|
address
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
%{address | "geom" => geom}
|
|
|
|
end
|
|
|
|
|
2018-07-04 16:23:52 +02:00
|
|
|
%{event | "physical_address" => address}
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2018-07-04 16:23:52 +02:00
|
|
|
true ->
|
|
|
|
event
|
2018-01-17 11:39:01 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-30 14:27:21 +02:00
|
|
|
def search(conn, %{"name" => name}) do
|
|
|
|
events = Events.find_events_by_name(name)
|
|
|
|
render(conn, "index.json", events: events)
|
|
|
|
end
|
|
|
|
|
2018-06-06 17:42:53 +02:00
|
|
|
def show(conn, %{"uuid" => uuid}) do
|
2018-06-14 17:25:55 +02:00
|
|
|
case Events.get_event_full_by_uuid(uuid) do
|
|
|
|
nil ->
|
|
|
|
send_resp(conn, 404, "")
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2018-06-14 17:25:55 +02:00
|
|
|
event ->
|
|
|
|
render(conn, "show.json", event: event)
|
|
|
|
end
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
2018-06-06 17:42:53 +02:00
|
|
|
def export_to_ics(conn, %{"uuid" => uuid}) do
|
2018-06-14 18:15:27 +02:00
|
|
|
event = uuid |> Events.get_event_full_by_uuid() |> ICalendar.export_event()
|
2018-01-15 11:40:01 +01:00
|
|
|
send_resp(conn, 200, event)
|
|
|
|
end
|
|
|
|
|
2018-06-06 17:42:53 +02:00
|
|
|
def update(conn, %{"uuid" => uuid, "event" => event_params}) do
|
|
|
|
event = Events.get_event_full_by_uuid(uuid)
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
with {:ok, %Event{} = event} <- Events.update_event(event, event_params) do
|
2018-01-16 19:45:09 +01:00
|
|
|
render(conn, "show_simple.json", event: event)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-06 17:42:53 +02:00
|
|
|
def delete(conn, %{"uuid" => uuid}) do
|
2018-06-14 17:25:55 +02:00
|
|
|
with event <- Events.get_event_by_uuid(uuid),
|
2018-07-27 10:45:35 +02:00
|
|
|
{:ok, %Event{}} <- Events.delete_event(event) do
|
2018-01-13 23:33:03 +01:00
|
|
|
send_resp(conn, :no_content, "")
|
|
|
|
end
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
end
|