shift template live crud working

This commit is contained in:
Adam Piontek 2021-03-11 13:30:30 -05:00
commit 8aa4b8eee0
14 changed files with 9279 additions and 1123 deletions
lib/shift73k

View file

@ -22,8 +22,8 @@ defmodule Shift73k.ShiftTemplates do
end
def list_shift_templates_by_user_id(user_id) do
from(s in ShiftTemplate, where: s.user_id == ^user_id)
|> Repo.all()
q = from s in ShiftTemplate, where: s.user_id == ^user_id, order_by: s.subject
Repo.all(q)
end
@doc """

View file

@ -8,13 +8,12 @@ defmodule Shift73k.ShiftTemplates.ShiftTemplate do
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "shift_templates" do
field :subject, :string, default: "My Work Shift"
field :subject, :string
field :description, :string
field :location, :string
field :timezone, :string, default: @time_zone
field :start_time, :time, default: ~T[09:00:00]
field :length_hours, :integer, default: 8
field :length_minutes, :integer, default: 0
field :time_zone, :string, default: @time_zone
field :time_start, :time, default: ~T[09:00:00]
field :time_end, :time, default: ~T[17:00:00]
belongs_to(:user, Shift73k.Accounts.User)
@ -26,22 +25,61 @@ defmodule Shift73k.ShiftTemplates.ShiftTemplate do
shift_template
|> cast(attrs, [
:subject,
:description,
:location,
:timezone,
:start_time,
:length_hours,
:length_minutes,
:description,
:time_zone,
:time_start,
:time_end,
:user_id
])
|> validate_required([
:subject,
:timezone,
:start_time,
:length_hours
:time_zone,
:time_start,
:time_end
])
|> validate_number(:length_hours, greater_than_or_equal_to: 0, less_than_or_equal_to: 24)
|> validate_number(:length_minutes, greater_than_or_equal_to: 0, less_than: 60)
|> validate_inclusion(:timezone, Tzdata.zone_list())
|> validate_length(:subject, count: :codepoints, max: 280)
|> validate_length(:location, count: :codepoints, max: 280)
|> validate_change(:time_end, fn :time_end, time_end ->
shift_length = shift_length(get_time_start(attrs), time_end)
cond do
shift_length == 0 ->
[time_end: "end time cannot equal start time"]
shift_length >= 16 * 3600 ->
[time_end: "you don't want to work 16 or more hours!"]
true ->
[]
end
end)
|> validate_inclusion(:time_zone, Tzdata.zone_list())
end
defp get_time_start(%{"time_start" => time_start}), do: time_start
defp get_time_start(%{time_start: time_start}), do: time_start
defp get_time_start(_), do: nil
def shift_length(time_start, time_end) do
cond do
time_end > time_start ->
Time.diff(time_end, time_start)
time_start > time_end ->
len1 = Time.diff(~T[23:59:59], time_start) + 1
len2 = Time.diff(time_end, ~T[00:00:00])
len1 + len2
true ->
0
end
end
def shift_length_h_m_tuple(time_start, time_end) do
shift_length_seconds = shift_length(time_start, time_end)
h = shift_length_seconds |> Integer.floor_div(3600)
m = shift_length_seconds |> rem(3600) |> Integer.floor_div(60)
{h, m}
end
end