mobilizon/lib/eventos_web/controllers/category_controller.ex

61 lines
1.8 KiB
Elixir
Raw Normal View History

defmodule EventosWeb.CategoryController do
use EventosWeb, :controller
alias Eventos.Events
alias Eventos.Events.Category
def index(conn, _params) do
categories = Events.list_categories()
render(conn, "index.html", categories: categories)
end
def new(conn, _params) do
changeset = Events.change_category(%Category{})
render(conn, "new.html", changeset: changeset)
end
def create(conn, %{"category" => category_params}) do
case Events.create_category(category_params) do
{:ok, category} ->
conn
|> put_flash(:info, "Category created successfully.")
|> redirect(to: category_path(conn, :show, category))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
def show(conn, %{"id" => id}) do
category = Events.get_category!(id)
render(conn, "show.html", category: category)
end
def edit(conn, %{"id" => id}) do
category = Events.get_category!(id)
changeset = Events.change_category(category)
render(conn, "edit.html", category: category, changeset: changeset)
end
def update(conn, %{"id" => id, "category" => category_params}) do
category = Events.get_category!(id)
case Events.update_category(category, category_params) do
{:ok, category} ->
conn
|> put_flash(:info, "Category updated successfully.")
|> redirect(to: category_path(conn, :show, category))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "edit.html", category: category, changeset: changeset)
end
end
def delete(conn, %{"id" => id}) do
category = Events.get_category!(id)
{:ok, _category} = Events.delete_category(category)
conn
|> put_flash(:info, "Category deleted successfully.")
|> redirect(to: category_path(conn, :index))
end
end