-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(backend): return all the owner's tickets
- Loading branch information
Showing
7 changed files
with
172 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
defmodule Peach.Tickets do | ||
@moduledoc """ | ||
Manages the tickets for the peach app | ||
""" | ||
alias Peach.Repo | ||
alias Peach.Ticket | ||
import Ecto.Query | ||
|
||
def list_tickets_with_event_by_owner(owner_address) do | ||
Repo.all( | ||
from t in Ticket, | ||
where: t.owner == ^owner_address, | ||
join: tier in assoc(t, :ticket_tier), | ||
join: event in assoc(tier, :event), | ||
preload: [ticket_tier: {tier, event: event}] | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
defmodule PeachWeb.TicketController do | ||
use PeachWeb, :controller | ||
alias Peach.Tickets | ||
|
||
def get_tickets_with_event_by_address(conn, %{"address" => address}) do | ||
# Fetch the tickets with preloaded ticket_tier and event associations | ||
tickets = Tickets.list_tickets_with_event_by_owner(address) | ||
|
||
# Group tickets by event and then by tier_id within each event | ||
events_with_tickets = | ||
tickets | ||
|> Enum.group_by(fn ticket -> ticket.ticket_tier.event end) | ||
|> Enum.map(fn {event, tickets} -> | ||
# Group tickets by tier within each event | ||
tickets_by_tier = | ||
tickets | ||
|> Enum.group_by(fn ticket -> ticket.ticket_tier end) | ||
|> Enum.map(fn {tier, tickets} -> | ||
%{ | ||
"tier_id" => tier.id, | ||
"name" => tier.name, | ||
"description" => tier.description, | ||
"ticket_ids" => Enum.map(tickets, & &1.id) |> Enum.sort() | ||
} | ||
end) | ||
|
||
%{ | ||
"name" => event.name, | ||
"location" => event.location, | ||
"date" => event.date, | ||
"cover" => event.cover, | ||
"tickets" => tickets_by_tier | ||
} | ||
end) | ||
|
||
# Wrap the result in a top-level map with "events" key | ||
json(conn, %{events: events_with_tickets}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
backend/test/peach_web/controllers/ticket_controller_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
defmodule PeachWeb.TicketControllerTest do | ||
use PeachWeb.ConnCase, async: true | ||
alias Peach.{Event, Repo, Ticket, TicketTier} | ||
|
||
setup do | ||
# Create an event | ||
event = | ||
Repo.insert!(%Event{ | ||
name: "Blockchain Conference", | ||
date: ~N[2024-11-10 00:00:00], | ||
description: "A blockchain event", | ||
location: "San Francisco, CA", | ||
cover: "https://example.com/cover.jpg", | ||
treasury: "0x1234567890abcdef1234567890abcdef12345678" | ||
}) | ||
|
||
# Create ticket tiers associated with the event | ||
vip_tier = | ||
Repo.insert!(%TicketTier{ | ||
name: "VIP", | ||
description: "Access to VIP sessions", | ||
max_supply: 100, | ||
event_id: event.id | ||
}) | ||
|
||
standard_tier = | ||
Repo.insert!(%TicketTier{ | ||
name: "Standard", | ||
description: "General admission", | ||
max_supply: 200, | ||
event_id: event.id | ||
}) | ||
|
||
# Create tickets for the user address associated with the tiers | ||
vip_ticket = | ||
Repo.insert!(%Ticket{ | ||
owner: "0xdead", | ||
# Using tier_id from the association | ||
ticket_tier_id: vip_tier.id | ||
}) | ||
|
||
# Create tickets for the user address associated with the tiers | ||
vip_ticket = | ||
Repo.insert!(%Ticket{ | ||
owner: "0xdead", | ||
# Using tier_id from the association | ||
ticket_tier_id: vip_tier.id | ||
}) | ||
|
||
standard_ticket = | ||
Repo.insert!(%Ticket{ | ||
owner: "0xdead", | ||
ticket_tier_id: standard_tier.id | ||
}) | ||
|
||
# Make the created data available for all tests | ||
{:ok, event: event, vip_ticket: vip_ticket, standard_ticket: standard_ticket} | ||
end | ||
|
||
test "returns events with tickets grouped by event for a given owner", %{ | ||
conn: conn, | ||
vip_ticket: vip_ticket, | ||
standard_ticket: standard_ticket | ||
} do | ||
# Send the GET request to the endpoint | ||
conn = get(conn, "/api/tickets/0xdead") | ||
|
||
# Expected JSON structure | ||
expected_response = %{ | ||
"events" => [ | ||
%{ | ||
"name" => "Blockchain Conference", | ||
"location" => "San Francisco, CA", | ||
"date" => "2024-11-10T00:00:00", | ||
"cover" => "https://example.com/cover.jpg", | ||
"tickets" => [ | ||
%{ | ||
"tier_id" => vip_ticket.ticket_tier_id, | ||
"name" => "VIP", | ||
"description" => "Access to VIP sessions", | ||
"ticket_ids" => [1, 2] | ||
}, | ||
%{ | ||
"tier_id" => standard_ticket.ticket_tier_id, | ||
"name" => "Standard", | ||
"description" => "General admission", | ||
"ticket_ids" => [3] | ||
} | ||
] | ||
} | ||
] | ||
} | ||
|
||
# Assert that the response matches the expected JSON structure | ||
assert json_response(conn, 200) == expected_response | ||
end | ||
|
||
test "returns an empty events list if no tickets are found for the address", %{conn: conn} do | ||
# Send the GET request to the endpoint with an address that has no tickets | ||
conn = get(conn, "/api/tickets/0xnonexistentaddress") | ||
|
||
# Assert that the response is an empty list under "events" | ||
assert json_response(conn, 200) == %{"events" => []} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters