-
-
Save shankardevy/e9febbe754b0b54ff1b0e08e22aa9330 to your computer and use it in GitHub Desktop.
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
defmodule Mango.CRM.Customer do | |
use Ecto.Schema | |
import Ecto.Changeset | |
import Comeonin.Bcrypt, only: [hashpwsalt: 1] | |
alias Mango.CRM.Customer | |
schema "customers" do | |
field :email, :string | |
field :name, :string | |
field :password, :string, virtual: true | |
field :password_hash, :string | |
field :phone, :string | |
field :residence_area, :string | |
timestamps() | |
end | |
@doc false | |
def changeset(%Customer{} = customer, attrs) do | |
customer | |
|> cast(attrs, [:name, :email, :residence_area, :password, :phone]) | |
|> validate_required([:name, :email, :residence_area, :password]) | |
|> validate_format(:email, ~r/@/, message: "is invalid") | |
|> validate_length(:password, min: 6, max: 100) | |
|> unique_constraint(:email) | |
|> put_hashed_password | |
end | |
defp put_hashed_password(changeset) do | |
case changeset.valid? do | |
true -> | |
changes = changeset.changes | |
put_change(changeset, :password_hash, hashpwsalt(changes.password)) | |
_ -> | |
changeset | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment