2017-12-08 09:58:14 +01:00
|
|
|
defmodule EventosWeb.EventController do
|
2018-01-14 17:56:50 +01:00
|
|
|
@moduledoc """
|
|
|
|
Controller for Events
|
|
|
|
"""
|
2017-12-08 09:58:14 +01:00
|
|
|
use EventosWeb, :controller
|
|
|
|
|
|
|
|
alias Eventos.Events
|
|
|
|
alias Eventos.Events.Event
|
2018-01-15 11:40:01 +01:00
|
|
|
alias Eventos.Export.ICalendar
|
2018-01-21 19:43:16 +01:00
|
|
|
alias Eventos.Addresses
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
action_fallback EventosWeb.FallbackController
|
|
|
|
|
2017-12-08 09:58:14 +01:00
|
|
|
def index(conn, _params) do
|
|
|
|
events = Events.list_events()
|
2018-01-13 23:33:03 +01:00
|
|
|
render(conn, "index.json", events: events)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def create(conn, %{"event" => event_params}) do
|
2018-01-17 11:39:01 +01:00
|
|
|
event_params = %{event_params | "address" => process_address(event_params["address"])}
|
2018-01-13 23:33:03 +01:00
|
|
|
with {:ok, %Event{} = event} <- Events.create_event(event_params) do
|
|
|
|
conn
|
|
|
|
|> put_status(:created)
|
|
|
|
|> put_resp_header("location", event_path(conn, :show, event))
|
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-01-17 11:39:01 +01:00
|
|
|
defp process_address(address) do
|
2018-01-21 19:43:16 +01:00
|
|
|
case Addresses.process_geom(address["geom"]) do
|
|
|
|
{:ok, geom} ->
|
2018-01-17 11:39:01 +01:00
|
|
|
%{address | "geom" => geom}
|
2018-01-21 19:43:16 +01:00
|
|
|
_ ->
|
|
|
|
address
|
2018-01-17 11:39:01 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-19 10:19:21 +02:00
|
|
|
def show(conn, %{"username" => username, "slug" => slug}) do
|
|
|
|
event = Events.get_event_full_by_name_and_slug!(username, slug)
|
2018-01-13 23:33:03 +01:00
|
|
|
render(conn, "show.json", event: event)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
2018-05-19 10:19:21 +02:00
|
|
|
def export_to_ics(conn, %{"username" => username, "slug" => slug}) do
|
|
|
|
event = Events.get_event_full_by_name_and_slug!(username, slug)
|
2018-01-15 11:40:01 +01:00
|
|
|
|> ICalendar.export_event()
|
|
|
|
send_resp(conn, 200, event)
|
|
|
|
end
|
|
|
|
|
2018-05-19 10:19:21 +02:00
|
|
|
def update(conn, %{"username" => username, "slug" => slug, "event" => event_params}) do
|
|
|
|
event = Events.get_event_full_by_name_and_slug!(username, slug)
|
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-05-19 10:19:21 +02:00
|
|
|
def delete(conn, %{"username" => username, "slug" => slug}) do
|
|
|
|
event = Events.get_event_full_by_name_and_slug!(username, slug)
|
2018-01-13 23:33:03 +01:00
|
|
|
with {:ok, %Event{}} <- Events.delete_event(event) do
|
|
|
|
send_resp(conn, :no_content, "")
|
|
|
|
end
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
end
|