-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7756e0f
commit f3be358
Showing
4 changed files
with
112 additions
and
1 deletion.
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
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,30 @@ | ||
defmodule Safira.Contest.BadgeRedeem do | ||
@moduledoc """ | ||
Event badge redeem. | ||
""" | ||
|
||
use Safira.Schema | ||
|
||
alias Safira.Accounts.{Attendee, Staff} | ||
alias Safira.Contest.Badge | ||
|
||
@required_fields ~w(badge_id attendee_id)a | ||
@optional_fields ~w(redeemed_by_id)a | ||
|
||
schema "badge_redeems" do | ||
belongs_to :badge, Badge | ||
|
||
belongs_to :attendee, Attendee | ||
belongs_to :redeemed_by, Staff | ||
|
||
timestamps(type: :utc_datetime) | ||
end | ||
|
||
@doc false | ||
def changeset(badge_redeem, attrs) do | ||
badge_redeem | ||
|> cast(attrs, @required_fields ++ @optional_fields) | ||
|> validate_required(@required_fields) | ||
|> unique_constraint([:badge_id, :attendee_id]) | ||
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,20 @@ | ||
defmodule Safira.Repo.Migrations.AddBadgeRedeems do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:badge_redeems, primary_key: false) do | ||
add :id, :binary_id, primary_key: true | ||
|
||
add :badge_id, references(:badges, on_delete: :nothing, type: :binary_id), null: false | ||
add :attendee_id, references(:attendees, on_delete: :delete_all, type: :binary_id), null: false | ||
add :redeemed_by_id, references(:staffs, on_delete: :nilify_all, type: :binary_id), null: true | ||
|
||
timestamps() | ||
end | ||
|
||
create unique_index(:badge_redeems, [:attendee_id, :badge_id]) | ||
create index(:badge_redeems, [:attendee_id]) | ||
create index(:badge_redeems, [:badge_id]) | ||
end | ||
|
||
end |