Added JSON API for biddings. (Only listing all biddings is supported)

This commit is contained in:
2026-02-12 10:41:21 +01:00
parent c994ea171e
commit 9709dbabe2
5 changed files with 174 additions and 0 deletions

View File

@ -44,6 +44,10 @@ defmodule BeetRoundServer.Biddings do
Repo.all_by(Bidding, user_id: scope.user.id)
end
def list_biddings() do
Repo.all(Bidding)
end
@doc """
Gets a single bidding.

View File

@ -0,0 +1,45 @@
defmodule BeetRoundServerWeb.BiddingController do
use BeetRoundServerWeb, :controller
alias BeetRoundServer.Biddings
# alias BeetRoundServer.Biddings.Bidding
action_fallback BeetRoundServerWeb.FallbackController
def index(conn, _params) do
biddings = Biddings.list_biddings()
IO.puts("biddings:")
IO.inspect(biddings)
render(conn, :index, biddings: biddings)
end
# def create(conn, %{"bidding" => bidding_params}) do
# with {:ok, %Bidding{} = bidding} <- Biddings.create_bidding(bidding_params) do
# conn
# |> put_status(:created)
# |> put_resp_header("location", ~p"/api/biddings/#{bidding}")
# |> render(:show, bidding: bidding)
# end
# end
# def show(conn, %{"id" => id}) do
# bidding = Biddings.get_bidding!(id)
# render(conn, :show, bidding: bidding)
# end
# def update(conn, %{"id" => id, "bidding" => bidding_params}) do
# bidding = Biddings.get_bidding!(id)
# with {:ok, %Bidding{} = bidding} <- Biddings.update_bidding(bidding, bidding_params) do
# render(conn, :show, bidding: bidding)
# end
# end
# def delete(conn, %{"id" => id}) do
# bidding = Biddings.get_bidding!(id)
# with {:ok, %Bidding{}} <- Biddings.delete_bidding(bidding) do
# send_resp(conn, :no_content, "")
# end
# end
end

View File

@ -0,0 +1,28 @@
defmodule BeetRoundServerWeb.BiddingJSON do
alias BeetRoundServer.Biddings.Bidding
@doc """
Renders a list of biddings.
"""
def index(%{biddings: biddings}) do
%{data: for(bidding <- biddings, do: data(bidding))}
end
@doc """
Renders a single bidding.
"""
def show(%{bidding: bidding}) do
%{data: data(bidding)}
end
defp data(%Bidding{} = bidding) do
%{
user_id: bidding.user_id,
id: bidding.id,
bidding_round: bidding.bidding_round,
amount: bidding.amount,
depot_wish_one: bidding.depot_wish_one,
depot_wish_two: bidding.depot_wish_two
}
end
end

View File

@ -31,6 +31,7 @@ defmodule BeetRoundServerWeb.Router do
resources "/bidding_rounds", BiddingRoundController, except: [:new, :edit]
resources "/users", UserController, except: [:new, :edit]
resources "/biddings", BiddingController, except: [:new, :edit]
end
# Enable LiveDashboard and Swoosh mailbox preview in development

View File

@ -0,0 +1,96 @@
defmodule BeetRoundServerWeb.BiddingControllerTest do
use BeetRoundServerWeb.ConnCase
import BeetRoundServer.BiddingsFixtures
alias BeetRoundServer.Biddings.Bidding
@create_attrs %{
amount: 42,
bidding_round: 42,
depot_wish_one: "some depot_wish_one",
depot_wish_two: "some depot_wish_two"
}
@update_attrs %{
amount: 43,
bidding_round: 43,
depot_wish_one: "some updated depot_wish_one",
depot_wish_two: "some updated depot_wish_two"
}
@invalid_attrs %{amount: nil, bidding_round: nil, depot_wish_one: nil, depot_wish_two: nil}
setup %{conn: conn} do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end
describe "index" do
test "lists all biddings", %{conn: conn} do
conn = get(conn, ~p"/api/biddings")
assert json_response(conn, 200)["data"] == []
end
end
describe "create bidding" do
test "renders bidding when data is valid", %{conn: conn} do
conn = post(conn, ~p"/api/biddings", bidding: @create_attrs)
assert %{"id" => id} = json_response(conn, 201)["data"]
conn = get(conn, ~p"/api/biddings/#{id}")
assert %{
"id" => ^id,
"amount" => 42,
"bidding_round" => 42,
"depot_wish_one" => "some depot_wish_one",
"depot_wish_two" => "some depot_wish_two"
} = json_response(conn, 200)["data"]
end
test "renders errors when data is invalid", %{conn: conn} do
conn = post(conn, ~p"/api/biddings", bidding: @invalid_attrs)
assert json_response(conn, 422)["errors"] != %{}
end
end
describe "update bidding" do
setup [:create_bidding]
test "renders bidding when data is valid", %{conn: conn, bidding: %Bidding{id: id} = bidding} do
conn = put(conn, ~p"/api/biddings/#{bidding}", bidding: @update_attrs)
assert %{"id" => ^id} = json_response(conn, 200)["data"]
conn = get(conn, ~p"/api/biddings/#{id}")
assert %{
"id" => ^id,
"amount" => 43,
"bidding_round" => 43,
"depot_wish_one" => "some updated depot_wish_one",
"depot_wish_two" => "some updated depot_wish_two"
} = json_response(conn, 200)["data"]
end
test "renders errors when data is invalid", %{conn: conn, bidding: bidding} do
conn = put(conn, ~p"/api/biddings/#{bidding}", bidding: @invalid_attrs)
assert json_response(conn, 422)["errors"] != %{}
end
end
describe "delete bidding" do
setup [:create_bidding]
test "deletes chosen bidding", %{conn: conn, bidding: bidding} do
conn = delete(conn, ~p"/api/biddings/#{bidding}")
assert response(conn, 204)
assert_error_sent 404, fn ->
get(conn, ~p"/api/biddings/#{bidding}")
end
end
end
defp create_bidding(_) do
bidding = bidding_fixture()
%{bidding: bidding}
end
end