From 5841d5df41baef9d2fb1d0bd6d3d31e85ae10133 Mon Sep 17 00:00:00 2001 From: LucasLvy Date: Thu, 17 Oct 2024 14:39:10 +0200 Subject: [PATCH] feat(backend): ticket tiers from event id --- backend/lib/peach/ticket_tier.ex | 2 +- backend/lib/peach/ticket_tiers.ex | 2 +- .../controllers/ticket_tier_controller.ex | 16 ++-- backend/lib/peach_web/router.ex | 1 + .../ticket_tier_controller_test.exs | 75 +++++++++++++++++++ 5 files changed, 83 insertions(+), 13 deletions(-) diff --git a/backend/lib/peach/ticket_tier.ex b/backend/lib/peach/ticket_tier.ex index 4988cf5..0a94159 100644 --- a/backend/lib/peach/ticket_tier.ex +++ b/backend/lib/peach/ticket_tier.ex @@ -5,7 +5,7 @@ defmodule Peach.TicketTier do use Ecto.Schema import Ecto.Changeset - @derive Jason.Encoder + @derive {Jason.Encoder, only: [:id, :name, :description, :max_supply]} schema "ticket_tiers" do field :name, :string field :description, :string diff --git a/backend/lib/peach/ticket_tiers.ex b/backend/lib/peach/ticket_tiers.ex index 4a7fa52..2230fd1 100644 --- a/backend/lib/peach/ticket_tiers.ex +++ b/backend/lib/peach/ticket_tiers.ex @@ -2,8 +2,8 @@ defmodule Peach.TicketTiers do @moduledoc """ Manages the events for the peach app """ - alias Peach.TicketTier alias Peach.Repo + alias Peach.TicketTier import Ecto.Query diff --git a/backend/lib/peach_web/controllers/ticket_tier_controller.ex b/backend/lib/peach_web/controllers/ticket_tier_controller.ex index ca55aa8..6cb60f0 100644 --- a/backend/lib/peach_web/controllers/ticket_tier_controller.ex +++ b/backend/lib/peach_web/controllers/ticket_tier_controller.ex @@ -2,17 +2,11 @@ defmodule PeachWeb.TicketTierController do use PeachWeb, :controller alias Peach.TicketTiers - def index(conn, %{"event_id" => event_id}) do - case TicketTiers.event_ticket_tiers(event_id) do - {:ok, ticket_tiers} -> - conn - |> put_status(:ok) - |> json(%{ticket_tiers: ticket_tiers}) + def index(conn, %{"id" => event_id}) do + ticket_tiers = TicketTiers.event_ticket_tiers(event_id) - {:error, error} -> - conn - |> put_status(:unprocessable_entity) - |> json(%{errors: error}) - end + conn + |> put_status(:ok) + |> json(%{ticket_tiers: ticket_tiers}) end end diff --git a/backend/lib/peach_web/router.ex b/backend/lib/peach_web/router.ex index ca85aa9..0df3120 100644 --- a/backend/lib/peach_web/router.ex +++ b/backend/lib/peach_web/router.ex @@ -11,6 +11,7 @@ defmodule PeachWeb.Router do patch "/events/:id", EventController, :update get "/events", EventController, :index get "/tickets/:address", TicketController, :index + get "/events/:id/ticket_tiers", TicketTierController, :index end # Enable LiveDashboard and Swoosh mailbox preview in development 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 e69de29..6ae93fc 100644 --- a/backend/test/peach_web/controllers/ticket_tier_controller_test.exs +++ b/backend/test/peach_web/controllers/ticket_tier_controller_test.exs @@ -0,0 +1,75 @@ +defmodule PeachWeb.TicketTierControllerTest do + use PeachWeb.ConnCase, async: true + alias Peach.{Event, Repo, TicketTier} + + setup do + # Insert a sample event and ticket tiers for testing + event = + Repo.insert!(%Event{ + name: "Sample Event", + start: ~N[2024-11-10 09:00:00], + end: ~N[2024-11-12 17:00:00] + }) + + vip_tier = + Repo.insert!(%TicketTier{ + name: "VIP", + description: "Access to VIP areas", + max_supply: 50, + event_id: event.id + }) + + standard_tier = + Repo.insert!(%TicketTier{ + name: "Standard", + description: "General admission", + max_supply: 200, + event_id: event.id + }) + + # Pass event and ticket tiers to each test case + {:ok, event: event, vip_tier: vip_tier, standard_tier: standard_tier} + end + + test "returns ticket tiers for a valid event_id", %{ + conn: conn, + event: event, + vip_tier: vip_tier, + standard_tier: standard_tier + } do + # Send the GET request with a valid event_id + conn = get(conn, "/api/events/#{event.id}/ticket_tiers") + + # Expected response structure + expected_response = %{ + "ticket_tiers" => [ + %{ + "id" => vip_tier.id, + "name" => vip_tier.name, + "description" => vip_tier.description, + "max_supply" => vip_tier.max_supply + }, + %{ + "id" => standard_tier.id, + "name" => standard_tier.name, + "description" => standard_tier.description, + "max_supply" => standard_tier.max_supply + } + ] + } + + # Assert the response status and structure + assert json_response(conn, 200) == expected_response + end + + test "returns error for invalid event_id", %{conn: conn} do + # Send the GET request with an invalid event_id + conn = get(conn, "/api/events/9999/ticket_tiers") + + # Expected error response + expected_error = %{"errors" => "Event not found"} + + # Assert the response status and error message + assert json_response(conn, 200) == %{"ticket_tiers" => []} + end +end