feat(db): user
This commit is contained in:
parent
2142d7eddf
commit
3fd0565227
2 changed files with 44 additions and 0 deletions
31
lib/learning_phoenix/user.ex
Normal file
31
lib/learning_phoenix/user.ex
Normal file
|
@ -0,0 +1,31 @@
|
|||
defmodule LearningPhoenix.User do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
schema "users" do
|
||||
field :name, :string
|
||||
field :email, :string
|
||||
field :password, :string
|
||||
|
||||
timestamps(type: :utc_datetime)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def changeset(user, attrs) do
|
||||
user
|
||||
|> cast(attrs, [:name, :email, :password])
|
||||
|> validate_required([:name, :email, :password])
|
||||
|> validate_length(:name, min: 2)
|
||||
|> validate_length(:name, max: 30)
|
||||
|> valid_email(:email)
|
||||
end
|
||||
|
||||
def valid_email(changeset, field) do
|
||||
value = get_field(changeset, field)
|
||||
if value =~ "@" do
|
||||
changeset
|
||||
else
|
||||
add_error(changeset, field, "invalid email")
|
||||
end
|
||||
end
|
||||
end
|
13
priv/repo/migrations/20250812135200_create_users.exs
Normal file
13
priv/repo/migrations/20250812135200_create_users.exs
Normal file
|
@ -0,0 +1,13 @@
|
|||
defmodule LearningPhoenix.Repo.Migrations.CreateUsers do
|
||||
use Ecto.Migration
|
||||
|
||||
def change do
|
||||
create table(:users) do
|
||||
add :name, :string
|
||||
add :email, :string
|
||||
add :password, :string
|
||||
|
||||
timestamps(type: :utc_datetime)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue