Skip to content

Commit

Permalink
feat(backend): event creation
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLucqs committed Oct 9, 2024
1 parent be447ca commit 4960326
Show file tree
Hide file tree
Showing 34 changed files with 253 additions and 106 deletions.
2 changes: 1 addition & 1 deletion backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ erl_crash.dump
/tmp/

# Ignore package tarball (built via "mix hex.build").
peach_backend-*.tar
peach-*.tar

2 changes: 1 addition & 1 deletion backend/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# PeachBackend
# Peach

To start your Phoenix server:

Expand Down
12 changes: 6 additions & 6 deletions backend/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
# General application configuration
import Config

config :peach_backend,
ecto_repos: [PeachBackend.Repo],
config :peach,
ecto_repos: [Peach.Repo],
generators: [timestamp_type: :utc_datetime]

# Configures the endpoint
config :peach_backend, PeachBackendWeb.Endpoint,
config :peach, PeachWeb.Endpoint,
url: [host: "localhost"],
adapter: Bandit.PhoenixAdapter,
render_errors: [
formats: [json: PeachBackendWeb.ErrorJSON],
formats: [json: PeachWeb.ErrorJSON],
layout: false
],
pubsub_server: PeachBackend.PubSub,
pubsub_server: Peach.PubSub,
live_view: [signing_salt: "41pzaInh"]

# Configures the mailer
Expand All @@ -29,7 +29,7 @@ config :peach_backend, PeachBackendWeb.Endpoint,
#
# For production it's recommended to configure a different adapter
# at the `config/runtime.exs`.
config :peach_backend, PeachBackend.Mailer, adapter: Swoosh.Adapters.Local
config :peach, Peach.Mailer, adapter: Swoosh.Adapters.Local

# Configures Elixir's Logger
config :logger, :console,
Expand Down
8 changes: 4 additions & 4 deletions backend/config/dev.exs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Config

# Configure your database
config :peach_backend, PeachBackend.Repo,
config :peach, Peach.Repo,
username: "postgres",
password: "postgres",
hostname: "localhost",
database: "peach_backend_dev",
database: "peach_dev",
stacktrace: true,
show_sensitive_data_on_connection_error: true,
pool_size: 10
Expand All @@ -16,7 +16,7 @@ config :peach_backend, PeachBackend.Repo,
# The watchers configuration can be used to run external
# watchers to your application. For example, we can use it
# to bundle .js and .css sources.
config :peach_backend, PeachBackendWeb.Endpoint,
config :peach, PeachWeb.Endpoint,
# Binding to loopback ipv4 address prevents access from other machines.
# Change to `ip: {0, 0, 0, 0}` to allow access from other machines.
http: [ip: {127, 0, 0, 1}, port: 4000],
Expand Down Expand Up @@ -50,7 +50,7 @@ config :peach_backend, PeachBackendWeb.Endpoint,
# different ports.

# Enable dev routes for dashboard and mailbox
config :peach_backend, dev_routes: true
config :peach, dev_routes: true

# Do not include metadata nor timestamps in development logs
config :logger, :console, format: "[$level] $message\n"
Expand Down
2 changes: 1 addition & 1 deletion backend/config/prod.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Config

# Configures Swoosh API Client
config :swoosh, api_client: Swoosh.ApiClient.Finch, finch_name: PeachBackend.Finch
config :swoosh, api_client: Swoosh.ApiClient.Finch, finch_name: Peach.Finch

# Disable Swoosh Local Memory Storage
config :swoosh, local: false
Expand Down
16 changes: 8 additions & 8 deletions backend/config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import Config
# If you use `mix release`, you need to explicitly enable the server
# by passing the PHX_SERVER=true when you start it:
#
# PHX_SERVER=true bin/peach_backend start
# PHX_SERVER=true bin/peach start
#
# Alternatively, you can use `mix phx.gen.release` to generate a `bin/server`
# script that automatically sets the env var above.
if System.get_env("PHX_SERVER") do
config :peach_backend, PeachBackendWeb.Endpoint, server: true
config :peach, PeachWeb.Endpoint, server: true
end

if config_env() == :prod do
Expand All @@ -30,7 +30,7 @@ if config_env() == :prod do

maybe_ipv6 = if System.get_env("ECTO_IPV6") in ~w(true 1), do: [:inet6], else: []

config :peach_backend, PeachBackend.Repo,
config :peach, Peach.Repo,
# ssl: true,
url: database_url,
pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"),
Expand All @@ -51,9 +51,9 @@ if config_env() == :prod do
host = System.get_env("PHX_HOST") || "example.com"
port = String.to_integer(System.get_env("PORT") || "4000")

config :peach_backend, :dns_cluster_query, System.get_env("DNS_CLUSTER_QUERY")
config :peach, :dns_cluster_query, System.get_env("DNS_CLUSTER_QUERY")

config :peach_backend, PeachBackendWeb.Endpoint,
config :peach, PeachWeb.Endpoint,
url: [host: host, port: 443, scheme: "https"],
http: [
# Enable IPv6 and bind on all interfaces.
Expand All @@ -70,7 +70,7 @@ if config_env() == :prod do
# To get SSL working, you will need to add the `https` key
# to your endpoint configuration:
#
# config :peach_backend, PeachBackendWeb.Endpoint,
# config :peach, PeachWeb.Endpoint,
# https: [
# ...,
# port: 443,
Expand All @@ -92,7 +92,7 @@ if config_env() == :prod do
# We also recommend setting `force_ssl` in your config/prod.exs,
# ensuring no data is ever sent via http, always redirecting to https:
#
# config :peach_backend, PeachBackendWeb.Endpoint,
# config :peach, PeachWeb.Endpoint,
# force_ssl: [hsts: true]
#
# Check `Plug.SSL` for all available options in `force_ssl`.
Expand All @@ -103,7 +103,7 @@ if config_env() == :prod do
# Also, you may need to configure the Swoosh API client of your choice if you
# are not using SMTP. Here is an example of the configuration:
#
# config :peach_backend, PeachBackend.Mailer,
# config :peach, Peach.Mailer,
# adapter: Swoosh.Adapters.Mailgun,
# api_key: System.get_env("MAILGUN_API_KEY"),
# domain: System.get_env("MAILGUN_DOMAIN")
Expand Down
8 changes: 4 additions & 4 deletions backend/config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ import Config
# The MIX_TEST_PARTITION environment variable can be used
# to provide built-in test partitioning in CI environment.
# Run `mix help test` for more information.
config :peach_backend, PeachBackend.Repo,
config :peach, Peach.Repo,
username: "postgres",
password: "postgres",
hostname: "localhost",
database: "peach_backend_test#{System.get_env("MIX_TEST_PARTITION")}",
database: "peach_test#{System.get_env("MIX_TEST_PARTITION")}",
pool: Ecto.Adapters.SQL.Sandbox,
pool_size: System.schedulers_online() * 2

# We don't run a server during test. If one is required,
# you can enable the server option below.
config :peach_backend, PeachBackendWeb.Endpoint,
config :peach, PeachWeb.Endpoint,
http: [ip: {127, 0, 0, 1}, port: 4002],
secret_key_base: "CivUWocGsceVtejxam3MT7w51Q9/UYNArM8wNwrYadEaRWhP39NuT/O9uI5SMbRq",
server: false

# In test we don't send emails
config :peach_backend, PeachBackend.Mailer, adapter: Swoosh.Adapters.Test
config :peach, Peach.Mailer, adapter: Swoosh.Adapters.Test

# Disable swoosh api client as it is only required for production adapters
config :swoosh, :api_client, false
Expand Down
4 changes: 2 additions & 2 deletions backend/lib/venuela_backend.ex → backend/lib/peach.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule PeachBackend do
defmodule Peach do
@moduledoc """
PeachBackend keeps the contexts that define your domain
Peach keeps the contexts that define your domain
and business logic.
Contexts are also responsible for managing your data, regardless
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule PeachBackend.Application do
defmodule Peach.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false
Expand All @@ -8,29 +8,29 @@ defmodule PeachBackend.Application do
@impl true
def start(_type, _args) do
children = [
PeachBackendWeb.Telemetry,
PeachBackend.Repo,
{DNSCluster, query: Application.get_env(:peach_backend, :dns_cluster_query) || :ignore},
{Phoenix.PubSub, name: PeachBackend.PubSub},
PeachWeb.Telemetry,
Peach.Repo,
{DNSCluster, query: Application.get_env(:peach, :dns_cluster_query) || :ignore},
{Phoenix.PubSub, name: Peach.PubSub},
# Start the Finch HTTP client for sending emails
{Finch, name: PeachBackend.Finch},
# Start a worker by calling: PeachBackend.Worker.start_link(arg)
# {PeachBackend.Worker, arg},
{Finch, name: Peach.Finch},
# Start a worker by calling: Peach.Worker.start_link(arg)
# {Peach.Worker, arg},
# Start to serve requests, typically the last entry
PeachBackendWeb.Endpoint
PeachWeb.Endpoint
]

# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: PeachBackend.Supervisor]
opts = [strategy: :one_for_one, name: Peach.Supervisor]
Supervisor.start_link(children, opts)
end

# Tell Phoenix to update the endpoint configuration
# whenever the application is updated.
@impl true
def config_change(changed, _new, removed) do
PeachBackendWeb.Endpoint.config_change(changed, removed)
PeachWeb.Endpoint.config_change(changed, removed)
:ok
end
end
26 changes: 26 additions & 0 deletions backend/lib/peach/event.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule Peach.Event do
use Ecto.Schema
import Ecto.Changeset

@derive Jason.Encoder
schema "events" do
field :name, :string
field :date, :naive_datetime
field :description, :string
field :location, :string
field :cover, :string
field :onchain, :boolean, default: false

has_many :ticket_tiers, Peach.TicketTier

timestamps(type: :utc_datetime)
end

@doc false
def changeset(event, attrs) do
event
|> cast(attrs, [:name, :description, :location, :date, :cover])
|> cast_assoc(:ticket_tiers, with: &Peach.TicketTier.changeset/2)
|> validate_required([:name, :description, :location, :date, :cover])
end
end
14 changes: 14 additions & 0 deletions backend/lib/peach/events.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule Events do
alias Peach.Repo
alias Peach.Event
alias Peach.TicketTier

@doc """
Creates an event with the given attributes.
"""
def create_event(event \\ %{}) do
%Event{}
|> Event.changeset(event)
|> Repo.insert()
end
end
3 changes: 3 additions & 0 deletions backend/lib/peach/mailer.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule Peach.Mailer do
use Swoosh.Mailer, otp_app: :peach
end
5 changes: 5 additions & 0 deletions backend/lib/peach/repo.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule Peach.Repo do
use Ecto.Repo,
otp_app: :peach,
adapter: Ecto.Adapters.Postgres
end
20 changes: 20 additions & 0 deletions backend/lib/peach/ticket.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Peach.Ticket do
use Ecto.Schema
import Ecto.Changeset

schema "tickets" do
field :owner, :string
field :balance, :integer
field :tier_id, :id

timestamps(type: :utc_datetime)
end

@doc false
def changeset(ticket, attrs) do
ticket
|> cast(attrs, [:owner, :balance, :tier_id])
|> validate_required([:owner, :balance, :tier_id])
|> validate_length(:owner, max: 66)
end
end
22 changes: 22 additions & 0 deletions backend/lib/peach/ticket_tier.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
defmodule Peach.TicketTier do
use Ecto.Schema
import Ecto.Changeset

@derive Jason.Encoder
schema "ticket_tiers" do
field :name, :string
field :description, :string
field :max_supply, :integer

belongs_to :event, Peach.Event

timestamps(type: :utc_datetime)
end

@doc false
def changeset(ticket_tier, attrs) do
ticket_tier
|> cast(attrs, [:name, :description, :max_supply])
|> validate_required([:name, :description, :max_supply])
end
end
16 changes: 8 additions & 8 deletions backend/lib/venuela_backend_web.ex → backend/lib/peach_web.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
defmodule PeachBackendWeb do
defmodule PeachWeb do
@moduledoc """
The entrypoint for defining your web interface, such
as controllers, components, channels, and so on.
This can be used in your application as:
use PeachBackendWeb, :controller
use PeachBackendWeb, :html
use PeachWeb, :controller
use PeachWeb, :html
The definitions below will be executed for every controller,
component, etc, so keep them short and clean, focused
Expand Down Expand Up @@ -39,10 +39,10 @@ defmodule PeachBackendWeb do
quote do
use Phoenix.Controller,
formats: [:html, :json],
layouts: [html: PeachBackendWeb.Layouts]
layouts: [html: PeachWeb.Layouts]

import Plug.Conn
import PeachBackendWeb.Gettext
import PeachWeb.Gettext

unquote(verified_routes())
end
Expand All @@ -51,9 +51,9 @@ defmodule PeachBackendWeb do
def verified_routes do
quote do
use Phoenix.VerifiedRoutes,
endpoint: PeachBackendWeb.Endpoint,
router: PeachBackendWeb.Router,
statics: PeachBackendWeb.static_paths()
endpoint: PeachWeb.Endpoint,
router: PeachWeb.Router,
statics: PeachWeb.static_paths()
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule PeachBackendWeb.ErrorJSON do
defmodule PeachWeb.ErrorJSON do
@moduledoc """
This module is invoked by your endpoint in case of errors on JSON requests.
Expand Down
17 changes: 17 additions & 0 deletions backend/lib/peach_web/controllers/event_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule PeachWeb.EventController do
use PeachWeb, :controller

def create(conn, %{"event" => event_params}) do
case Events.create_event(event_params) do
{:ok, event} ->
conn
|> put_status(:created)
|> json(%{message: "Event created successfully", event: event.name})

{:error, changeset} ->
conn
|> put_status(:unprocessable_entity)
|> json(%{errors: changeset})
end
end
end
Loading

0 comments on commit 4960326

Please sign in to comment.