Skip to content

Commit

Permalink
feat(backend): remaining tickets for event
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLucqs committed Oct 18, 2024
1 parent 7380a18 commit 4f13a8f
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 1 deletion.
8 changes: 8 additions & 0 deletions backend/lib/peach/events.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Peach.Events do
"""
alias Peach.Event
alias Peach.Repo
alias Peach.TicketTiers
import Ecto.Query

@default_limit 50
Expand All @@ -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`
"""
Expand Down
16 changes: 15 additions & 1 deletion backend/lib/peach_web/controllers/event_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
1 change: 1 addition & 0 deletions backend/lib/peach_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4f13a8f

Please sign in to comment.