mobilizon/lib/eventos_web/controllers/event_controller.ex
Thomas Citharel 90ceb4f6fe initial commit
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
2017-12-08 09:58:14 +01:00

61 lines
1.7 KiB
Elixir

defmodule EventosWeb.EventController do
use EventosWeb, :controller
alias Eventos.Events
alias Eventos.Events.Event
def index(conn, _params) do
events = Events.list_events()
render(conn, "index.html", events: events)
end
def new(conn, _params) do
changeset = Events.change_event(%Event{})
render(conn, "new.html", changeset: changeset)
end
def create(conn, %{"event" => event_params}) do
case Events.create_event(event_params) do
{:ok, event} ->
conn
|> put_flash(:info, "Event created successfully.")
|> redirect(to: event_path(conn, :show, event))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
def show(conn, %{"id" => id}) do
event = Events.get_event!(id)
render(conn, "show.html", event: event)
end
def edit(conn, %{"id" => id}) do
event = Events.get_event!(id)
changeset = Events.change_event(event)
render(conn, "edit.html", event: event, changeset: changeset)
end
def update(conn, %{"id" => id, "event" => event_params}) do
event = Events.get_event!(id)
case Events.update_event(event, event_params) do
{:ok, event} ->
conn
|> put_flash(:info, "Event updated successfully.")
|> redirect(to: event_path(conn, :show, event))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "edit.html", event: event, changeset: changeset)
end
end
def delete(conn, %{"id" => id}) do
event = Events.get_event!(id)
{:ok, _event} = Events.delete_event(event)
conn
|> put_flash(:info, "Event deleted successfully.")
|> redirect(to: event_path(conn, :index))
end
end