Implement core denizen interface
This commit is contained in:
parent
cdbb6646eb
commit
e983ef7ce1
|
@ -27,6 +27,49 @@ defmodule Hostas.Denizen do
|
|||
%{password_hash: hash} = Bcrypt.add_hash(password)
|
||||
change(changeset, password: hash)
|
||||
end
|
||||
|
||||
defp hash_password(changeset), do: changeset
|
||||
|
||||
@doc """
|
||||
Returns an Ecto query searching for a denizen by the given ID
|
||||
"""
|
||||
defp query_by_id(id) do
|
||||
from d in Denizen, where: d.id == ^id
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a denizen. Returns the result of the Ecto.Repo insertion
|
||||
"""
|
||||
def create(denizen) do
|
||||
Repo.insert(changeset(%Denizen{}, denizen))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a denizen
|
||||
"""
|
||||
def get(id) do
|
||||
case query_by_id(id) |> Repo.one do
|
||||
nil -> {:error, :not_found}
|
||||
struct -> {:ok, struct}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Removes a denizen from the community. Does nothing if the denizen doesn't
|
||||
exist
|
||||
"""
|
||||
def delete(id) do
|
||||
id
|
||||
|> query_by_id
|
||||
|> Repo.delete_all
|
||||
|
||||
:ok
|
||||
end
|
||||
|
||||
@doc """
|
||||
Modifies all the denizen's fields except `:id`, replacing them with the
|
||||
corresponding values in the map
|
||||
"""
|
||||
def update(%{"id" => id} = update_map) do
|
||||
Repo.update(changeset(get(id), update_map))
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue