reorganized shift templates files

This commit is contained in:
Adam Piontek 2021-03-11 16:57:20 -05:00
parent 243ebd8aa2
commit e0f8515566
13 changed files with 80 additions and 49 deletions

View file

@ -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

View file

@ -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

View file

@ -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",

View file

@ -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.

View file

@ -1,4 +1,4 @@
defmodule Shift73k.ShiftTemplates.ShiftTemplate do
defmodule Shift73k.Shifts.Templates.ShiftTemplate do
use Ecto.Schema
import Ecto.Changeset

View file

@ -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}\""}

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -1,4 +1,4 @@
defmodule Shift73k.Repo.Migrations.CreateShiftTemplates do
defmodule Shift73k.Repo.Migrations.CreateShifts.Templates do
use Ecto.Migration
def change do

View file

@ -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!()

View file

@ -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

View file

@ -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