Skip to content

Commit

Permalink
Merge pull request #6 from keep-starknet-strange/lucas/backend
Browse files Browse the repository at this point in the history
Lucas/backend
  • Loading branch information
0xLucqs authored Oct 17, 2024
2 parents c83064f + 5841d5d commit 72e2c44
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 307 deletions.
76 changes: 5 additions & 71 deletions backend/lib/peach/events.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
12 changes: 12 additions & 0 deletions backend/lib/peach/ticket_tiers.ex
Original file line number Diff line number Diff line change
@@ -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
133 changes: 3 additions & 130 deletions backend/lib/peach_web/controllers/event_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion backend/lib/peach_web/controllers/ticket_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
12 changes: 12 additions & 0 deletions backend/lib/peach_web/controllers/ticket_tier_controller.ex
Original file line number Diff line number Diff line change
@@ -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
13 changes: 4 additions & 9 deletions backend/lib/peach_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
93 changes: 0 additions & 93 deletions backend/test/peach/events/event_db_setters_test.exs

This file was deleted.

Loading

0 comments on commit 72e2c44

Please sign in to comment.