-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from ImNotAVirus/refacto/elven_database
Refacto/elven database
- Loading branch information
Showing
16 changed files
with
1,653 additions
and
214 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
locals_without_parens = [ | ||
# Ecto | ||
from: 2, | ||
field: 1, | ||
field: 2, | ||
field: 3, | ||
timestamps: 1, | ||
belongs_to: 2, | ||
belongs_to: 3, | ||
has_one: 2, | ||
has_one: 3, | ||
has_many: 2, | ||
has_many: 3, | ||
many_to_many: 2, | ||
many_to_many: 3, | ||
embeds_one: 2, | ||
embeds_one: 3, | ||
embeds_one: 4, | ||
embeds_many: 2, | ||
embeds_many: 3, | ||
embeds_many: 4 | ||
] | ||
|
||
[ | ||
locals_without_parens: locals_without_parens | ||
] |
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
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
42 changes: 36 additions & 6 deletions
42
apps/elven_database/lib/elven_database/players/accounts.ex
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,27 +1,57 @@ | ||
defmodule ElvenDatabase.Players.Accounts do | ||
@moduledoc """ | ||
TODO: Documentation | ||
Module for querying Accounts information from the database. | ||
""" | ||
|
||
alias ElvenDatabase.Players.Account | ||
alias ElvenDatabase.Repo | ||
|
||
@spec log_in(String.t(), String.t()) :: Ecto.Schema.t() | nil | ||
def log_in(username, hashed_password) do | ||
Repo.get_by(Account, username: username, hashed_password: hashed_password) | ||
# Dyalizer doesn't like `Account.changeset(%Account{}, attrs)` | ||
# because fields on Account struct can't be nil | ||
@dialyzer [ | ||
{:no_return, create: 1, create!: 1}, | ||
{:no_fail_call, create: 1, create!: 1} | ||
] | ||
|
||
## Public API | ||
|
||
@spec authenticate(String.t(), String.t()) :: {:ok, Account.t()} | {:error, :not_found} | ||
def authenticate(username, hashed_password) do | ||
case Repo.get_by(Account, username: username, hashed_password: hashed_password) do | ||
%Account{} = account -> {:ok, account} | ||
nil -> {:error, :not_found} | ||
end | ||
end | ||
|
||
@spec create(map) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()} | ||
@spec create(map()) :: {:ok, Account.t()} | {:error, Ecto.Changeset.t()} | ||
def create(attrs) do | ||
%Account{} | ||
|> Account.changeset(attrs) | ||
|> Repo.insert() | ||
end | ||
|
||
@spec create!(map) :: Ecto.Schema.t() | ||
@spec create!(map()) :: Account.t() | ||
def create!(attrs) do | ||
%Account{} | ||
|> Account.changeset(attrs) | ||
|> Repo.insert!() | ||
end | ||
|
||
@spec get(Account.id()) :: {:ok, Account.t()} | {:error, :not_found} | ||
def get(id) do | ||
case Repo.get(Account, id) do | ||
nil -> {:error, :not_found} | ||
item -> {:ok, item} | ||
end | ||
end | ||
|
||
@spec get!(Account.id()) :: Account.t() | ||
def get!(id) do | ||
Repo.get!(Account, id) | ||
end | ||
|
||
@spec preload_characters(Account.t()) :: Account.t() | ||
def preload_characters(account) do | ||
Repo.preload(account, :characters) | ||
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
Oops, something went wrong.