2017-12-08 09:58:14 +01:00
|
|
|
defmodule EventosWeb.CategoryController do
|
2018-01-14 17:56:50 +01:00
|
|
|
@moduledoc """
|
|
|
|
Controller for Categories
|
|
|
|
"""
|
2017-12-08 09:58:14 +01:00
|
|
|
use EventosWeb, :controller
|
|
|
|
|
|
|
|
alias Eventos.Events
|
|
|
|
alias Eventos.Events.Category
|
|
|
|
|
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
|
|
|
|
categories = Events.list_categories()
|
2018-01-13 23:33:03 +01:00
|
|
|
render(conn, "index.json", categories: categories)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def create(conn, %{"category" => category_params}) do
|
2018-01-13 23:33:03 +01:00
|
|
|
with {:ok, %Category{} = category} <- Events.create_category(category_params) do
|
|
|
|
conn
|
|
|
|
|> put_status(:created)
|
|
|
|
|> put_resp_header("location", category_path(conn, :show, category))
|
|
|
|
|> render("show.json", category: category)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def show(conn, %{"id" => id}) do
|
|
|
|
category = Events.get_category!(id)
|
2018-01-13 23:33:03 +01:00
|
|
|
render(conn, "show.json", category: category)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update(conn, %{"id" => id, "category" => category_params}) do
|
|
|
|
category = Events.get_category!(id)
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
with {:ok, %Category{} = category} <- Events.update_category(category, category_params) do
|
|
|
|
render(conn, "show.json", category: category)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(conn, %{"id" => id}) do
|
|
|
|
category = Events.get_category!(id)
|
2018-01-13 23:33:03 +01:00
|
|
|
with {:ok, %Category{}} <- Events.delete_category(category) do
|
|
|
|
send_resp(conn, :no_content, "")
|
|
|
|
end
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
end
|