Skip to content

Commit

Permalink
feat(backend): get remaining tickets
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLucqs committed Oct 17, 2024
1 parent 72e2c44 commit 7380a18
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 20 deletions.
3 changes: 2 additions & 1 deletion backend/lib/peach/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 26 additions & 2 deletions backend/lib/peach/ticket_tiers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 1 addition & 12 deletions backend/lib/peach_web/controllers/event_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
"""
Expand Down
14 changes: 14 additions & 0 deletions backend/lib/peach_web/controllers/ticket_tier_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions backend/lib/peach_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions backend/test/peach_web/controllers/get_events_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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}
Expand Down Expand Up @@ -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
Expand All @@ -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
}
]

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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

0 comments on commit 7380a18

Please sign in to comment.