After 'mix phx.gen.json Items Item items name:string description:string info:string amount:integer factor:float type:string --no-context --no-schema'.

This commit is contained in:
2026-04-21 13:56:18 +02:00
parent 851665ef60
commit 6076654aa4
6 changed files with 224 additions and 3 deletions

View File

@ -0,0 +1,43 @@
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