44 lines
1.3 KiB
Elixir
44 lines
1.3 KiB
Elixir
defmodule GenericRestServerWeb.ItemController do
|
|
use GenericRestServerWeb, :controller
|
|
|
|
alias GenericRestServer.Items
|
|
alias GenericRestServer.Items.Item
|
|
|
|
action_fallback GenericRestServerWeb.FallbackController
|
|
|
|
def index(conn, _params) do
|
|
items = Items.list_items(conn.assigns.current_scope)
|
|
render(conn, :index, items: items)
|
|
end
|
|
|
|
def create(conn, %{"item" => item_params}) do
|
|
with {:ok, %Item{} = item} <- Items.create_item(conn.assigns.current_scope, item_params) do
|
|
conn
|
|
|> put_status(:created)
|
|
|> put_resp_header("location", ~p"/api/items/#{item}")
|
|
|> render(:show, item: item)
|
|
end
|
|
end
|
|
|
|
def show(conn, %{"id" => id}) do
|
|
item = Items.get_item!(conn.assigns.current_scope, id)
|
|
render(conn, :show, item: item)
|
|
end
|
|
|
|
def update(conn, %{"id" => id, "item" => item_params}) do
|
|
item = Items.get_item!(conn.assigns.current_scope, id)
|
|
|
|
with {:ok, %Item{} = item} <- Items.update_item(conn.assigns.current_scope, item, item_params) do
|
|
render(conn, :show, item: item)
|
|
end
|
|
end
|
|
|
|
def delete(conn, %{"id" => id}) do
|
|
item = Items.get_item!(conn.assigns.current_scope, id)
|
|
|
|
with {:ok, %Item{}} <- Items.delete_item(conn.assigns.current_scope, item) do
|
|
send_resp(conn, :no_content, "")
|
|
end
|
|
end
|
|
end
|