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:
43
lib/generic_rest_server_web/controllers/item_controller.ex
Normal file
43
lib/generic_rest_server_web/controllers/item_controller.ex
Normal 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
|
||||
Reference in New Issue
Block a user