diff --git a/backend/lib/peach/events.ex b/backend/lib/peach/events.ex index 8c6b44b..a2c884b 100644 --- a/backend/lib/peach/events.ex +++ b/backend/lib/peach/events.ex @@ -107,79 +107,13 @@ defmodule Peach.Events do end @doc """ - Updates the `name` field + Updates the event """ - def update_event_name(event_id, name) do - event = Repo.get!(Event, event_id) + def update_event(id, event) do + current_event = Repo.get!(Event, id) - event - |> Event.update_changeset(%{name: name}) - |> Repo.update() - end - - @doc """ - Updates the `description` field - """ - def update_event_description(event_id, description) do - event = Repo.get!(Event, event_id) - - event - |> Event.update_changeset(%{description: description}) - |> Repo.update() - end - - @doc """ - Updates the `end` field - """ - def update_event_end(event_id, end_date) do - event = Repo.get!(Event, event_id) - - event - |> Event.update_changeset(%{end: end_date}) - |> Repo.update() - end - - @doc """ - Updates the `start` field - """ - def update_event_start(event_id, start) do - event = Repo.get!(Event, event_id) - - event - |> Event.update_changeset(%{start: start}) - |> Repo.update() - end - - @doc """ - Updates the `location` field - """ - def update_event_location(event_id, location) do - event = Repo.get!(Event, event_id) - - event - |> Event.update_changeset(%{location: location}) - |> Repo.update() - end - - @doc """ - Updates the `cover` field - """ - def update_event_cover(event_id, cover) do - event = Repo.get!(Event, event_id) - - event - |> Event.update_changeset(%{cover: cover}) - |> Repo.update() - end - - @doc """ - Updates the `treasury` field - """ - def update_event_treasury(event_id, treasury) do - event = Repo.get!(Event, event_id) - - event - |> Event.update_changeset(%{treasury: treasury}) + current_event + |> Event.update_changeset(event) |> Repo.update() end end diff --git a/backend/lib/peach/ticket_tier.ex b/backend/lib/peach/ticket_tier.ex index 4988cf5..0a94159 100644 --- a/backend/lib/peach/ticket_tier.ex +++ b/backend/lib/peach/ticket_tier.ex @@ -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 diff --git a/backend/lib/peach/ticket_tiers.ex b/backend/lib/peach/ticket_tiers.ex new file mode 100644 index 0000000..2230fd1 --- /dev/null +++ b/backend/lib/peach/ticket_tiers.ex @@ -0,0 +1,12 @@ +defmodule Peach.TicketTiers do + @moduledoc """ + Manages the events for the peach app + """ + alias Peach.Repo + alias Peach.TicketTier + + import Ecto.Query + + def event_ticket_tiers(event_id), + do: Repo.all(from tt in TicketTier, where: tt.event_id == ^event_id) +end diff --git a/backend/lib/peach_web/controllers/event_controller.ex b/backend/lib/peach_web/controllers/event_controller.ex index 0b0ced3..6913c0e 100644 --- a/backend/lib/peach_web/controllers/event_controller.ex +++ b/backend/lib/peach_web/controllers/event_controller.ex @@ -21,8 +21,7 @@ defmodule PeachWeb.EventController do end end - def events(conn, params) do - # Fetch events and map them to desired structure + def index(conn, params) do case Events.get_events(params) do {:ok, events} -> conn @@ -50,134 +49,8 @@ defmodule PeachWeb.EventController do @doc """ Updates the name of an event. """ - def update_event_name(conn, %{"id" => id, "name" => name}) do - case Events.update_event_name(id, name) do - {:ok, _event} -> - conn - |> put_status(:no_content) - - {:error, changeset} -> - errors = - Ecto.Changeset.traverse_errors(changeset, fn {msg, _opts} -> - Phoenix.Naming.humanize(msg) - end) - - conn - |> put_status(:bad_request) - |> json(%{errors: errors}) - end - end - - @doc """ - Updates the starting datetime of an event. - """ - def update_event_start(conn, %{"id" => id, "start" => start}) do - case Events.update_event_start(id, start) do - {:ok, _event} -> - conn - |> put_status(:no_content) - - {:error, changeset} -> - errors = - Ecto.Changeset.traverse_errors(changeset, fn {msg, _opts} -> - Phoenix.Naming.humanize(msg) - end) - - conn - |> put_status(:bad_request) - |> json(%{errors: errors}) - end - end - - @doc """ - Updates the ending datetime of an event. - """ - def update_event_end(conn, %{"id" => id, "end" => end_date}) do - case Events.update_event_end(id, end_date) do - {:ok, _event} -> - conn - |> put_status(:no_content) - - {:error, changeset} -> - errors = - Ecto.Changeset.traverse_errors(changeset, fn {msg, _opts} -> - Phoenix.Naming.humanize(msg) - end) - - conn - |> put_status(:bad_request) - |> json(%{errors: errors}) - end - end - - @doc """ - Updates the description of an event. - """ - def update_event_description(conn, %{"id" => id, "description" => description}) do - case Events.update_event_description(id, description) do - {:ok, _event} -> - conn - |> put_status(:no_content) - - {:error, changeset} -> - errors = - Ecto.Changeset.traverse_errors(changeset, fn {msg, _opts} -> - Phoenix.Naming.humanize(msg) - end) - - conn - |> put_status(:bad_request) - |> json(%{errors: errors}) - end - end - - @doc """ - Updates the location of an event. - """ - def update_event_location(conn, %{"id" => id, "location" => location}) do - case Events.update_event_location(id, location) do - {:ok, _event} -> - conn - |> put_status(:no_content) - - {:error, changeset} -> - errors = - Ecto.Changeset.traverse_errors(changeset, fn {msg, _opts} -> - Phoenix.Naming.humanize(msg) - end) - - conn - |> put_status(:bad_request) - |> json(%{errors: errors}) - end - end - - @doc """ - Updates the cover of an event. - """ - def update_event_cover(conn, %{"id" => id, "cover" => cover}) do - case Events.update_event_cover(id, cover) do - {:ok, _event} -> - conn - |> put_status(:no_content) - - {:error, changeset} -> - errors = - Ecto.Changeset.traverse_errors(changeset, fn {msg, _opts} -> - Phoenix.Naming.humanize(msg) - end) - - conn - |> put_status(:bad_request) - |> json(%{errors: errors}) - end - end - - @doc """ - Updates the treasury of an event. - """ - def update_event_treasury(conn, %{"id" => id, "treasury" => treasury}) do - case Events.update_event_treasury(id, treasury) do + def update(conn, %{"id" => id, "event" => event_params}) do + case Events.update_event(id, event_params) do {:ok, _event} -> conn |> put_status(:no_content) diff --git a/backend/lib/peach_web/controllers/ticket_controller.ex b/backend/lib/peach_web/controllers/ticket_controller.ex index adfbbd2..2e31b29 100644 --- a/backend/lib/peach_web/controllers/ticket_controller.ex +++ b/backend/lib/peach_web/controllers/ticket_controller.ex @@ -2,7 +2,7 @@ defmodule PeachWeb.TicketController do use PeachWeb, :controller alias Peach.Tickets - def get_tickets_with_event_by_address(conn, %{"address" => address}) do + def index(conn, %{"address" => address}) do # Fetch the tickets with preloaded ticket_tier and event associations tickets = Tickets.list_tickets_with_event_by_owner(address) diff --git a/backend/lib/peach_web/controllers/ticket_tier_controller.ex b/backend/lib/peach_web/controllers/ticket_tier_controller.ex new file mode 100644 index 0000000..6cb60f0 --- /dev/null +++ b/backend/lib/peach_web/controllers/ticket_tier_controller.ex @@ -0,0 +1,12 @@ +defmodule PeachWeb.TicketTierController do + use PeachWeb, :controller + alias Peach.TicketTiers + + def index(conn, %{"id" => event_id}) do + ticket_tiers = TicketTiers.event_ticket_tiers(event_id) + + conn + |> put_status(:ok) + |> json(%{ticket_tiers: ticket_tiers}) + end +end diff --git a/backend/lib/peach_web/router.ex b/backend/lib/peach_web/router.ex index 883ac48..0df3120 100644 --- a/backend/lib/peach_web/router.ex +++ b/backend/lib/peach_web/router.ex @@ -8,15 +8,10 @@ defmodule PeachWeb.Router do scope "/api", PeachWeb do pipe_through(:api) post "/events/create", EventController, :create - patch "/events/:id/name", EventController, :update_event_name - patch "/events/:id/description", EventController, :update_event_description - patch "/events/:id/location", EventController, :update_event_location - patch "/events/:id/cover", EventController, :update_event_cover - patch "/events/:id/treasury", EventController, :update_event_treasury - patch "/events/:id/start", EventController, :update_event_start - patch "/events/:id/end", EventController, :update_event_end - get "/events", EventController, :events - get "/tickets/:address", TicketController, :get_tickets_with_event_by_address + 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 diff --git a/backend/test/peach/events/event_db_setters_test.exs b/backend/test/peach/events/event_db_setters_test.exs deleted file mode 100644 index 9eec683..0000000 --- a/backend/test/peach/events/event_db_setters_test.exs +++ /dev/null @@ -1,93 +0,0 @@ -defmodule Peach.Events.EventDBSettersTest do - use Peach.DataCase, async: true - - alias Peach.Event - alias Peach.Events - alias Peach.Repo - - setup do - # Create an initial event record for testing - {:ok, event} = - Repo.insert(%Event{ - name: "Original Name", - start: ~N[2024-01-01 10:00:00], - end: ~N[2024-01-01 15:00:00], - description: "Original description", - location: "Original location", - cover: "https://example.com/original_cover.jpg", - onchain: false, - treasury: "0xdead" - }) - - {:ok, event: event} - end - - test "updates name in the database", %{event: event} do - updated_name = "Updated Name" - Events.update_event_name(event.id, updated_name) - updated_event = Repo.get!(Event, event.id) - assert updated_event.name == updated_name - Events.update_event_name(event.id, 1) - updated_event = Repo.get!(Event, event.id) - assert updated_event.name == updated_name - end - - test "updates end date in the database", %{event: event} do - new_date = ~N[2025-12-31 23:59:59] - Events.update_event_end(event.id, new_date) - updated_event = Repo.get!(Event, event.id) - assert updated_event.end == new_date - Events.update_event_end(event.id, 1) - updated_event = Repo.get!(Event, event.id) - assert updated_event.end == new_date - end - - test "updates start date in the database", %{event: event} do - new_date = ~N[2024-01-01 11:00:00] - Events.update_event_start(event.id, new_date) - updated_event = Repo.get!(Event, event.id) - assert updated_event.start == new_date - Events.update_event_start(event.id, 1) - updated_event = Repo.get!(Event, event.id) - assert updated_event.start == new_date - end - - test "updates description in the database", %{event: event} do - updated_description = "Updated description" - Events.update_event_description(event.id, updated_description) - updated_event = Repo.get!(Event, event.id) - assert updated_event.description == updated_description - Events.update_event_description(event.id, 1) - updated_event = Repo.get!(Event, event.id) - assert updated_event.description == updated_description - end - - test "updates location in the database", %{event: event} do - updated_location = "Updated location" - Events.update_event_location(event.id, updated_location) - updated_event = Repo.get!(Event, event.id) - assert updated_event.location == updated_location - Events.update_event_location(event.id, 1) - updated_event = Repo.get!(Event, event.id) - assert updated_event.location == updated_location - end - - test "updates cover in the database", %{event: event} do - updated_cover = "https://example.com/updated_cover.jpg" - Events.update_event_cover(event.id, updated_cover) - updated_event = Repo.get!(Event, event.id) - assert updated_event.cover == updated_cover - Events.update_event_cover(event.id, 1) - updated_event = Repo.get!(Event, event.id) - assert updated_event.cover == updated_cover - end - - test "updates treasury in the database", %{event: event} do - updated_treasury = "0xbeef" - Events.update_event_treasury(event.id, updated_treasury) - updated_event = Repo.get!(Event, event.id) - assert updated_event.treasury == updated_treasury - Events.update_event_treasury(event.id, "beef") - assert updated_event.treasury == updated_treasury - end -end diff --git a/backend/test/peach_web/controllers/ticket_tier_controller_test.exs b/backend/test/peach_web/controllers/ticket_tier_controller_test.exs new file mode 100644 index 0000000..6ae93fc --- /dev/null +++ b/backend/test/peach_web/controllers/ticket_tier_controller_test.exs @@ -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 diff --git a/backend/test/peach_web/controllers/update_event_test.exs b/backend/test/peach_web/controllers/update_event_test.exs index 9a0eb82..af8eaab 100644 --- a/backend/test/peach_web/controllers/update_event_test.exs +++ b/backend/test/peach_web/controllers/update_event_test.exs @@ -37,7 +37,7 @@ defmodule PeachWeb.EventUpdateControllertest do expected_event = @original_event Enum.reduce(updated_fields, expected_event, fn {field, value}, acc -> - conn = patch(conn, "/api/events/1/#{field}", %{"#{field}" => value}) + conn = patch(conn, "/api/events/1", %{"event" => %{"#{field}" => value}}) acc = Map.replace(acc, String.to_atom(field), value) # Assert response status @@ -69,7 +69,7 @@ defmodule PeachWeb.EventUpdateControllertest do expected_event = @original_event Enum.each(updated_fields, fn {field, value} -> - conn = patch(conn, "/api/events/1/#{field}", %{"#{field}" => value}) + conn = patch(conn, "/api/events/1", %{"event" => %{"#{field}" => value}}) # Assert response status assert conn.status == 400