After "mix phx.gen.json BiddingRounds BiddingRound bidding_rounds round_number:integer running:boolean --no-scope".
This commit is contained in:
@ -0,0 +1,43 @@
|
||||
defmodule BeetRoundServerWeb.BiddingRoundController do
|
||||
use BeetRoundServerWeb, :controller
|
||||
|
||||
alias BeetRoundServer.BiddingRounds
|
||||
alias BeetRoundServer.BiddingRounds.BiddingRound
|
||||
|
||||
action_fallback BeetRoundServerWeb.FallbackController
|
||||
|
||||
def index(conn, _params) do
|
||||
bidding_rounds = BiddingRounds.list_bidding_rounds()
|
||||
render(conn, :index, bidding_rounds: bidding_rounds)
|
||||
end
|
||||
|
||||
def create(conn, %{"bidding_round" => bidding_round_params}) do
|
||||
with {:ok, %BiddingRound{} = bidding_round} <- BiddingRounds.create_bidding_round(bidding_round_params) do
|
||||
conn
|
||||
|> put_status(:created)
|
||||
|> put_resp_header("location", ~p"/api/bidding_rounds/#{bidding_round}")
|
||||
|> render(:show, bidding_round: bidding_round)
|
||||
end
|
||||
end
|
||||
|
||||
def show(conn, %{"id" => id}) do
|
||||
bidding_round = BiddingRounds.get_bidding_round!(id)
|
||||
render(conn, :show, bidding_round: bidding_round)
|
||||
end
|
||||
|
||||
def update(conn, %{"id" => id, "bidding_round" => bidding_round_params}) do
|
||||
bidding_round = BiddingRounds.get_bidding_round!(id)
|
||||
|
||||
with {:ok, %BiddingRound{} = bidding_round} <- BiddingRounds.update_bidding_round(bidding_round, bidding_round_params) do
|
||||
render(conn, :show, bidding_round: bidding_round)
|
||||
end
|
||||
end
|
||||
|
||||
def delete(conn, %{"id" => id}) do
|
||||
bidding_round = BiddingRounds.get_bidding_round!(id)
|
||||
|
||||
with {:ok, %BiddingRound{}} <- BiddingRounds.delete_bidding_round(bidding_round) do
|
||||
send_resp(conn, :no_content, "")
|
||||
end
|
||||
end
|
||||
end
|
||||
25
lib/beet_round_server_web/controllers/bidding_round_json.ex
Normal file
25
lib/beet_round_server_web/controllers/bidding_round_json.ex
Normal file
@ -0,0 +1,25 @@
|
||||
defmodule BeetRoundServerWeb.BiddingRoundJSON do
|
||||
alias BeetRoundServer.BiddingRounds.BiddingRound
|
||||
|
||||
@doc """
|
||||
Renders a list of bidding_rounds.
|
||||
"""
|
||||
def index(%{bidding_rounds: bidding_rounds}) do
|
||||
%{data: for(bidding_round <- bidding_rounds, do: data(bidding_round))}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Renders a single bidding_round.
|
||||
"""
|
||||
def show(%{bidding_round: bidding_round}) do
|
||||
%{data: data(bidding_round)}
|
||||
end
|
||||
|
||||
defp data(%BiddingRound{} = bidding_round) do
|
||||
%{
|
||||
id: bidding_round.id,
|
||||
round_number: bidding_round.round_number,
|
||||
running: bidding_round.running
|
||||
}
|
||||
end
|
||||
end
|
||||
25
lib/beet_round_server_web/controllers/changeset_json.ex
Normal file
25
lib/beet_round_server_web/controllers/changeset_json.ex
Normal file
@ -0,0 +1,25 @@
|
||||
defmodule BeetRoundServerWeb.ChangesetJSON do
|
||||
@doc """
|
||||
Renders changeset errors.
|
||||
"""
|
||||
def error(%{changeset: changeset}) do
|
||||
# When encoded, the changeset returns its errors
|
||||
# as a JSON object. So we just pass it forward.
|
||||
%{errors: Ecto.Changeset.traverse_errors(changeset, &translate_error/1)}
|
||||
end
|
||||
|
||||
defp translate_error({msg, opts}) do
|
||||
# You can make use of gettext to translate error messages by
|
||||
# uncommenting and adjusting the following code:
|
||||
|
||||
# if count = opts[:count] do
|
||||
# Gettext.dngettext(BeetRoundServerWeb.Gettext, "errors", msg, msg, count, opts)
|
||||
# else
|
||||
# Gettext.dgettext(BeetRoundServerWeb.Gettext, "errors", msg, opts)
|
||||
# end
|
||||
|
||||
Enum.reduce(opts, msg, fn {key, value}, acc ->
|
||||
String.replace(acc, "%{#{key}}", fn _ -> to_string(value) end)
|
||||
end)
|
||||
end
|
||||
end
|
||||
24
lib/beet_round_server_web/controllers/fallback_controller.ex
Normal file
24
lib/beet_round_server_web/controllers/fallback_controller.ex
Normal file
@ -0,0 +1,24 @@
|
||||
defmodule BeetRoundServerWeb.FallbackController do
|
||||
@moduledoc """
|
||||
Translates controller action results into valid `Plug.Conn` responses.
|
||||
|
||||
See `Phoenix.Controller.action_fallback/1` for more details.
|
||||
"""
|
||||
use BeetRoundServerWeb, :controller
|
||||
|
||||
# This clause handles errors returned by Ecto's insert/update/delete.
|
||||
def call(conn, {:error, %Ecto.Changeset{} = changeset}) do
|
||||
conn
|
||||
|> put_status(:unprocessable_entity)
|
||||
|> put_view(json: BeetRoundServerWeb.ChangesetJSON)
|
||||
|> render(:error, changeset: changeset)
|
||||
end
|
||||
|
||||
# This clause is an example of how to handle resources that cannot be found.
|
||||
def call(conn, {:error, :not_found}) do
|
||||
conn
|
||||
|> put_status(:not_found)
|
||||
|> put_view(html: BeetRoundServerWeb.ErrorHTML, json: BeetRoundServerWeb.ErrorJSON)
|
||||
|> render(:"404")
|
||||
end
|
||||
end
|
||||
@ -24,9 +24,11 @@ defmodule BeetRoundServerWeb.Router do
|
||||
end
|
||||
|
||||
# Other scopes may use custom stacks.
|
||||
# scope "/api", BeetRoundServerWeb do
|
||||
# pipe_through :api
|
||||
# end
|
||||
scope "/api", BeetRoundServerWeb do
|
||||
pipe_through :api
|
||||
|
||||
resources "/bidding_rounds", BiddingRoundController, except: [:new, :edit]
|
||||
end
|
||||
|
||||
# Enable LiveDashboard and Swoosh mailbox preview in development
|
||||
if Application.compile_env(:beet_round_server, :dev_routes) do
|
||||
|
||||
Reference in New Issue
Block a user