Skip to content

Commit

Permalink
add migration for user
Browse files Browse the repository at this point in the history
  • Loading branch information
raulpe7eira committed Feb 24, 2021
1 parent 7127bff commit 13beb97
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 35 deletions.
5 changes: 5 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ config :rp7pay, Rp7payWeb.Endpoint,
pubsub_server: Rp7pay.PubSub,
live_view: [signing_salt: "RcTrGm5N"]

# Configures repository
config :rp7pay, Rp7pay.Repo,
migration_primary_key: [type: :binary_id],
migration_foreign_key: [type: :binary_id]

# Configures Elixir's Logger
config :logger, :console,
format: "$time $metadata[$level] $message\n",
Expand Down
8 changes: 2 additions & 6 deletions lib/rp7pay.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
defmodule Rp7pay do
@moduledoc """
Rp7pay keeps the contexts that define your domain
and business logic.
alias Rp7pay.Users.Create, as: UserCreate

Contexts are also responsible for managing your data, regardless
if it comes from the database, an external API or others.
"""
defdelegate create_user(params), to: UserCreate, as: :call
end
38 changes: 38 additions & 0 deletions lib/rp7pay/user.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
defmodule Rp7pay.User do
use Ecto.Schema
import Ecto.Changeset

alias Ecto.Changeset

@primary_key {:id, :binary_id, autogenerate: true}

@required_params [:name, :age, :email, :password, :nickname]

schema "users" do
field :name, :string
field :age, :integer
field :email, :string
field :password, :string, virtual: true
field :password_hash, :string
field :nickname, :string

timestamps()
end

def changeset(params) do
%__MODULE__{}
|> cast(params, @required_params)
|> validate_required(@required_params)
|> validate_length(:password, min: 6)
|> validate_number(:age, greater_than_or_equal_to: 18)
|> validate_format(:email, ~r/@/)
|> unique_constraint([:email])
|> unique_constraint([:nickname])
|> put_password_hash
end

defp put_password_hash(%Changeset{valid?: true, changes: %{password: password}} = changeset) do
change(changeset, Bcrypt.add_hash(password))
end
defp put_password_hash(changeset), do: changeset
end
9 changes: 9 additions & 0 deletions lib/rp7pay/users/create.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Rp7pay.Users.Create do
alias Rp7pay.{Repo, User}

def call(params) do
params
|> User.changeset
|> Repo.insert
end
end
23 changes: 23 additions & 0 deletions lib/rp7pay_web/controllers/users_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule Rp7payWeb.UsersController do
use Rp7payWeb, :controller

alias Rp7pay.User

def create(conn, params) do
params
|> Rp7pay.create_user
|> handle_response(conn)
end

defp handle_response({:ok, %User{} = user}, conn) do
conn
|> put_status(:created)
|> render("create.json", user: user)
end
defp handle_response({:error, result}, conn) do
conn
|> put_status(:bad_request)
|> put_view(Rp7payWeb.ErrorView)
|> render("400.json", result: result)
end
end
2 changes: 2 additions & 0 deletions lib/rp7pay_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ defmodule Rp7payWeb.Router do
pipe_through :api

get "/:filename", WelcomeController, :index

post "/users", UsersController, :create
end

# Enables LiveDashboard only for development
Expand Down
14 changes: 14 additions & 0 deletions lib/rp7pay_web/views/users_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule Rp7payWeb.UsersView do
alias Rp7pay.User

def render("create.json", %{user: %User{id: id, name: name, nickname: nickname}}) do
%{
message: "User created",
user: %{
id: id,
name: name,
nickname: nickname
}
}
end
end
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ defmodule Rp7pay.MixProject do
{:gettext, "~> 0.11"},
{:jason, "~> 1.0"},
{:plug_cowboy, "~> 2.0"},
{:credo, "~> 1.5", only: [:dev, :test], runtime: false}
{:credo, "~> 1.5", only: [:dev, :test], runtime: false},
{:bcrypt_elixir, "~> 2.0"}
]
end

Expand Down
59 changes: 31 additions & 28 deletions mix.lock

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions priv/repo/migrations/20210223225727_create_user_table.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule Rp7pay.Repo.Migrations.CreateUserTable do
use Ecto.Migration

def change do
create table :users do
add :name, :string
add :age, :integer
add :email, :string
add :password_hash, :string
add :nickname, :string

timestamps()
end

create unique_index(:users, [:email])
create unique_index(:users, [:nickname])
end
end

0 comments on commit 13beb97

Please sign in to comment.