-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(users): created users table migration and Ecto.Schema
- Loading branch information
1 parent
811acc3
commit 17a62d4
Showing
5 changed files
with
92 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
defmodule BookMyGigs.Users.Storage.User do | ||
@moduledoc """ | ||
The Ecto Schema for a User | ||
""" | ||
|
||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
alias BookMyGigs.Accounts.Storage.Account | ||
alias BookMyGigs.Utils.DateUtils | ||
|
||
@schema_prefix "public" | ||
@primary_key {:id, Ecto.UUID, autogenerate: :true} | ||
schema "users" do | ||
field(:username, :string) | ||
field(:first_name, :string) | ||
field(:last_name, :string) | ||
field(:birthday, :date) | ||
|
||
belongs_to(:account, Account, type: Ecto.UUID) | ||
|
||
timestamps(type: :utc_datetime) | ||
end | ||
|
||
def changeset(user, attrs) do | ||
user | ||
|> cast(attrs, [:username, :first_name, :last_name, :birthday, :account_id]) | ||
|> validate_required([:username, :account_id]) | ||
|> validate_length(:username, min: 6, max: 20) | ||
|> validate_format(:birthday, ~r/^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/\d{4}$/, message: "must be in the format DD/MM/YYYY") | ||
|> validate_birthday() | ||
|> unique_constraint(:username) | ||
|> unique_constraint(:account_id) | ||
end | ||
|
||
defp validate_birthday(changeset) do | ||
case get_field(changeset, :birthday) do | ||
nil -> changeset | ||
birthday_string -> | ||
case DateUtils.parse_date(birthday_string) do | ||
{:ok, date} -> | ||
if Date.compare(date, Date.utc_today()) == :lt do | ||
put_change(changeset, :birthday, date) | ||
else | ||
add_error(changeset, :birthday, "must be in the past") | ||
end | ||
{:error, _} -> add_error(changeset, :birthday, "is invalid, must be in the format 'DD/MM/YYYY") | ||
end | ||
end | ||
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,13 @@ | ||
defmodule BookMyGigs.Utils.DateUtils do | ||
Check warning on line 1 in lib/book_my_gigs/utils/date_utils.ex GitHub Actions / build
|
||
|
||
def parse_date(date_string) do | ||
with [day, month, year] <- String.split(date_string, "/"), | ||
{day, ""} <- Integer.parse(day), | ||
{month, ""} <- Integer.parse(month), | ||
{year, ""} <- Integer.parse(year) do | ||
Date.new(year, month, day) | ||
else | ||
_ -> :error | ||
end | ||
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
19 changes: 19 additions & 0 deletions
19
priv/repo/migrations/20241010122748_create_users_table.exs
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,19 @@ | ||
defmodule BookMyGigs.Repo.Migrations.CreateUsersTable do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:users, primary_key: false, prefix: "public") do | ||
add(:id, :uuid, primary_key: true) | ||
add(:account_id, references(:accounts, type: :uuid, on_delete: :delete_all), null: false) | ||
add(:username, :string) | ||
add(:first_name, :string) | ||
add(:last_name, :string) | ||
add(:birthday, :date) | ||
|
||
timestamps(type: :utc_datetime) | ||
end | ||
|
||
create unique_index(:users, [:account_id]) | ||
create unique_index(:users, [:username]) | ||
end | ||
end |