some reorganization for inserting multiple shifts, setting up dev seeds

This commit is contained in:
Adam Piontek 2021-03-21 19:27:25 -04:00
commit 4c673508b5
8 changed files with 95 additions and 61 deletions

View file

@ -4,4 +4,9 @@ defmodule Shift73k.Repo do
adapter: Ecto.Adapters.Postgres
use Scrivener, page_size: 10
def timestamp(%{} = attrs) do
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
Map.merge(attrs, %{inserted_at: now, updated_at: now})
end
end

View file

@ -87,6 +87,14 @@ defmodule Shift73k.Shifts do
|> Repo.insert()
end
def create_multiple(shift_attrs) when is_list(shift_attrs) do
try do
Repo.insert_all(Shift, shift_attrs)
rescue
e in Postgrex.Error -> {:error, e.message}
end
end
@doc """
Updates a shift.

View file

@ -2,6 +2,8 @@ defmodule Shift73k.Shifts.Shift do
use Ecto.Schema
import Ecto.Changeset
alias Shift73k.Shifts.Templates.ShiftTemplate
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "shifts" do
@ -31,14 +33,13 @@ defmodule Shift73k.Shifts.Shift do
:time_end,
:user_id
])
# |> validate_required([
# :subject,
# :date,
# :time_zone,
# :time_start,
# :time_end,
# :user_id
# ])
|> validate_required([
:subject,
:date,
:time_zone,
:time_start,
:time_end,
:user_id
])
end
end

View file

@ -21,13 +21,11 @@ defmodule Shift73k.Shifts.Templates do
Repo.all(ShiftTemplate)
end
def list_shift_templates_by_user_id(user_id) do
q =
from s in ShiftTemplate,
where: s.user_id == ^user_id,
order_by: [fragment("lower(?)", s.subject), s.time_start]
Repo.all(q)
def list_shift_templates_by_user(user_id) do
from(s in ShiftTemplate)
|> where([s], s.user_id == ^user_id)
|> order_by([s], [fragment("lower(?)", s.subject), s.time_start])
|> Repo.all()
end
@doc """

View file

@ -77,6 +77,14 @@ defmodule Shift73k.Shifts.Templates.ShiftTemplate do
def shift_length(len_min) when is_integer(len_min) and len_min >= 0, do: len_min
def shift_length(len_min) when is_integer(len_min) and len_min < 0, do: 1440 + len_min
def shift_length(time_end, time_start),
do: shift_length(%ShiftTemplate{time_end: time_end, time_start: time_start})
def shift_length(time_end, time_start) do
shift_length(%ShiftTemplate{time_end: time_end, time_start: time_start})
end
# Get shift attrs from shift template
def attrs(%ShiftTemplate{} = shift_template) do
shift_template
|> Map.from_struct()
|> Map.drop([:__meta__, :id, :inserted_at, :updated_at, :user, :is_fave_of_user])
end
end