-
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.
- Loading branch information
1 parent
7127bff
commit 13beb97
Showing
10 changed files
with
144 additions
and
35 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 |
---|---|---|
@@ -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 |
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,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 |
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,9 @@ | ||
defmodule Rp7pay.Users.Create do | ||
alias Rp7pay.{Repo, User} | ||
|
||
def call(params) do | ||
params | ||
|> User.changeset | ||
|> Repo.insert | ||
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,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 |
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,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 |
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,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 |