diff --git a/.iex.exs b/.iex.exs index 99529970..0cc62318 100644 --- a/.iex.exs +++ b/.iex.exs @@ -3,5 +3,5 @@ import Ecto.Query alias Shift73k.Repo alias Shift73k.Accounts alias Shift73k.Accounts.User -alias Shift73k.ShiftTemplates -alias Shift73k.ShiftTemplates.ShiftTemplate +alias Shift73k.Shifts.Templates +alias Shift73k.Shifts.Templates.ShiftTemplate diff --git a/lib/shift73k/accounts.ex b/lib/shift73k/accounts.ex index c90f2633..965e3c31 100644 --- a/lib/shift73k/accounts.ex +++ b/lib/shift73k/accounts.ex @@ -7,7 +7,6 @@ defmodule Shift73k.Accounts do alias Shift73k.Repo alias Shift73k.Accounts.{User, UserToken, UserNotifier} alias Shift73kWeb.UserAuth - alias Shift73k.ShiftTemplates.ShiftTemplate ## Database getters diff --git a/lib/shift73k/accounts/user.ex b/lib/shift73k/accounts/user.ex index 387f3561..f1e44f9e 100644 --- a/lib/shift73k/accounts/user.ex +++ b/lib/shift73k/accounts/user.ex @@ -3,7 +3,7 @@ defmodule Shift73k.Accounts.User do import Ecto.Changeset import EctoEnum - alias Shift73k.ShiftTemplates.ShiftTemplate + alias Shift73k.Shifts.Templates.ShiftTemplate @roles [ user: "Basic user level", diff --git a/lib/shift73k/shift_templates.ex b/lib/shift73k/shifts/templates.ex similarity index 95% rename from lib/shift73k/shift_templates.ex rename to lib/shift73k/shifts/templates.ex index 9520613c..0e0c251e 100644 --- a/lib/shift73k/shift_templates.ex +++ b/lib/shift73k/shifts/templates.ex @@ -1,12 +1,12 @@ -defmodule Shift73k.ShiftTemplates do +defmodule Shift73k.Shifts.Templates do @moduledoc """ - The ShiftTemplates context. + The Shifts.Templates context. """ import Ecto.Query, warn: false alias Shift73k.Repo - alias Shift73k.ShiftTemplates.ShiftTemplate + alias Shift73k.Shifts.Templates.ShiftTemplate @doc """ Returns the list of shift_templates. diff --git a/lib/shift73k/shift_templates/shift_template.ex b/lib/shift73k/shifts/templates/shift_template.ex similarity index 97% rename from lib/shift73k/shift_templates/shift_template.ex rename to lib/shift73k/shifts/templates/shift_template.ex index f5c6c2ca..65ca7fb6 100644 --- a/lib/shift73k/shift_templates/shift_template.ex +++ b/lib/shift73k/shifts/templates/shift_template.ex @@ -1,4 +1,4 @@ -defmodule Shift73k.ShiftTemplates.ShiftTemplate do +defmodule Shift73k.Shifts.Templates.ShiftTemplate do use Ecto.Schema import Ecto.Changeset diff --git a/lib/shift73k_web/live/shift_template_live/delete_component.ex b/lib/shift73k_web/live/shift_template_live/delete_component.ex index 47f62f73..6540d6cb 100644 --- a/lib/shift73k_web/live/shift_template_live/delete_component.ex +++ b/lib/shift73k_web/live/shift_template_live/delete_component.ex @@ -1,7 +1,7 @@ defmodule Shift73kWeb.ShiftTemplateLive.DeleteComponent do use Shift73kWeb, :live_component - alias Shift73k.ShiftTemplates + alias Shift73k.Shifts.Templates @impl true def update(assigns, socket) do @@ -18,8 +18,8 @@ defmodule Shift73kWeb.ShiftTemplateLive.DeleteComponent do @impl true def handle_event("confirm", %{"id" => id, "subject" => subject}, socket) do id - |> ShiftTemplates.get_shift_template() - |> ShiftTemplates.delete_shift_template() + |> Templates.get_shift_template() + |> Templates.delete_shift_template() |> case do {:ok, _} -> flash = {:info, "Shift template deleted successfully: \"#{subject}\""} diff --git a/lib/shift73k_web/live/shift_template_live/form_component.ex b/lib/shift73k_web/live/shift_template_live/form_component.ex index c7969881..53d9965f 100644 --- a/lib/shift73k_web/live/shift_template_live/form_component.ex +++ b/lib/shift73k_web/live/shift_template_live/form_component.ex @@ -1,12 +1,12 @@ defmodule Shift73kWeb.ShiftTemplateLive.FormComponent do use Shift73kWeb, :live_component - alias Shift73k.ShiftTemplates - alias Shift73k.ShiftTemplates.ShiftTemplate + alias Shift73k.Shifts.Templates + alias Shift73k.Shifts.Templates.ShiftTemplate @impl true def update(%{shift_template: shift_template} = assigns, socket) do - changeset = ShiftTemplates.change_shift_template(shift_template) + changeset = Templates.change_shift_template(shift_template) socket |> assign(assigns) @@ -37,7 +37,7 @@ defmodule Shift73kWeb.ShiftTemplateLive.FormComponent do changeset = socket.assigns.shift_template - |> ShiftTemplates.change_shift_template(shift_template_params) + |> Templates.change_shift_template(shift_template_params) |> Map.put(:action, :validate) socket @@ -63,7 +63,7 @@ defmodule Shift73kWeb.ShiftTemplateLive.FormComponent do end defp save_shift_template(socket, :new, shift_template_params) do - case ShiftTemplates.create_shift_template(shift_template_params) do + case Templates.create_shift_template(shift_template_params) do {:ok, _shift_template} -> flash = {:info, "Shift template created successfully"} send(self(), {:put_flash_message, flash}) @@ -82,7 +82,7 @@ defmodule Shift73kWeb.ShiftTemplateLive.FormComponent do end defp save_shift_template(socket, :edit, shift_template_params) do - case ShiftTemplates.update_shift_template( + case Templates.update_shift_template( socket.assigns.shift_template, shift_template_params ) do diff --git a/lib/shift73k_web/live/shift_template_live/index.ex b/lib/shift73k_web/live/shift_template_live/index.ex index 053d8354..20fc1e80 100644 --- a/lib/shift73k_web/live/shift_template_live/index.ex +++ b/lib/shift73k_web/live/shift_template_live/index.ex @@ -2,8 +2,8 @@ defmodule Shift73kWeb.ShiftTemplateLive.Index do use Shift73kWeb, :live_view alias Shift73k.Accounts - alias Shift73k.ShiftTemplates - alias Shift73k.ShiftTemplates.ShiftTemplate + alias Shift73k.Shifts.Templates + alias Shift73k.Shifts.Templates.ShiftTemplate alias Shift73kWeb.Roles @impl true @@ -38,13 +38,13 @@ defmodule Shift73kWeb.ShiftTemplateLive.Index do defp apply_action(socket, :clone, %{"id" => id}) do socket |> assign(:page_title, "Clone Shift Template") - |> assign(:shift_template, ShiftTemplates.get_shift_template!(id)) + |> assign(:shift_template, Templates.get_shift_template!(id)) end defp apply_action(socket, :edit, %{"id" => id}) do socket |> assign(:page_title, "Edit Shift Template") - |> assign(:shift_template, ShiftTemplates.get_shift_template!(id)) + |> assign(:shift_template, Templates.get_shift_template!(id)) end defp apply_action(socket, :new, _params) do @@ -61,20 +61,20 @@ defmodule Shift73kWeb.ShiftTemplateLive.Index do defp assign_shift_templates(socket) do %User{id: uid} = socket.assigns.current_user - user_shifts = ShiftTemplates.list_shift_templates_by_user_id(uid) + user_shifts = Templates.list_shift_templates_by_user_id(uid) assign(socket, :shift_templates, user_shifts) end defp shift_template_from_params(params) defp shift_template_from_params(%{"id" => id}), - do: ShiftTemplates.get_shift_template!(id) + do: Templates.get_shift_template!(id) defp shift_template_from_params(_params), do: %ShiftTemplate{} @impl true def handle_event("delete-modal", %{"id" => id}, socket) do - {:noreply, assign(socket, :delete_shift_template, ShiftTemplates.get_shift_template!(id))} + {:noreply, assign(socket, :delete_shift_template, Templates.get_shift_template!(id))} end def handle_event("set-user-fave-shift-template", %{"id" => shift_template_id}, socket) do @@ -99,8 +99,8 @@ defmodule Shift73kWeb.ShiftTemplateLive.Index do # @impl true # def handle_event("delete", %{"id" => id}, socket) do - # shift_template = ShiftTemplates.get_shift_template!(id) - # {:ok, _} = ShiftTemplates.delete_shift_template(shift_template) + # shift_template = Templates.get_shift_template!(id) + # {:ok, _} = Templates.delete_shift_template(shift_template) # {:noreply, assign_shift_templates(socket)} # end diff --git a/lib/shift73k_web/roles.ex b/lib/shift73k_web/roles.ex index 630c4277..f020aa17 100644 --- a/lib/shift73k_web/roles.ex +++ b/lib/shift73k_web/roles.ex @@ -4,7 +4,7 @@ defmodule Shift73kWeb.Roles do """ alias Shift73k.Accounts.User - alias Shift73k.ShiftTemplates.ShiftTemplate + alias Shift73k.Shifts.Templates.ShiftTemplate @type entity :: struct() @type action :: :new | :index | :edit | :show | :delete | :edit_role @@ -12,7 +12,7 @@ defmodule Shift73kWeb.Roles do def can?(user, entity, action) - # ShiftTemplates / ShiftTemplate + # Shifts.Templates / ShiftTemplate def can?(%User{role: :admin}, %ShiftTemplate{}, _any), do: true def can?(%User{}, %ShiftTemplate{}, :index), do: true def can?(%User{}, %ShiftTemplate{}, :new), do: true diff --git a/priv/repo/migrations/20210306171754_create_shift_templates.exs b/priv/repo/migrations/20210306171754_create_shift_templates.exs index 7f0050a5..e97afe57 100644 --- a/priv/repo/migrations/20210306171754_create_shift_templates.exs +++ b/priv/repo/migrations/20210306171754_create_shift_templates.exs @@ -1,4 +1,4 @@ -defmodule Shift73k.Repo.Migrations.CreateShiftTemplates do +defmodule Shift73k.Repo.Migrations.CreateShifts.Templates do use Ecto.Migration def change do diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs index b976d279..5d3cf543 100644 --- a/priv/repo/seeds.exs +++ b/priv/repo/seeds.exs @@ -77,7 +77,7 @@ Repo.insert_all(User, mock_users) ##### # shift tepmlates -alias Shift73k.ShiftTemplates.ShiftTemplate +alias Shift73k.Shifts.Templates.ShiftTemplate shifts_json = Path.join(this_path, "MOCK_DATA_shift-templates.json") mock_shifts = shifts_json |> File.read!() |> Jason.decode!() diff --git a/test/shift73k/shift_templates_test.exs b/test/shift73k/shift_templates_test.exs index 717390b9..d3813c81 100644 --- a/test/shift73k/shift_templates_test.exs +++ b/test/shift73k/shift_templates_test.exs @@ -1,36 +1,59 @@ -defmodule Shift73k.ShiftTemplatesTest do +defmodule Shift73k.Shifts.TemplatesTest do use Shift73k.DataCase - alias Shift73k.ShiftTemplates + alias Shift73k.Shifts.Templates describe "shift_templates" do - alias Shift73k.ShiftTemplates.ShiftTemplate + alias Shift73k.Shifts.Templates.ShiftTemplate - @valid_attrs %{description: "some description", location: "some location", time_start: ~T[08:00:00], time_end: ~T[16:00:00], subject: "some subject", time_zone: "some time_zone"} - @update_attrs %{description: "some updated description", location: "some updated location", time_start: ~T[13:00:00], time_end: ~T[19:30:00], subject: "some updated subject", time_zone: "some updated time_zone"} - @invalid_attrs %{description: nil, location: nil, time_start: nil, time_end: nil, subject: nil, time_zone: nil} + @valid_attrs %{ + description: "some description", + location: "some location", + time_start: ~T[08:00:00], + time_end: ~T[16:00:00], + subject: "some subject", + time_zone: "some time_zone" + } + @update_attrs %{ + description: "some updated description", + location: "some updated location", + time_start: ~T[13:00:00], + time_end: ~T[19:30:00], + subject: "some updated subject", + time_zone: "some updated time_zone" + } + @invalid_attrs %{ + description: nil, + location: nil, + time_start: nil, + time_end: nil, + subject: nil, + time_zone: nil + } def shift_template_fixture(attrs \\ %{}) do {:ok, shift_template} = attrs |> Enum.into(@valid_attrs) - |> ShiftTemplates.create_shift_template() + |> Templates.create_shift_template() shift_template end test "list_shift_templates/0 returns all shift_templates" do shift_template = shift_template_fixture() - assert ShiftTemplates.list_shift_templates() == [shift_template] + assert Templates.list_shift_templates() == [shift_template] end test "get_shift_template!/1 returns the shift_template with given id" do shift_template = shift_template_fixture() - assert ShiftTemplates.get_shift_template!(shift_template.id) == shift_template + assert Templates.get_shift_template!(shift_template.id) == shift_template end test "create_shift_template/1 with valid data creates a shift_template" do - assert {:ok, %ShiftTemplate{} = shift_template} = ShiftTemplates.create_shift_template(@valid_attrs) + assert {:ok, %ShiftTemplate{} = shift_template} = + Templates.create_shift_template(@valid_attrs) + assert shift_template.description == "some description" assert shift_template.location == "some location" assert shift_template.time_start == ~T[07:00:00] @@ -40,12 +63,15 @@ defmodule Shift73k.ShiftTemplatesTest do end test "create_shift_template/1 with invalid data returns error changeset" do - assert {:error, %Ecto.Changeset{}} = ShiftTemplates.create_shift_template(@invalid_attrs) + assert {:error, %Ecto.Changeset{}} = Templates.create_shift_template(@invalid_attrs) end test "update_shift_template/2 with valid data updates the shift_template" do shift_template = shift_template_fixture() - assert {:ok, %ShiftTemplate{} = shift_template} = ShiftTemplates.update_shift_template(shift_template, @update_attrs) + + assert {:ok, %ShiftTemplate{} = shift_template} = + Templates.update_shift_template(shift_template, @update_attrs) + assert shift_template.description == "some updated description" assert shift_template.location == "some updated location" assert shift_template.time_start == ~T[15:00:00] @@ -56,19 +82,25 @@ defmodule Shift73k.ShiftTemplatesTest do test "update_shift_template/2 with invalid data returns error changeset" do shift_template = shift_template_fixture() - assert {:error, %Ecto.Changeset{}} = ShiftTemplates.update_shift_template(shift_template, @invalid_attrs) - assert shift_template == ShiftTemplates.get_shift_template!(shift_template.id) + + assert {:error, %Ecto.Changeset{}} = + Templates.update_shift_template(shift_template, @invalid_attrs) + + assert shift_template == Templates.get_shift_template!(shift_template.id) end test "delete_shift_template/1 deletes the shift_template" do shift_template = shift_template_fixture() - assert {:ok, %ShiftTemplate{}} = ShiftTemplates.delete_shift_template(shift_template) - assert_raise Ecto.NoResultsError, fn -> ShiftTemplates.get_shift_template!(shift_template.id) end + assert {:ok, %ShiftTemplate{}} = Templates.delete_shift_template(shift_template) + + assert_raise Ecto.NoResultsError, fn -> + Templates.get_shift_template!(shift_template.id) + end end test "change_shift_template/1 returns a shift_template changeset" do shift_template = shift_template_fixture() - assert %Ecto.Changeset{} = ShiftTemplates.change_shift_template(shift_template) + assert %Ecto.Changeset{} = Templates.change_shift_template(shift_template) end end end diff --git a/test/shift73k_web/live/shift_template_live_test.exs b/test/shift73k_web/live/shift_template_live_test.exs index 290d762b..9391291d 100644 --- a/test/shift73k_web/live/shift_template_live_test.exs +++ b/test/shift73k_web/live/shift_template_live_test.exs @@ -3,7 +3,7 @@ defmodule Shift73kWeb.ShiftTemplateLiveTest do import Phoenix.LiveViewTest - alias Shift73k.ShiftTemplates + alias Shift73k.Shifts.Templates @create_attrs %{ description: "some description", @@ -31,7 +31,7 @@ defmodule Shift73kWeb.ShiftTemplateLiveTest do } defp fixture(:shift_template) do - {:ok, shift_template} = ShiftTemplates.create_shift_template(@create_attrs) + {:ok, shift_template} = Templates.create_shift_template(@create_attrs) shift_template end