Skip to content

Commit

Permalink
feat(users): created users table migration and Ecto.Schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabrielparizet committed Oct 10, 2024
1 parent 811acc3 commit 17a62d4
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lib/book_my_gigs/accounts/Storage/account.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule BookMyGigs.Accounts.Storage.Account do
schema "accounts" do
field(:email, :string)
field(:password, :string)
has_one(:user, BookMyGigs.Users.Storage.User)

timestamps(type: :utc_datetime)
end
Expand All @@ -18,8 +19,13 @@ defmodule BookMyGigs.Accounts.Storage.Account do
account
|> cast(attrs, [:email, :password])
|> validate_required([:email, :password])
|> unique_constraint(:email)
|> validate_format(:email, ~r/@/)
|> validate_format(:email, ~r/^[^\s]+@[^\s]+\.[^\s]+$/, message: "must have the @ sign and no spaces")
|> validate_length(:email, max: 160)
|> validate_length(:password, min: 8)
|> validate_format(:password, ~r/[a-z]/, message: "must include at least one lowercase letter")
|> validate_format(:password, ~r/[A-Z]/, message: "must include at least one uppercase letter")
|> validate_format(:password, ~r/[0-9]/, message: "must include at least one number")
|> validate_format(:password, ~r/[?!.,@*€$\-_#:]/, message: "must include at least one special character (?!.,@*€$-_#:)")
|> unique_constraint(:email)
end
end
51 changes: 51 additions & 0 deletions lib/book_my_gigs/users/storage/user.ex
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

Check warning on line 46 in lib/book_my_gigs/users/storage/user.ex

View workflow job for this annotation

GitHub Actions / build

Function body is nested too deep (max depth is 2, was 3).
{:error, _} -> add_error(changeset, :birthday, "is invalid, must be in the format 'DD/MM/YYYY")
end
end
end
end
13 changes: 13 additions & 0 deletions lib/book_my_gigs/utils/date_utils.ex
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

View workflow job for this annotation

GitHub Actions / build

Modules should have a @moduledoc tag.

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
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"floki": {:hex, :floki, "0.36.2", "a7da0193538c93f937714a6704369711998a51a6164a222d710ebd54020aa7a3", [:mix], [], "hexpm", "a8766c0bc92f074e5cb36c4f9961982eda84c5d2b8e979ca67f5c268ec8ed580"},
"gettext": {:hex, :gettext, "0.24.0", "6f4d90ac5f3111673cbefc4ebee96fe5f37a114861ab8c7b7d5b30a1108ce6d8", [:mix], [{:expo, "~> 0.5.1", [hex: :expo, repo: "hexpm", optional: false]}], "hexpm", "bdf75cdfcbe9e4622dd18e034b227d77dd17f0f133853a1c73b97b3d6c770e8b"},
"guardian": {:hex, :guardian, "2.3.2", "78003504b987f2b189d76ccf9496ceaa6a454bb2763627702233f31eb7212881", [:mix], [{:jose, "~> 1.8", [hex: :jose, repo: "hexpm", optional: false]}, {:plug, "~> 1.3.3 or ~> 1.4", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "b189ff38cd46a22a8a824866a6867ca8722942347f13c33f7d23126af8821b52"},
"heroicons": {:git, "https://github.com/tailwindlabs/heroicons.git", "88ab3a0d790e6a47404cba02800a6b25d2afae50", [tag: "v2.1.1", sparse: "optimized", depth: 1]},
"heroicons": {:git, "https://github.com/tailwindlabs/heroicons.git", "88ab3a0d790e6a47404cba02800a6b25d2afae50", [tag: "v2.1.1", sparse: "optimized"]},
"hpax": {:hex, :hpax, "0.1.2", "09a75600d9d8bbd064cdd741f21fc06fc1f4cf3d0fcc335e5aa19be1a7235c84", [:mix], [], "hexpm", "2c87843d5a23f5f16748ebe77969880e29809580efdaccd615cd3bed628a8c13"},
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"jose": {:hex, :jose, "1.11.10", "a903f5227417bd2a08c8a00a0cbcc458118be84480955e8d251297a425723f83", [:mix, :rebar3], [], "hexpm", "0d6cd36ff8ba174db29148fc112b5842186b68a90ce9fc2b3ec3afe76593e614"},
Expand Down
19 changes: 19 additions & 0 deletions priv/repo/migrations/20241010122748_create_users_table.exs
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

0 comments on commit 17a62d4

Please sign in to comment.