Skip to content

Commit

Permalink
Run mix format
Browse files Browse the repository at this point in the history
slashdotdash committed May 25, 2020
1 parent bb68e82 commit 8721f72
Showing 65 changed files with 794 additions and 681 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: elixir

elixir:
- 1.9.4
- 1.10.3

otp_release:
- 22.0
@@ -10,6 +10,7 @@ services:
- postgresql

before_script:
- mix format --check-formatted
- MIX_ENV=test mix do event_store.create, event_store.init
- MIX_ENV=test mix do ecto.create, ecto.migrate

39 changes: 25 additions & 14 deletions lib/conduit/accounts/aggregates/user.ex
Original file line number Diff line number Diff line change
@@ -3,19 +3,21 @@ defmodule Conduit.Accounts.Aggregates.User do
:uuid,
:username,
:email,
:hashed_password,
:hashed_password
]

alias Conduit.Accounts.Aggregates.User

alias Conduit.Accounts.Commands.{
RegisterUser,
UpdateUser,
UpdateUser
}

alias Conduit.Accounts.Events.{
UserEmailChanged,
UsernameChanged,
UserPasswordChanged,
UserRegistered,
UserRegistered
}

@doc """
@@ -26,15 +28,16 @@ defmodule Conduit.Accounts.Aggregates.User do
user_uuid: register.user_uuid,
username: register.username,
email: register.email,
hashed_password: register.hashed_password,
hashed_password: register.hashed_password
}
end

@doc """
Update a user's username, email, and password
"""
def execute(%User{} = user, %UpdateUser{} = update) do
Enum.reduce([&username_changed/2, &email_changed/2, &password_changed/2], [], fn (change, events) ->
Enum.reduce([&username_changed/2, &email_changed/2, &password_changed/2], [], fn change,
events ->
case change.(user, update) do
nil -> events
event -> [event | events]
@@ -45,11 +48,12 @@ defmodule Conduit.Accounts.Aggregates.User do
# state mutators

def apply(%User{} = user, %UserRegistered{} = registered) do
%User{user |
uuid: registered.user_uuid,
username: registered.username,
email: registered.email,
hashed_password: registered.hashed_password,
%User{
user
| uuid: registered.user_uuid,
username: registered.username,
email: registered.email,
hashed_password: registered.hashed_password
}
end

@@ -69,28 +73,35 @@ defmodule Conduit.Accounts.Aggregates.User do

defp username_changed(%User{}, %UpdateUser{username: ""}), do: nil
defp username_changed(%User{username: username}, %UpdateUser{username: username}), do: nil

defp username_changed(%User{uuid: user_uuid}, %UpdateUser{username: username}) do
%UsernameChanged{
user_uuid: user_uuid,
username: username,
username: username
}
end

defp email_changed(%User{}, %UpdateUser{email: ""}), do: nil
defp email_changed(%User{email: email}, %UpdateUser{email: email}), do: nil

defp email_changed(%User{uuid: user_uuid}, %UpdateUser{email: email}) do
%UserEmailChanged{
user_uuid: user_uuid,
email: email,
email: email
}
end

defp password_changed(%User{}, %UpdateUser{hashed_password: ""}), do: nil
defp password_changed(%User{hashed_password: hashed_password}, %UpdateUser{hashed_password: hashed_password}), do: nil

defp password_changed(%User{hashed_password: hashed_password}, %UpdateUser{
hashed_password: hashed_password
}),
do: nil

defp password_changed(%User{uuid: user_uuid}, %UpdateUser{hashed_password: hashed_password}) do
%UserPasswordChanged{
user_uuid: user_uuid,
hashed_password: hashed_password,
hashed_password: hashed_password
}
end
end
41 changes: 20 additions & 21 deletions lib/conduit/accounts/commands/register_user.ex
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
defmodule Conduit.Accounts.Commands.RegisterUser do
defstruct [
user_uuid: "",
username: "",
email: "",
password: "",
hashed_password: "",
]
defstruct user_uuid: "",
username: "",
email: "",
password: "",
hashed_password: ""

use ExConstructor
use Vex.Struct

alias Conduit.Accounts.Commands.RegisterUser
alias Conduit.Accounts.Validators.{UniqueEmail,UniqueUsername}
alias Conduit.Accounts.Validators.{UniqueEmail, UniqueUsername}
alias Conduit.Auth

validates :user_uuid, uuid: true
validates(:user_uuid, uuid: true)

validates :username,
validates(:username,
presence: [message: "can't be empty"],
format: [with: ~r/^[a-z0-9]+$/, allow_nil: true, allow_blank: true, message: "is invalid"],
string: true,
by: &UniqueUsername.validate/2
)

validates :email,
validates(:email,
presence: [message: "can't be empty"],
format: [with: ~r/\S+@\S+\.\S+/, allow_nil: true, allow_blank: true, message: "is invalid"],
string: true,
by: &UniqueEmail.validate/2
)

validates :hashed_password, presence: [message: "can't be empty"], string: true
validates(:hashed_password, presence: [message: "can't be empty"], string: true)

@doc """
Assign a unique identity for the user
@@ -55,16 +55,15 @@ defmodule Conduit.Accounts.Commands.RegisterUser do
Hash the password, clear the original password
"""
def hash_password(%RegisterUser{password: password} = register_user) do
%RegisterUser{register_user |
password: nil,
hashed_password: Auth.hash_password(password),
}
%RegisterUser{register_user | password: nil, hashed_password: Auth.hash_password(password)}
end
end

defimpl Conduit.Support.Middleware.Uniqueness.UniqueFields, for: Conduit.Accounts.Commands.RegisterUser do
def unique(%Conduit.Accounts.Commands.RegisterUser{user_uuid: user_uuid}), do: [
{:email, "has already been taken", user_uuid},
{:username, "has already been taken", user_uuid},
]
defimpl Conduit.Support.Middleware.Uniqueness.UniqueFields,
for: Conduit.Accounts.Commands.RegisterUser do
def unique(%Conduit.Accounts.Commands.RegisterUser{user_uuid: user_uuid}),
do: [
{:email, "has already been taken", user_uuid},
{:username, "has already been taken", user_uuid}
]
end
42 changes: 21 additions & 21 deletions lib/conduit/accounts/commands/update_user.ex
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
defmodule Conduit.Accounts.Commands.UpdateUser do
defstruct [
user_uuid: "",
username: "",
email: "",
password: "",
hashed_password: "",
]
defstruct user_uuid: "",
username: "",
email: "",
password: "",
hashed_password: ""

use ExConstructor
use Vex.Struct

alias Conduit.Accounts.Commands.UpdateUser
alias Conduit.Accounts.Projections.User
alias Conduit.Accounts.Validators.{UniqueEmail,UniqueUsername}
alias Conduit.Accounts.Validators.{UniqueEmail, UniqueUsername}
alias Conduit.Auth

validates :user_uuid, uuid: true
validates(:user_uuid, uuid: true)

validates :username,
validates(:username,
presence: [message: "can't be empty"],
format: [with: ~r/^[a-z0-9]+$/, allow_nil: true, allow_blank: true, message: "is invalid"],
string: true,
by: &UniqueUsername.validate/2
)

validates :email,
validates(:email,
presence: [message: "can't be empty"],
format: [with: ~r/\S+@\S+\.\S+/, allow_nil: true, allow_blank: true, message: "is invalid"],
string: true,
by: &UniqueEmail.validate/2
)

validates :hashed_password, string: [allow_nil: true, allow_blank: true]
validates(:hashed_password, string: [allow_nil: true, allow_blank: true])

@doc """
Assign the user identity
@@ -56,17 +56,17 @@ defmodule Conduit.Accounts.Commands.UpdateUser do
Hash the password, clear the original password
"""
def hash_password(%UpdateUser{password: ""} = update_user), do: update_user

def hash_password(%UpdateUser{password: password} = update_user) do
%UpdateUser{update_user |
password: nil,
hashed_password: Auth.hash_password(password),
}
%UpdateUser{update_user | password: nil, hashed_password: Auth.hash_password(password)}
end
end

defimpl Conduit.Support.Middleware.Uniqueness.UniqueFields, for: Conduit.Accounts.Commands.UpdateUser do
def unique(%Conduit.Accounts.Commands.UpdateUser{user_uuid: user_uuid}), do: [
{:email, "has already been taken", user_uuid},
{:username, "has already been taken", user_uuid},
]
defimpl Conduit.Support.Middleware.Uniqueness.UniqueFields,
for: Conduit.Accounts.Commands.UpdateUser do
def unique(%Conduit.Accounts.Commands.UpdateUser{user_uuid: user_uuid}),
do: [
{:email, "has already been taken", user_uuid},
{:username, "has already been taken", user_uuid}
]
end
2 changes: 1 addition & 1 deletion lib/conduit/accounts/events/user_email_changed.ex
Original file line number Diff line number Diff line change
@@ -2,6 +2,6 @@ defmodule Conduit.Accounts.Events.UserEmailChanged do
@derive Jason.Encoder
defstruct [
:user_uuid,
:email,
:email
]
end
2 changes: 1 addition & 1 deletion lib/conduit/accounts/events/user_password_changed.ex
Original file line number Diff line number Diff line change
@@ -2,6 +2,6 @@ defmodule Conduit.Accounts.Events.UserPasswordChanged do
@derive Jason.Encoder
defstruct [
:user_uuid,
:hashed_password,
:hashed_password
]
end
2 changes: 1 addition & 1 deletion lib/conduit/accounts/events/user_registered.ex
Original file line number Diff line number Diff line change
@@ -4,6 +4,6 @@ defmodule Conduit.Accounts.Events.UserRegistered do
:user_uuid,
:username,
:email,
:hashed_password,
:hashed_password
]
end
2 changes: 1 addition & 1 deletion lib/conduit/accounts/events/username_changed.ex
Original file line number Diff line number Diff line change
@@ -2,6 +2,6 @@ defmodule Conduit.Accounts.Events.UsernameChanged do
@derive Jason.Encoder
defstruct [
:user_uuid,
:username,
:username
]
end
6 changes: 3 additions & 3 deletions lib/conduit/accounts/projections/user.ex
Original file line number Diff line number Diff line change
@@ -5,9 +5,9 @@ defmodule Conduit.Accounts.Projections.User do
@timestamps_opts [type: :utc_datetime_usec]

schema "accounts_users" do
field :username, :string, unique: true
field :email, :string, unique: true
field :hashed_password, :string
field(:username, :string, unique: true)
field(:email, :string, unique: true)
field(:hashed_password, :string)

timestamps()
end
5 changes: 3 additions & 2 deletions lib/conduit/accounts/queries/user_by_email.ex
Original file line number Diff line number Diff line change
@@ -4,7 +4,8 @@ defmodule Conduit.Accounts.Queries.UserByEmail do
alias Conduit.Accounts.Projections.User

def new(email) do
from u in User,
where: u.email == ^email
from(u in User,
where: u.email == ^email
)
end
end
5 changes: 3 additions & 2 deletions lib/conduit/accounts/queries/user_by_username.ex
Original file line number Diff line number Diff line change
@@ -4,7 +4,8 @@ defmodule Conduit.Accounts.Queries.UserByUsername do
alias Conduit.Accounts.Projections.User

def new(username) do
from u in User,
where: u.username == ^username
from(u in User,
where: u.username == ^username
)
end
end
2 changes: 1 addition & 1 deletion lib/conduit/auth/auth.ex
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ defmodule Conduit.Auth do
def authenticate(email, password) do
with {:ok, user} <- user_by_email(email) do
check_password(user, password)
end
end
end

def hash_password(password), do: Bcrypt.hashpwsalt(password)
Loading
Oops, something went wrong.

0 comments on commit 8721f72

Please sign in to comment.