2017-12-08 09:58:14 +01:00
|
|
|
defmodule EventosWeb.EventController do
|
|
|
|
use EventosWeb, :controller
|
|
|
|
|
|
|
|
alias Eventos.Events
|
|
|
|
alias Eventos.Events.Event
|
|
|
|
|
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-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))
|
|
|
|
|> render("show.json", event: event)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def show(conn, %{"id" => id}) do
|
|
|
|
event = Events.get_event!(id)
|
2018-01-13 23:33:03 +01:00
|
|
|
render(conn, "show.json", event: event)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update(conn, %{"id" => id, "event" => event_params}) do
|
|
|
|
event = Events.get_event!(id)
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
with {:ok, %Event{} = event} <- Events.update_event(event, event_params) do
|
|
|
|
render(conn, "show.json", event: event)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(conn, %{"id" => id}) do
|
|
|
|
event = Events.get_event!(id)
|
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
|