Skip to content

Commit

Permalink
Removed all todos (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
gerardo-navarro authored Jan 31, 2021
1 parent 455107b commit 98d902f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 16 deletions.
78 changes: 65 additions & 13 deletions lib/mindwendel/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,42 @@ defmodule Mindwendel.Accounts do
alias Mindwendel.Accounts.User
alias Mindwendel.Brainstormings.Brainstorming

# TODO: Add proper docu for methods
@doc """
Finds an existing user or creates a new user based on an UUID.
Returns nil if User does not exist or any other error is raised.
Returns nil if invalid UUID is given.
## Examples
iex> get_or_create_user(uuid)
%User{}
"""
def get_or_create_user(id) do
Repo.get(User, id) ||
case %User{id: id} |> Repo.insert() do
{:ok, user} -> user
end
end

@doc """
Gets a single user based on its UUID.
Returns nil if User does not exist or any other error is raised.
Returns nil if invalid UUID is given.
## Examples
iex> get_user("valid-uuid")
%User{}
iex> get_user("invalid-or-non-existing-uuid")
nil
"""
def get_user(id) when is_nil(id) do
nil
end
Expand All @@ -22,26 +49,38 @@ defmodule Mindwendel.Accounts do
Ecto.Query.CastError -> nil
end

@doc """
Updates a user.
## Examples
iex> update_user(user, %{field: new_value})
{:ok, %User{}}
iex> update_user(user, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_user(%User{} = user, attrs) do
user
|> User.changeset(attrs)
|> Repo.update()
end

def merge_brainstorming_user(%Brainstorming{} = brainstorming, user_id)
when is_nil(user_id) do
brainstorming
end
@doc """
Connects user to a brainstorm.
def merge_brainstorming_user(%Brainstorming{} = brainstorming, user_id)
when is_binary(user_id) do
# TODO: Convert this to a guard
case Ecto.UUID.dump(user_id) do
:error -> brainstorming
{:ok, _} -> merge_brainstorming_user(brainstorming, get_or_create_user(user_id))
end
end
Returns a valid brainstorming with preloaded user list.
## Examples
iex> merge_brainstorming_user(brainstorming, user)
%Brainstorming{}
iex> update_user(user, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def merge_brainstorming_user(%Brainstorming{} = brainstorming, %User{} = user) do
unless user.id in Enum.map(brainstorming.users, fn e -> e.id end) do
brainstorming_users = [user | brainstorming.users]
Expand All @@ -59,4 +98,17 @@ defmodule Mindwendel.Accounts do
brainstorming
end
end

def merge_brainstorming_user(%Brainstorming{} = brainstorming, user_id)
when is_nil(user_id) do
brainstorming
end

def merge_brainstorming_user(%Brainstorming{} = brainstorming, user_id)
when is_binary(user_id) do
case Ecto.UUID.dump(user_id) do
:error -> brainstorming
{:ok, _} -> merge_brainstorming_user(brainstorming, get_or_create_user(user_id))
end
end
end
1 change: 0 additions & 1 deletion lib/mindwendel/brainstormings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ defmodule Mindwendel.Brainstormings do
** (Ecto.Query.CastError)
"""
# TODO: Handle CastError when wrong uuid is given
# See https://stackoverflow.com/questions/53802091/elixir-uuid-how-to-handle-500-error-when-uuid-doesnt-match
def get_brainstorming!(id) do
Repo.get!(Brainstorming, id) |> Repo.preload([:users, ideas: [:link, :likes]])
Expand Down
1 change: 0 additions & 1 deletion lib/mindwendel_web/templates/static_page/home.html.leex
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
%>
<%= for brainstorming <- sorted_brainstorming do %>
<a href="<%= Routes.brainstorming_show_path(@conn, :show, brainstorming) %>">
<%# TODO: Better styling %>
<h5><%= brainstorming.name %> <span class="badge rounded-pill bg-light text-dark"><%= Timex.format!(brainstorming.inserted_at, "{relative}", :relative) %></span></h5>
</a>
<% end %>
Expand Down
1 change: 0 additions & 1 deletion test/support/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ defmodule Mindwendel.Factory do
}
end

# TODO: extract to helper
def build(factory_name, attributes) do
factory_name |> build() |> struct!(attributes)
end
Expand Down

0 comments on commit 98d902f

Please sign in to comment.