diff --git a/lib/mindwendel/accounts.ex b/lib/mindwendel/accounts.ex index a027684e..3f3aa70b 100644 --- a/lib/mindwendel/accounts.ex +++ b/lib/mindwendel/accounts.ex @@ -3,8 +3,19 @@ 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 @@ -12,6 +23,22 @@ defmodule Mindwendel.Accounts do 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 @@ -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] @@ -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 diff --git a/lib/mindwendel/brainstormings.ex b/lib/mindwendel/brainstormings.ex index c8ffb531..dc9cff1b 100644 --- a/lib/mindwendel/brainstormings.ex +++ b/lib/mindwendel/brainstormings.ex @@ -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]]) diff --git a/lib/mindwendel_web/templates/static_page/home.html.leex b/lib/mindwendel_web/templates/static_page/home.html.leex index f86acbf3..3852f692 100644 --- a/lib/mindwendel_web/templates/static_page/home.html.leex +++ b/lib/mindwendel_web/templates/static_page/home.html.leex @@ -36,7 +36,6 @@ %> <%= for brainstorming <- sorted_brainstorming do %> - <%# TODO: Better styling %>
<%= brainstorming.name %> <%= Timex.format!(brainstorming.inserted_at, "{relative}", :relative) %>
<% end %> diff --git a/test/support/factory.ex b/test/support/factory.ex index fc13a3ee..7fc4eb99 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -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