From 19b9bbfb0d730d4704c506a678361a42e278c3b1 Mon Sep 17 00:00:00 2001 From: Adam Piontek Date: Thu, 11 Mar 2021 15:41:30 -0500 Subject: [PATCH] Accounts context can now set and clear a user's favorite shift template --- lib/shift73k/accounts.ex | 33 +++++++++++++++---- lib/shift73k/accounts/user.ex | 2 +- ...06173113_add_user_default_shift_column.exs | 2 +- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/lib/shift73k/accounts.ex b/lib/shift73k/accounts.ex index 52c94a3e..cc3a7078 100644 --- a/lib/shift73k/accounts.ex +++ b/lib/shift73k/accounts.ex @@ -7,6 +7,7 @@ defmodule Shift73k.Accounts do alias Shift73k.Repo alias Shift73k.Accounts.{User, UserToken, UserNotifier} alias Shift73kWeb.UserAuth + alias Shift73k.ShiftTemplates.ShiftTemplate ## Database getters @@ -162,13 +163,6 @@ defmodule Shift73k.Accounts do User.update_changeset(user, attrs, hash_password: true) end - # @doc """ - # Returns an `%Ecto.Changeset{}` for tracking singer_name updates. - # """ - # def change_singer_name_update(%User{} = user, attrs \\ %{}) do - # User.update_singer_name_changeset(user, attrs) - # end - @doc """ Updates the user given with attributes given """ @@ -449,4 +443,29 @@ defmodule Shift73k.Accounts do """ def delete_user(nil), do: {:error, false} def delete_user(%User{} = user), do: Repo.delete(user) + + ## Favorite Shift Template + + @doc """ + Sets a shift template as a user's favorite + """ + def set_user_fave_shift_template(%User{id: user_id}, %ShiftTemplate{ + id: shift_template_id, + user_id: user_id + }) do + User + |> where(id: ^user_id) + |> Repo.update_all(set: [fave_shift_template_id: shift_template_id]) + end + + def set_user_fave_shift_template(_, _), do: {0, nil} + + @doc """ + Clears a user's favorite shift template + """ + def unset_user_fave_shift_template(%User{id: user_id}) do + User + |> where(id: ^user_id) + |> Repo.update_all(set: [fave_shift_template_id: nil]) + end end diff --git a/lib/shift73k/accounts/user.ex b/lib/shift73k/accounts/user.ex index af59fa97..b1934068 100644 --- a/lib/shift73k/accounts/user.ex +++ b/lib/shift73k/accounts/user.ex @@ -29,7 +29,7 @@ defmodule Shift73k.Accounts.User do field(:role, RolesEnum, default: :user) has_many(:shift_templates, ShiftTemplate) - belongs_to(:default_shift_template, ShiftTemplate) + belongs_to(:fave_shift_template, ShiftTemplate) timestamps() end diff --git a/priv/repo/migrations/20210306173113_add_user_default_shift_column.exs b/priv/repo/migrations/20210306173113_add_user_default_shift_column.exs index 751b52e5..ee01179f 100644 --- a/priv/repo/migrations/20210306173113_add_user_default_shift_column.exs +++ b/priv/repo/migrations/20210306173113_add_user_default_shift_column.exs @@ -3,7 +3,7 @@ defmodule Shift73k.Repo.Migrations.AddUserDefaultShiftColumn do def change do alter table(:users) do - add(:default_shift_template_id, references(:shift_templates, type: :binary_id, on_delete: :delete_all)) + add(:fave_shift_template_id, references(:shift_templates, type: :binary_id, on_delete: :delete_all)) end end end