Skip to content

Commit

Permalink
feat(backend): ticket tiers from event id
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLucqs committed Oct 17, 2024
1 parent dc4d515 commit 5841d5d
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 13 deletions.
2 changes: 1 addition & 1 deletion backend/lib/peach/ticket_tier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion backend/lib/peach/ticket_tiers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 5 additions & 11 deletions backend/lib/peach_web/controllers/ticket_tier_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions backend/lib/peach_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
75 changes: 75 additions & 0 deletions backend/test/peach_web/controllers/ticket_tier_controller_test.exs
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5841d5d

Please sign in to comment.