From 4f13a8fc35c67729591dc39397b31e9c2657bb9d Mon Sep 17 00:00:00 2001 From: LucasLvy Date: Fri, 18 Oct 2024 11:35:34 +0200 Subject: [PATCH] feat(backend): remaining tickets for event --- backend/lib/peach/events.ex | 8 ++ .../peach_web/controllers/event_controller.ex | 16 +++- backend/lib/peach_web/router.ex | 1 + .../remaining_tickets_events_test.exs | 77 +++++++++++++++++++ 4 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 backend/test/peach_web/controllers/remaining_tickets_events_test.exs diff --git a/backend/lib/peach/events.ex b/backend/lib/peach/events.ex index a2c884b..f665645 100644 --- a/backend/lib/peach/events.ex +++ b/backend/lib/peach/events.ex @@ -4,6 +4,7 @@ defmodule Peach.Events do """ alias Peach.Event alias Peach.Repo + alias Peach.TicketTiers import Ecto.Query @default_limit 50 @@ -18,6 +19,13 @@ defmodule Peach.Events do |> Repo.insert() end + def remaining_event_tickets(event_id) do + Enum.map(TicketTiers.event_ticket_tiers(event_id), fn ticket_tier -> + {:ok, remaining} = TicketTiers.remaining_tickets(ticket_tier.id) + remaining + end) + end + @doc """ Returns the `first` events that end after `after_time` and their id is after `after_event_id` """ diff --git a/backend/lib/peach_web/controllers/event_controller.ex b/backend/lib/peach_web/controllers/event_controller.ex index 1f5a4d3..120d7d1 100644 --- a/backend/lib/peach_web/controllers/event_controller.ex +++ b/backend/lib/peach_web/controllers/event_controller.ex @@ -36,7 +36,7 @@ defmodule PeachWeb.EventController do end @doc """ - Updates the name of an event. + Updates an event. """ def update(conn, %{"id" => id, "event" => event_params}) do case Events.update_event(id, event_params) do @@ -55,4 +55,18 @@ defmodule PeachWeb.EventController do |> json(%{errors: errors}) end end + + def remaining_event_tickets(conn, %{"id" => id}) do + case Events.remaining_event_tickets(id) do + [] -> + conn + |> put_status(:not_found) + |> json(%{errors: "Event not found"}) + ticket_tier -> + conn + |> put_status(:ok) + |> json(%{tickets: ticket_tier}) + + end + end end diff --git a/backend/lib/peach_web/router.ex b/backend/lib/peach_web/router.ex index 86ced37..8e27912 100644 --- a/backend/lib/peach_web/router.ex +++ b/backend/lib/peach_web/router.ex @@ -13,6 +13,7 @@ defmodule PeachWeb.Router do get "/tickets/:address", TicketController, :index get "/events/:id/ticket_tiers", TicketTierController, :index get "/ticket_tiers/:id", TicketTierController, :show + get "events/:id/remaining_tickets", EventController, :remaining_event_tickets end # Enable LiveDashboard and Swoosh mailbox preview in development diff --git a/backend/test/peach_web/controllers/remaining_tickets_events_test.exs b/backend/test/peach_web/controllers/remaining_tickets_events_test.exs new file mode 100644 index 0000000..f5b7562 --- /dev/null +++ b/backend/test/peach_web/controllers/remaining_tickets_events_test.exs @@ -0,0 +1,77 @@ +defmodule PeachWeb.EventControllerTest do + use PeachWeb.ConnCase, async: true + alias Peach.{Event, Repo, Ticket, TicketTier} + + setup do + 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 + }) + + # 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}) + + # Pass the event to each test case + {:ok, event: event, vip_tier: vip_tier, standard_tier: standard_tier} + end + + test "returns remaining tickets for a valid event id", %{ + conn: conn, + vip_tier: vip_tier, + standard_tier: standard_tier, + event: event + } do + # Call the remaining_event_tickets endpoint with a valid event ID + conn = get(conn, "/api/events/#{event.id}/remaining_tickets") + + expected_response = %{ + "tickets" => [ + %{ + "id" => vip_tier.id, + "name" => vip_tier.name, + "description" => vip_tier.description, + "max_supply" => vip_tier.max_supply, + "remaining" => 48 + }, + %{ + "id" => standard_tier.id, + "name" => standard_tier.name, + "description" => standard_tier.description, + "max_supply" => standard_tier.max_supply, + "remaining" => standard_tier.max_supply + } + ] + } + + # Assert the response status and content + assert json_response(conn, 200) == expected_response + end + + test "returns error for an invalid event id", %{conn: conn} do + # Call the remaining_event_tickets endpoint with an invalid event ID + conn = get(conn, "/api/events/9999/remaining_tickets") + + # Assert the response status and error message + assert json_response(conn, 404) == %{"errors" => "Event not found"} + end +end