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:
29
lib/generic_rest_server_web/controllers/item_json.ex
Normal file
29
lib/generic_rest_server_web/controllers/item_json.ex
Normal file
@ -0,0 +1,29 @@
|
||||
defmodule GenericRestServerWeb.ItemJSON do
|
||||
alias GenericRestServer.Items.Item
|
||||
|
||||
@doc """
|
||||
Renders a list of items.
|
||||
"""
|
||||
def index(%{items: items}) do
|
||||
%{data: for(item <- items, do: data(item))}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Renders a single item.
|
||||
"""
|
||||
def show(%{item: item}) do
|
||||
%{data: data(item)}
|
||||
end
|
||||
|
||||
defp data(%Item{} = item) do
|
||||
%{
|
||||
id: item.id,
|
||||
name: item.name,
|
||||
description: item.description,
|
||||
info: item.info,
|
||||
amount: item.amount,
|
||||
factor: item.factor,
|
||||
type: item.type
|
||||
}
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user