After "mix phx.gen.json BiddingRounds BiddingRound bidding_rounds round_number:integer running:boolean --no-scope".
This commit is contained in:
104
lib/beet_round_server/bidding_rounds.ex
Normal file
104
lib/beet_round_server/bidding_rounds.ex
Normal file
@ -0,0 +1,104 @@
|
||||
defmodule BeetRoundServer.BiddingRounds do
|
||||
@moduledoc """
|
||||
The BiddingRounds context.
|
||||
"""
|
||||
|
||||
import Ecto.Query, warn: false
|
||||
alias BeetRoundServer.Repo
|
||||
|
||||
alias BeetRoundServer.BiddingRounds.BiddingRound
|
||||
|
||||
@doc """
|
||||
Returns the list of bidding_rounds.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> list_bidding_rounds()
|
||||
[%BiddingRound{}, ...]
|
||||
|
||||
"""
|
||||
def list_bidding_rounds do
|
||||
Repo.all(BiddingRound)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a single bidding_round.
|
||||
|
||||
Raises `Ecto.NoResultsError` if the Bidding round does not exist.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_bidding_round!(123)
|
||||
%BiddingRound{}
|
||||
|
||||
iex> get_bidding_round!(456)
|
||||
** (Ecto.NoResultsError)
|
||||
|
||||
"""
|
||||
def get_bidding_round!(id), do: Repo.get!(BiddingRound, id)
|
||||
|
||||
@doc """
|
||||
Creates a bidding_round.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> create_bidding_round(%{field: value})
|
||||
{:ok, %BiddingRound{}}
|
||||
|
||||
iex> create_bidding_round(%{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def create_bidding_round(attrs) do
|
||||
%BiddingRound{}
|
||||
|> BiddingRound.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates a bidding_round.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> update_bidding_round(bidding_round, %{field: new_value})
|
||||
{:ok, %BiddingRound{}}
|
||||
|
||||
iex> update_bidding_round(bidding_round, %{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def update_bidding_round(%BiddingRound{} = bidding_round, attrs) do
|
||||
bidding_round
|
||||
|> BiddingRound.changeset(attrs)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a bidding_round.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> delete_bidding_round(bidding_round)
|
||||
{:ok, %BiddingRound{}}
|
||||
|
||||
iex> delete_bidding_round(bidding_round)
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def delete_bidding_round(%BiddingRound{} = bidding_round) do
|
||||
Repo.delete(bidding_round)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an `%Ecto.Changeset{}` for tracking bidding_round changes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_bidding_round(bidding_round)
|
||||
%Ecto.Changeset{data: %BiddingRound{}}
|
||||
|
||||
"""
|
||||
def change_bidding_round(%BiddingRound{} = bidding_round, attrs \\ %{}) do
|
||||
BiddingRound.changeset(bidding_round, attrs)
|
||||
end
|
||||
end
|
||||
20
lib/beet_round_server/bidding_rounds/bidding_round.ex
Normal file
20
lib/beet_round_server/bidding_rounds/bidding_round.ex
Normal file
@ -0,0 +1,20 @@
|
||||
defmodule BeetRoundServer.BiddingRounds.BiddingRound do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
@primary_key {:id, :binary_id, autogenerate: true}
|
||||
@foreign_key_type :binary_id
|
||||
schema "bidding_rounds" do
|
||||
field :round_number, :integer
|
||||
field :running, :boolean, default: false
|
||||
|
||||
timestamps(type: :utc_datetime)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def changeset(bidding_round, attrs) do
|
||||
bidding_round
|
||||
|> cast(attrs, [:round_number, :running])
|
||||
|> validate_required([:round_number, :running])
|
||||
end
|
||||
end
|
||||
@ -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