reorganized shift templates files
This commit is contained in:
parent
243ebd8aa2
commit
e0f8515566
13 changed files with 80 additions and 49 deletions
4
.iex.exs
4
.iex.exs
|
@ -3,5 +3,5 @@ import Ecto.Query
|
||||||
alias Shift73k.Repo
|
alias Shift73k.Repo
|
||||||
alias Shift73k.Accounts
|
alias Shift73k.Accounts
|
||||||
alias Shift73k.Accounts.User
|
alias Shift73k.Accounts.User
|
||||||
alias Shift73k.ShiftTemplates
|
alias Shift73k.Shifts.Templates
|
||||||
alias Shift73k.ShiftTemplates.ShiftTemplate
|
alias Shift73k.Shifts.Templates.ShiftTemplate
|
||||||
|
|
|
@ -7,7 +7,6 @@ defmodule Shift73k.Accounts do
|
||||||
alias Shift73k.Repo
|
alias Shift73k.Repo
|
||||||
alias Shift73k.Accounts.{User, UserToken, UserNotifier}
|
alias Shift73k.Accounts.{User, UserToken, UserNotifier}
|
||||||
alias Shift73kWeb.UserAuth
|
alias Shift73kWeb.UserAuth
|
||||||
alias Shift73k.ShiftTemplates.ShiftTemplate
|
|
||||||
|
|
||||||
## Database getters
|
## Database getters
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ defmodule Shift73k.Accounts.User do
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
import EctoEnum
|
import EctoEnum
|
||||||
|
|
||||||
alias Shift73k.ShiftTemplates.ShiftTemplate
|
alias Shift73k.Shifts.Templates.ShiftTemplate
|
||||||
|
|
||||||
@roles [
|
@roles [
|
||||||
user: "Basic user level",
|
user: "Basic user level",
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
defmodule Shift73k.ShiftTemplates do
|
defmodule Shift73k.Shifts.Templates do
|
||||||
@moduledoc """
|
@moduledoc """
|
||||||
The ShiftTemplates context.
|
The Shifts.Templates context.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import Ecto.Query, warn: false
|
import Ecto.Query, warn: false
|
||||||
alias Shift73k.Repo
|
alias Shift73k.Repo
|
||||||
|
|
||||||
alias Shift73k.ShiftTemplates.ShiftTemplate
|
alias Shift73k.Shifts.Templates.ShiftTemplate
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Returns the list of shift_templates.
|
Returns the list of shift_templates.
|
|
@ -1,4 +1,4 @@
|
||||||
defmodule Shift73k.ShiftTemplates.ShiftTemplate do
|
defmodule Shift73k.Shifts.Templates.ShiftTemplate do
|
||||||
use Ecto.Schema
|
use Ecto.Schema
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
defmodule Shift73kWeb.ShiftTemplateLive.DeleteComponent do
|
defmodule Shift73kWeb.ShiftTemplateLive.DeleteComponent do
|
||||||
use Shift73kWeb, :live_component
|
use Shift73kWeb, :live_component
|
||||||
|
|
||||||
alias Shift73k.ShiftTemplates
|
alias Shift73k.Shifts.Templates
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def update(assigns, socket) do
|
def update(assigns, socket) do
|
||||||
|
@ -18,8 +18,8 @@ defmodule Shift73kWeb.ShiftTemplateLive.DeleteComponent do
|
||||||
@impl true
|
@impl true
|
||||||
def handle_event("confirm", %{"id" => id, "subject" => subject}, socket) do
|
def handle_event("confirm", %{"id" => id, "subject" => subject}, socket) do
|
||||||
id
|
id
|
||||||
|> ShiftTemplates.get_shift_template()
|
|> Templates.get_shift_template()
|
||||||
|> ShiftTemplates.delete_shift_template()
|
|> Templates.delete_shift_template()
|
||||||
|> case do
|
|> case do
|
||||||
{:ok, _} ->
|
{:ok, _} ->
|
||||||
flash = {:info, "Shift template deleted successfully: \"#{subject}\""}
|
flash = {:info, "Shift template deleted successfully: \"#{subject}\""}
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
defmodule Shift73kWeb.ShiftTemplateLive.FormComponent do
|
defmodule Shift73kWeb.ShiftTemplateLive.FormComponent do
|
||||||
use Shift73kWeb, :live_component
|
use Shift73kWeb, :live_component
|
||||||
|
|
||||||
alias Shift73k.ShiftTemplates
|
alias Shift73k.Shifts.Templates
|
||||||
alias Shift73k.ShiftTemplates.ShiftTemplate
|
alias Shift73k.Shifts.Templates.ShiftTemplate
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def update(%{shift_template: shift_template} = assigns, socket) do
|
def update(%{shift_template: shift_template} = assigns, socket) do
|
||||||
changeset = ShiftTemplates.change_shift_template(shift_template)
|
changeset = Templates.change_shift_template(shift_template)
|
||||||
|
|
||||||
socket
|
socket
|
||||||
|> assign(assigns)
|
|> assign(assigns)
|
||||||
|
@ -37,7 +37,7 @@ defmodule Shift73kWeb.ShiftTemplateLive.FormComponent do
|
||||||
|
|
||||||
changeset =
|
changeset =
|
||||||
socket.assigns.shift_template
|
socket.assigns.shift_template
|
||||||
|> ShiftTemplates.change_shift_template(shift_template_params)
|
|> Templates.change_shift_template(shift_template_params)
|
||||||
|> Map.put(:action, :validate)
|
|> Map.put(:action, :validate)
|
||||||
|
|
||||||
socket
|
socket
|
||||||
|
@ -63,7 +63,7 @@ defmodule Shift73kWeb.ShiftTemplateLive.FormComponent do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp save_shift_template(socket, :new, shift_template_params) do
|
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} ->
|
{:ok, _shift_template} ->
|
||||||
flash = {:info, "Shift template created successfully"}
|
flash = {:info, "Shift template created successfully"}
|
||||||
send(self(), {:put_flash_message, flash})
|
send(self(), {:put_flash_message, flash})
|
||||||
|
@ -82,7 +82,7 @@ defmodule Shift73kWeb.ShiftTemplateLive.FormComponent do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp save_shift_template(socket, :edit, shift_template_params) do
|
defp save_shift_template(socket, :edit, shift_template_params) do
|
||||||
case ShiftTemplates.update_shift_template(
|
case Templates.update_shift_template(
|
||||||
socket.assigns.shift_template,
|
socket.assigns.shift_template,
|
||||||
shift_template_params
|
shift_template_params
|
||||||
) do
|
) do
|
||||||
|
|
|
@ -2,8 +2,8 @@ defmodule Shift73kWeb.ShiftTemplateLive.Index do
|
||||||
use Shift73kWeb, :live_view
|
use Shift73kWeb, :live_view
|
||||||
|
|
||||||
alias Shift73k.Accounts
|
alias Shift73k.Accounts
|
||||||
alias Shift73k.ShiftTemplates
|
alias Shift73k.Shifts.Templates
|
||||||
alias Shift73k.ShiftTemplates.ShiftTemplate
|
alias Shift73k.Shifts.Templates.ShiftTemplate
|
||||||
alias Shift73kWeb.Roles
|
alias Shift73kWeb.Roles
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
|
@ -38,13 +38,13 @@ defmodule Shift73kWeb.ShiftTemplateLive.Index do
|
||||||
defp apply_action(socket, :clone, %{"id" => id}) do
|
defp apply_action(socket, :clone, %{"id" => id}) do
|
||||||
socket
|
socket
|
||||||
|> assign(:page_title, "Clone Shift Template")
|
|> assign(:page_title, "Clone Shift Template")
|
||||||
|> assign(:shift_template, ShiftTemplates.get_shift_template!(id))
|
|> assign(:shift_template, Templates.get_shift_template!(id))
|
||||||
end
|
end
|
||||||
|
|
||||||
defp apply_action(socket, :edit, %{"id" => id}) do
|
defp apply_action(socket, :edit, %{"id" => id}) do
|
||||||
socket
|
socket
|
||||||
|> assign(:page_title, "Edit Shift Template")
|
|> assign(:page_title, "Edit Shift Template")
|
||||||
|> assign(:shift_template, ShiftTemplates.get_shift_template!(id))
|
|> assign(:shift_template, Templates.get_shift_template!(id))
|
||||||
end
|
end
|
||||||
|
|
||||||
defp apply_action(socket, :new, _params) do
|
defp apply_action(socket, :new, _params) do
|
||||||
|
@ -61,20 +61,20 @@ defmodule Shift73kWeb.ShiftTemplateLive.Index do
|
||||||
|
|
||||||
defp assign_shift_templates(socket) do
|
defp assign_shift_templates(socket) do
|
||||||
%User{id: uid} = socket.assigns.current_user
|
%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)
|
assign(socket, :shift_templates, user_shifts)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp shift_template_from_params(params)
|
defp shift_template_from_params(params)
|
||||||
|
|
||||||
defp shift_template_from_params(%{"id" => id}),
|
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{}
|
defp shift_template_from_params(_params), do: %ShiftTemplate{}
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_event("delete-modal", %{"id" => id}, socket) do
|
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
|
end
|
||||||
|
|
||||||
def handle_event("set-user-fave-shift-template", %{"id" => shift_template_id}, socket) do
|
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
|
# @impl true
|
||||||
# def handle_event("delete", %{"id" => id}, socket) do
|
# def handle_event("delete", %{"id" => id}, socket) do
|
||||||
# shift_template = ShiftTemplates.get_shift_template!(id)
|
# shift_template = Templates.get_shift_template!(id)
|
||||||
# {:ok, _} = ShiftTemplates.delete_shift_template(shift_template)
|
# {:ok, _} = Templates.delete_shift_template(shift_template)
|
||||||
|
|
||||||
# {:noreply, assign_shift_templates(socket)}
|
# {:noreply, assign_shift_templates(socket)}
|
||||||
# end
|
# end
|
||||||
|
|
|
@ -4,7 +4,7 @@ defmodule Shift73kWeb.Roles do
|
||||||
"""
|
"""
|
||||||
|
|
||||||
alias Shift73k.Accounts.User
|
alias Shift73k.Accounts.User
|
||||||
alias Shift73k.ShiftTemplates.ShiftTemplate
|
alias Shift73k.Shifts.Templates.ShiftTemplate
|
||||||
|
|
||||||
@type entity :: struct()
|
@type entity :: struct()
|
||||||
@type action :: :new | :index | :edit | :show | :delete | :edit_role
|
@type action :: :new | :index | :edit | :show | :delete | :edit_role
|
||||||
|
@ -12,7 +12,7 @@ defmodule Shift73kWeb.Roles do
|
||||||
|
|
||||||
def can?(user, entity, action)
|
def can?(user, entity, action)
|
||||||
|
|
||||||
# ShiftTemplates / ShiftTemplate
|
# Shifts.Templates / ShiftTemplate
|
||||||
def can?(%User{role: :admin}, %ShiftTemplate{}, _any), do: true
|
def can?(%User{role: :admin}, %ShiftTemplate{}, _any), do: true
|
||||||
def can?(%User{}, %ShiftTemplate{}, :index), do: true
|
def can?(%User{}, %ShiftTemplate{}, :index), do: true
|
||||||
def can?(%User{}, %ShiftTemplate{}, :new), do: true
|
def can?(%User{}, %ShiftTemplate{}, :new), do: true
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
defmodule Shift73k.Repo.Migrations.CreateShiftTemplates do
|
defmodule Shift73k.Repo.Migrations.CreateShifts.Templates do
|
||||||
use Ecto.Migration
|
use Ecto.Migration
|
||||||
|
|
||||||
def change do
|
def change do
|
||||||
|
|
|
@ -77,7 +77,7 @@ Repo.insert_all(User, mock_users)
|
||||||
|
|
||||||
#####
|
#####
|
||||||
# shift tepmlates
|
# shift tepmlates
|
||||||
alias Shift73k.ShiftTemplates.ShiftTemplate
|
alias Shift73k.Shifts.Templates.ShiftTemplate
|
||||||
|
|
||||||
shifts_json = Path.join(this_path, "MOCK_DATA_shift-templates.json")
|
shifts_json = Path.join(this_path, "MOCK_DATA_shift-templates.json")
|
||||||
mock_shifts = shifts_json |> File.read!() |> Jason.decode!()
|
mock_shifts = shifts_json |> File.read!() |> Jason.decode!()
|
||||||
|
|
|
@ -1,36 +1,59 @@
|
||||||
defmodule Shift73k.ShiftTemplatesTest do
|
defmodule Shift73k.Shifts.TemplatesTest do
|
||||||
use Shift73k.DataCase
|
use Shift73k.DataCase
|
||||||
|
|
||||||
alias Shift73k.ShiftTemplates
|
alias Shift73k.Shifts.Templates
|
||||||
|
|
||||||
describe "shift_templates" do
|
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"}
|
@valid_attrs %{
|
||||||
@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"}
|
description: "some description",
|
||||||
@invalid_attrs %{description: nil, location: nil, time_start: nil, time_end: nil, subject: nil, time_zone: nil}
|
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
|
def shift_template_fixture(attrs \\ %{}) do
|
||||||
{:ok, shift_template} =
|
{:ok, shift_template} =
|
||||||
attrs
|
attrs
|
||||||
|> Enum.into(@valid_attrs)
|
|> Enum.into(@valid_attrs)
|
||||||
|> ShiftTemplates.create_shift_template()
|
|> Templates.create_shift_template()
|
||||||
|
|
||||||
shift_template
|
shift_template
|
||||||
end
|
end
|
||||||
|
|
||||||
test "list_shift_templates/0 returns all shift_templates" do
|
test "list_shift_templates/0 returns all shift_templates" do
|
||||||
shift_template = shift_template_fixture()
|
shift_template = shift_template_fixture()
|
||||||
assert ShiftTemplates.list_shift_templates() == [shift_template]
|
assert Templates.list_shift_templates() == [shift_template]
|
||||||
end
|
end
|
||||||
|
|
||||||
test "get_shift_template!/1 returns the shift_template with given id" do
|
test "get_shift_template!/1 returns the shift_template with given id" do
|
||||||
shift_template = shift_template_fixture()
|
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
|
end
|
||||||
|
|
||||||
test "create_shift_template/1 with valid data creates a shift_template" do
|
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.description == "some description"
|
||||||
assert shift_template.location == "some location"
|
assert shift_template.location == "some location"
|
||||||
assert shift_template.time_start == ~T[07:00:00]
|
assert shift_template.time_start == ~T[07:00:00]
|
||||||
|
@ -40,12 +63,15 @@ defmodule Shift73k.ShiftTemplatesTest do
|
||||||
end
|
end
|
||||||
|
|
||||||
test "create_shift_template/1 with invalid data returns error changeset" do
|
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
|
end
|
||||||
|
|
||||||
test "update_shift_template/2 with valid data updates the shift_template" do
|
test "update_shift_template/2 with valid data updates the shift_template" do
|
||||||
shift_template = shift_template_fixture()
|
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.description == "some updated description"
|
||||||
assert shift_template.location == "some updated location"
|
assert shift_template.location == "some updated location"
|
||||||
assert shift_template.time_start == ~T[15:00:00]
|
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
|
test "update_shift_template/2 with invalid data returns error changeset" do
|
||||||
shift_template = shift_template_fixture()
|
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
|
end
|
||||||
|
|
||||||
test "delete_shift_template/1 deletes the shift_template" do
|
test "delete_shift_template/1 deletes the shift_template" do
|
||||||
shift_template = shift_template_fixture()
|
shift_template = shift_template_fixture()
|
||||||
assert {:ok, %ShiftTemplate{}} = ShiftTemplates.delete_shift_template(shift_template)
|
assert {:ok, %ShiftTemplate{}} = Templates.delete_shift_template(shift_template)
|
||||||
assert_raise Ecto.NoResultsError, fn -> ShiftTemplates.get_shift_template!(shift_template.id) end
|
|
||||||
|
assert_raise Ecto.NoResultsError, fn ->
|
||||||
|
Templates.get_shift_template!(shift_template.id)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test "change_shift_template/1 returns a shift_template changeset" do
|
test "change_shift_template/1 returns a shift_template changeset" do
|
||||||
shift_template = shift_template_fixture()
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,7 +3,7 @@ defmodule Shift73kWeb.ShiftTemplateLiveTest do
|
||||||
|
|
||||||
import Phoenix.LiveViewTest
|
import Phoenix.LiveViewTest
|
||||||
|
|
||||||
alias Shift73k.ShiftTemplates
|
alias Shift73k.Shifts.Templates
|
||||||
|
|
||||||
@create_attrs %{
|
@create_attrs %{
|
||||||
description: "some description",
|
description: "some description",
|
||||||
|
@ -31,7 +31,7 @@ defmodule Shift73kWeb.ShiftTemplateLiveTest do
|
||||||
}
|
}
|
||||||
|
|
||||||
defp fixture(:shift_template) 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
|
shift_template
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue