Skip to content

Commit

Permalink
feat(backend): setters for each event field
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLucqs committed Oct 11, 2024
1 parent 9e00b8c commit 34f0e2f
Show file tree
Hide file tree
Showing 7 changed files with 373 additions and 8 deletions.
7 changes: 7 additions & 0 deletions backend/lib/peach/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,11 @@ defmodule Peach.Event do
|> validate_required([:name, :description, :location, :date, :cover, :treasury])
|> validate_format(:treasury, ~r/^0x[0-9a-fA-F]{1,64}$/)
end

def update_changeset(event, attrs) do
event
|> cast(attrs, [:name, :description, :location, :date, :cover, :treasury])
|> validate_required([:name, :description, :location, :date, :cover, :treasury])
|> validate_format(:treasury, ~r/^0x[0-9a-fA-F]{1,64}$/)
end
end
68 changes: 67 additions & 1 deletion backend/lib/peach/events.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Events do
defmodule Peach.Events do
@moduledoc """
Manages the events for the peach app
"""
Expand All @@ -13,4 +13,70 @@ defmodule Events do
|> Event.changeset(event)
|> Repo.insert()
end

@doc """
Updates the `name` field
"""
def update_event_name(event_id, name) do
event = Repo.get!(Event, event_id)

event
|> Event.update_changeset(%{name: name})
|> Repo.update()
end

@doc """
Updates the `date` field
"""
def update_event_date(event_id, date) do
event = Repo.get!(Event, event_id)

event
|> Event.update_changeset(%{date: date})
|> 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 `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})
|> Repo.update()
end
end
127 changes: 127 additions & 0 deletions backend/lib/peach_web/controllers/event_controller.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule PeachWeb.EventController do
use PeachWeb, :controller
alias Peach.Events

def create(conn, %{"event" => event_params}) do
case Events.create_event(event_params) do
Expand All @@ -19,4 +20,130 @@ defmodule PeachWeb.EventController do
|> json(%{errors: errors})
end
end

@doc """
Updates the name of an event.
"""
def update_event_name(conn, %{"id" => id, "name" => name}) do
case Peach.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 date of an event.
"""
def update_event_date(conn, %{"id" => id, "date" => date}) do
case Events.update_event_cover(id, date) do

Check warning on line 49 in backend/lib/peach_web/controllers/event_controller.ex

View check run for this annotation

Codecov / codecov/patch

backend/lib/peach_web/controllers/event_controller.ex#L49

Added line #L49 was not covered by tests
{:ok, _event} ->
conn
|> put_status(:no_content)

Check warning on line 52 in backend/lib/peach_web/controllers/event_controller.ex

View check run for this annotation

Codecov / codecov/patch

backend/lib/peach_web/controllers/event_controller.ex#L52

Added line #L52 was not covered by tests

{:error, changeset} ->
errors =

Check warning on line 55 in backend/lib/peach_web/controllers/event_controller.ex

View check run for this annotation

Codecov / codecov/patch

backend/lib/peach_web/controllers/event_controller.ex#L55

Added line #L55 was not covered by tests
Ecto.Changeset.traverse_errors(changeset, fn {msg, _opts} ->
Phoenix.Naming.humanize(msg)

Check warning on line 57 in backend/lib/peach_web/controllers/event_controller.ex

View check run for this annotation

Codecov / codecov/patch

backend/lib/peach_web/controllers/event_controller.ex#L57

Added line #L57 was not covered by tests
end)

conn
|> put_status(:bad_request)
|> json(%{errors: errors})

Check warning on line 62 in backend/lib/peach_web/controllers/event_controller.ex

View check run for this annotation

Codecov / codecov/patch

backend/lib/peach_web/controllers/event_controller.ex#L62

Added line #L62 was not covered by tests
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
{: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
end
7 changes: 6 additions & 1 deletion backend/lib/peach_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ defmodule PeachWeb.Router do

scope "/api", PeachWeb do
pipe_through(:api)
post "/create_event", EventController, :create
post "/events/create", EventController, :create
put "/events/:id/name", EventController, :update_event_name
put "/events/:id/description", EventController, :update_event_description
put "/events/:id/location", EventController, :update_event_location
put "/events/:id/cover", EventController, :update_event_cover
put "/events/:id/treasury", EventController, :update_event_treasury
end

# Enable LiveDashboard and Swoosh mailbox preview in development
Expand Down
82 changes: 82 additions & 0 deletions backend/test/peach/events/event_db_setters_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
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",
date: ~N[2024-01-01 10: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 date in the database", %{event: event} do
new_date = ~N[2025-12-31 23:59:59]
Events.update_event_date(event.id, new_date)
updated_event = Repo.get!(Event, event.id)
assert updated_event.date == new_date
Events.update_event_date(event.id, 1)
updated_event = Repo.get!(Event, event.id)
assert updated_event.date == 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
12 changes: 6 additions & 6 deletions backend/test/peach_web/controllers/create_event_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule PeachWeb.EventControllerTest do
defmodule PeachWeb.EventCreateControllerTest do
use PeachWeb.ConnCase, async: true

import Ecto.Query
Expand Down Expand Up @@ -29,7 +29,7 @@ defmodule PeachWeb.EventControllerTest do
}

test "creates an event with ticket tiers", %{conn: conn} do
conn = post(conn, "/api/create_event", %{"event" => @valid_event_attrs})
conn = post(conn, "/api/events/create", %{"event" => @valid_event_attrs})

# Assert response status
assert json_response(conn, 201)["message"] == "Event created successfully"
Expand Down Expand Up @@ -57,14 +57,14 @@ defmodule PeachWeb.EventControllerTest do
# Remove one required field at a time
invalid_attrs = Map.drop(@valid_event_attrs, [field])

conn = post(conn, "/api/create_event", %{"event" => invalid_attrs})
conn = post(conn, "/api/events/create", %{"event" => invalid_attrs})

assert json_response(conn, 422)["errors"][field] == ["Can't be blank"]
end)

# Test with an empty ticket tier list
empty_tiers = Map.replace(@valid_event_attrs, "ticket_tiers", [])
conn = post(conn, "/api/create_event", %{"event" => empty_tiers})
conn = post(conn, "/api/events/create", %{"event" => empty_tiers})

assert json_response(conn, 422)["errors"]["ticket_tiers"] == ["Can't be blank"]
end
Expand All @@ -85,7 +85,7 @@ defmodule PeachWeb.EventControllerTest do
Map.replace(@valid_event_attrs, field, true)
end

conn = post(conn, "/api/create_event", %{"event" => invalid_attrs})
conn = post(conn, "/api/events/create", %{"event" => invalid_attrs})

assert json_response(conn, 422)["errors"][field] == ["Is invalid"]
end)
Expand All @@ -94,7 +94,7 @@ defmodule PeachWeb.EventControllerTest do
invalid_address_format =
Map.replace(@valid_event_attrs, "treasury", "Some string that is not a starknet address")

conn = post(conn, "/api/create_event", %{"event" => invalid_address_format})
conn = post(conn, "/api/events/create", %{"event" => invalid_address_format})

assert json_response(conn, 422)["errors"]["treasury"] == ["Has invalid format"]
end
Expand Down
Loading

0 comments on commit 34f0e2f

Please sign in to comment.