Current section
Files
Jump to
Current section
Files
priv/templates/use_case.gen.phx_resource_json/controller.eex
<%= if Keyword.get(@option_context, :scoped, false) do %>
defmodule <%= @context[:web_module] %>.<%= @option_context[:scoped] %>.<%= @context[:module] |> String.replace("#{@context[:base]}.", "") %>Controller do
<% else %>
defmodule <%= @context[:web_module] %>.<%= @context[:module] |> String.replace("#{@context[:base]}.", "") %>Controller do
<% end %>
use <%= @context[:web_module] %>, :controller
<%= if Keyword.get(@option_context, :scoped, false) do %>
alias <%= @context[:base] %>.<%= @option_context[:scoped] %>.Repo.<%= @context[:scoped] %>
<% else %>
alias <%= @context[:base] %>.Repo.<%= @context[:scoped] %>
<% end %>
action_fallback <%= @context[:web_module] %>.FallbackController
def index(conn, _params) do
render(conn, "index.json", <%= @schema.plural %>: <%= @context[:alias] %>.list())
end
def create(conn, %{<%= inspect @schema.singular %> => <%= @schema.singular %>_params}) do
with {:ok, <%= @schema.singular %>} <- <%= @context[:alias] %>.create(<%= @schema.singular %>_params) do
conn
|> put_status(:created)
|> put_resp_header("location", Routes.<%= @schema.route_helper %>_path(conn, :show, <%= @schema.singular %>))
|> render("show.json", <%= @schema.singular %>: <%= @schema.singular %>)
end
end
def show(conn, %{"id" => id}) do
render(conn, "show.json", <%= @schema.singular %>: <%= @context[:alias] %>.get!(id))
end
def update(conn, %{"id" => id, <%= inspect @schema.singular %> => <%= @schema.singular %>_params}) do
<%= @schema.singular %> = <%= @context[:alias] %>.get!(id)
with {:ok, _} <- <%= @context[:alias] %>.update(<%= @schema.singular %>, <%= @schema.singular %>_params) do
render(conn, "show.json", <%= @schema.singular %>: <%= @schema.singular %>)
end
end
def delete(conn, %{"id" => id}) do
with {:ok, _} <- <%= @context[:alias] %>.delete(<%= @context[:alias] %>.get!(id)) do
send_resp(conn, :no_content, "")
end
end
end