From 7380a18bd186e130c9e7901647f45019b6d179cc Mon Sep 17 00:00:00 2001 From: LucasLvy Date: Thu, 17 Oct 2024 15:50:52 +0200 Subject: [PATCH] feat(backend): get remaining tickets --- backend/lib/peach/event.ex | 3 +- backend/lib/peach/ticket_tiers.ex | 28 ++++++++++++- .../peach_web/controllers/event_controller.ex | 13 +------ .../controllers/ticket_tier_controller.ex | 14 +++++++ backend/lib/peach_web/router.ex | 1 + .../peach_web/controllers/get_events_test.exs | 12 ++++-- .../ticket_tier_controller_test.exs | 39 ++++++++++++++++++- 7 files changed, 90 insertions(+), 20 deletions(-) diff --git a/backend/lib/peach/event.ex b/backend/lib/peach/event.ex index fdd5ca9..d08cab2 100644 --- a/backend/lib/peach/event.ex +++ b/backend/lib/peach/event.ex @@ -5,7 +5,8 @@ defmodule Peach.Event do use Ecto.Schema import Ecto.Changeset - @derive Jason.Encoder + @derive {Jason.Encoder, + only: [:name, :description, :cover, :start, :end, :location, :treasury, :id]} schema "events" do field :name, :string field :start, :naive_datetime diff --git a/backend/lib/peach/ticket_tiers.ex b/backend/lib/peach/ticket_tiers.ex index 2230fd1..1700696 100644 --- a/backend/lib/peach/ticket_tiers.ex +++ b/backend/lib/peach/ticket_tiers.ex @@ -3,10 +3,34 @@ defmodule Peach.TicketTiers do Manages the events for the peach app """ alias Peach.Repo - alias Peach.TicketTier - + alias Peach.{Ticket, TicketTier} import Ecto.Query def event_ticket_tiers(event_id), do: Repo.all(from tt in TicketTier, where: tt.event_id == ^event_id) + + def remaining_tickets(ticket_tier_id) do + case Repo.get(TicketTier, ticket_tier_id) do + nil -> + {:error, "Ticket tier not found"} + + ticket_tier -> + sold_tickets = + Repo.one( + from(t in Ticket, + where: t.ticket_tier_id == ^ticket_tier_id, + select: count(t.id) + ) + ) + + {:ok, + %{ + id: ticket_tier.id, + name: ticket_tier.name, + description: ticket_tier.description, + remaining: ticket_tier.max_supply - sold_tickets, + max_supply: ticket_tier.max_supply + }} + end + end end diff --git a/backend/lib/peach_web/controllers/event_controller.ex b/backend/lib/peach_web/controllers/event_controller.ex index 6913c0e..1f5a4d3 100644 --- a/backend/lib/peach_web/controllers/event_controller.ex +++ b/backend/lib/peach_web/controllers/event_controller.ex @@ -26,7 +26,7 @@ defmodule PeachWeb.EventController do {:ok, events} -> conn |> put_status(:ok) - |> json(%{events: Enum.map(events, &format_event/1)}) + |> json(%{events: events}) {:error, error} -> conn @@ -35,17 +35,6 @@ defmodule PeachWeb.EventController do end end - defp format_event(event), - do: %{ - "id" => event.id, - "name" => event.name, - "description" => event.description, - "start" => event.start, - "end" => event.end, - "location" => event.location, - "cover" => event.cover - } - @doc """ Updates the name of an event. """ diff --git a/backend/lib/peach_web/controllers/ticket_tier_controller.ex b/backend/lib/peach_web/controllers/ticket_tier_controller.ex index 6cb60f0..e74f5dd 100644 --- a/backend/lib/peach_web/controllers/ticket_tier_controller.ex +++ b/backend/lib/peach_web/controllers/ticket_tier_controller.ex @@ -9,4 +9,18 @@ defmodule PeachWeb.TicketTierController do |> put_status(:ok) |> json(%{ticket_tiers: ticket_tiers}) end + + def show(conn, %{"id" => ticket_tier_id}) do + case TicketTiers.remaining_tickets(ticket_tier_id) do + {:ok, ticket_tier} -> + conn + |> put_status(:ok) + |> json(%{ticket_tier: ticket_tier}) + + {:error, error} -> + conn + |> put_status(:not_found) + |> json(%{errors: error}) + end + end end diff --git a/backend/lib/peach_web/router.ex b/backend/lib/peach_web/router.ex index 0df3120..86ced37 100644 --- a/backend/lib/peach_web/router.ex +++ b/backend/lib/peach_web/router.ex @@ -12,6 +12,7 @@ defmodule PeachWeb.Router do get "/events", EventController, :index get "/tickets/:address", TicketController, :index get "/events/:id/ticket_tiers", TicketTierController, :index + get "/ticket_tiers/:id", TicketTierController, :show end # Enable LiveDashboard and Swoosh mailbox preview in development diff --git a/backend/test/peach_web/controllers/get_events_test.exs b/backend/test/peach_web/controllers/get_events_test.exs index 3d515e5..117cac2 100644 --- a/backend/test/peach_web/controllers/get_events_test.exs +++ b/backend/test/peach_web/controllers/get_events_test.exs @@ -9,7 +9,8 @@ defmodule PeachWeb.GetEventControllerTest do Repo.insert!(%Event{ name: "Past Event", start: ~N[2024-11-05 09:00:00], - end: ~N[2024-11-06 17:00:00] + end: ~N[2024-11-06 17:00:00], + treasury: "0xbeef" }) event2 = @@ -23,7 +24,8 @@ defmodule PeachWeb.GetEventControllerTest do Repo.insert!(%Event{ name: "Future Event", start: ~N[2024-11-15 09:00:00], - end: ~N[2024-11-17 17:00:00] + end: ~N[2024-11-17 17:00:00], + treasury: "0xbeef" }) {:ok, event1: event1, event2: event2, event3: event3} @@ -121,7 +123,8 @@ defmodule PeachWeb.GetEventControllerTest do "start" => NaiveDateTime.to_iso8601(event2.start), "end" => NaiveDateTime.to_iso8601(event2.end), "location" => event2.location, - "cover" => event2.cover + "cover" => event2.cover, + "treasury" => event2.treasury } ] end @@ -140,7 +143,8 @@ defmodule PeachWeb.GetEventControllerTest do "start" => NaiveDateTime.to_iso8601(event3.start), "end" => NaiveDateTime.to_iso8601(event3.end), "location" => event3.location, - "cover" => event3.cover + "cover" => event3.cover, + "treasury" => event3.treasury } ] diff --git a/backend/test/peach_web/controllers/ticket_tier_controller_test.exs b/backend/test/peach_web/controllers/ticket_tier_controller_test.exs index 6ae93fc..8f15604 100644 --- a/backend/test/peach_web/controllers/ticket_tier_controller_test.exs +++ b/backend/test/peach_web/controllers/ticket_tier_controller_test.exs @@ -1,6 +1,6 @@ defmodule PeachWeb.TicketTierControllerTest do use PeachWeb.ConnCase, async: true - alias Peach.{Event, Repo, TicketTier} + alias Peach.{Event, Repo, Ticket, TicketTier} setup do # Insert a sample event and ticket tiers for testing @@ -27,7 +27,10 @@ defmodule PeachWeb.TicketTierControllerTest do event_id: event.id }) + # Create a few tickets associated with this tier + _ticket1 = Repo.insert!(%Ticket{ticket_tier_id: vip_tier.id}) # Pass event and ticket tiers to each test case + _ticket2 = Repo.insert!(%Ticket{ticket_tier_id: vip_tier.id}) {:ok, event: event, vip_tier: vip_tier, standard_tier: standard_tier} end @@ -72,4 +75,38 @@ defmodule PeachWeb.TicketTierControllerTest do # Assert the response status and error message assert json_response(conn, 200) == %{"ticket_tiers" => []} end + + test "returns ticket tier with remaining tickets for valid id", %{ + conn: conn, + vip_tier: ticket_tier + } do + # Call the show endpoint with a valid ticket_tier_id + conn = get(conn, "/api/ticket_tiers/#{ticket_tier.id}") + + # Expected response + expected_response = %{ + "ticket_tier" => %{ + "id" => ticket_tier.id, + "name" => ticket_tier.name, + "description" => ticket_tier.description, + "max_supply" => ticket_tier.max_supply, + # Expected remaining tickets (50 - 2 tickets sold) + "remaining" => 48 + } + } + + # Assert that the response status is 200 and matches the expected response + assert json_response(conn, 200) == expected_response + end + + test "returns error for invalid ticket_tier_id", %{conn: conn} do + # Call the show endpoint with an invalid ticket_tier_id + conn = get(conn, "/api/ticket_tiers/9999") + + # Expected error response (adjust based on your actual error handling) + expected_error = %{"errors" => "Ticket tier not found"} + + # Assert the response status and error message + assert json_response(conn, 404) == expected_error + end end