initial liveview/context/schema generation for Shifts
This commit is contained in:
parent
c864ff9563
commit
290475c101
15 changed files with 648 additions and 6 deletions
lib/shift73k
|
@ -4,6 +4,7 @@ defmodule Shift73k.Accounts.User do
|
|||
import EctoEnum
|
||||
|
||||
alias Shift73k.Shifts.Templates.ShiftTemplate
|
||||
alias Shift73k.Shifts.Shift
|
||||
|
||||
@roles [
|
||||
user: "Basic user level",
|
||||
|
@ -31,6 +32,8 @@ defmodule Shift73k.Accounts.User do
|
|||
has_many(:shift_templates, ShiftTemplate)
|
||||
belongs_to(:fave_shift_template, ShiftTemplate)
|
||||
|
||||
has_many(:shifts, Shift)
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
|
|
104
lib/shift73k/shifts.ex
Normal file
104
lib/shift73k/shifts.ex
Normal file
|
@ -0,0 +1,104 @@
|
|||
defmodule Shift73k.Shifts do
|
||||
@moduledoc """
|
||||
The Shifts context.
|
||||
"""
|
||||
|
||||
import Ecto.Query, warn: false
|
||||
alias Shift73k.Repo
|
||||
|
||||
alias Shift73k.Shifts.Shift
|
||||
|
||||
@doc """
|
||||
Returns the list of shifts.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> list_shifts()
|
||||
[%Shift{}, ...]
|
||||
|
||||
"""
|
||||
def list_shifts do
|
||||
Repo.all(Shift)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a single shift.
|
||||
|
||||
Raises `Ecto.NoResultsError` if the Shift does not exist.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_shift!(123)
|
||||
%Shift{}
|
||||
|
||||
iex> get_shift!(456)
|
||||
** (Ecto.NoResultsError)
|
||||
|
||||
"""
|
||||
def get_shift!(id), do: Repo.get!(Shift, id)
|
||||
|
||||
@doc """
|
||||
Creates a shift.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> create_shift(%{field: value})
|
||||
{:ok, %Shift{}}
|
||||
|
||||
iex> create_shift(%{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def create_shift(attrs \\ %{}) do
|
||||
%Shift{}
|
||||
|> Shift.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates a shift.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> update_shift(shift, %{field: new_value})
|
||||
{:ok, %Shift{}}
|
||||
|
||||
iex> update_shift(shift, %{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def update_shift(%Shift{} = shift, attrs) do
|
||||
shift
|
||||
|> Shift.changeset(attrs)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a shift.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> delete_shift(shift)
|
||||
{:ok, %Shift{}}
|
||||
|
||||
iex> delete_shift(shift)
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def delete_shift(%Shift{} = shift) do
|
||||
Repo.delete(shift)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an `%Ecto.Changeset{}` for tracking shift changes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_shift(shift)
|
||||
%Ecto.Changeset{data: %Shift{}}
|
||||
|
||||
"""
|
||||
def change_shift(%Shift{} = shift, attrs \\ %{}) do
|
||||
Shift.changeset(shift, attrs)
|
||||
end
|
||||
end
|
44
lib/shift73k/shifts/shift.ex
Normal file
44
lib/shift73k/shifts/shift.ex
Normal file
|
@ -0,0 +1,44 @@
|
|||
defmodule Shift73k.Shifts.Shift do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
@primary_key {:id, :binary_id, autogenerate: true}
|
||||
@foreign_key_type :binary_id
|
||||
schema "shifts" do
|
||||
field :subject, :string
|
||||
field :description, :string
|
||||
field :location, :string
|
||||
field :date, :date
|
||||
field :time_zone, :string
|
||||
field :time_start, :time
|
||||
field :time_end, :time
|
||||
|
||||
belongs_to(:user, Shift73k.Accounts.User)
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
@doc false
|
||||
def changeset(shift, attrs) do
|
||||
shift
|
||||
|> cast(attrs, [
|
||||
:subject,
|
||||
:location,
|
||||
:description,
|
||||
:date,
|
||||
:time_zone,
|
||||
:time_start,
|
||||
:time_end,
|
||||
:user_id
|
||||
])
|
||||
|
||||
# |> validate_required([
|
||||
# :subject,
|
||||
# :date,
|
||||
# :time_zone,
|
||||
# :time_start,
|
||||
# :time_end,
|
||||
# :user_id
|
||||
# ])
|
||||
end
|
||||
end
|
|
@ -36,7 +36,8 @@ defmodule Shift73k.Shifts.Templates.ShiftTemplate do
|
|||
:subject,
|
||||
:time_zone,
|
||||
:time_start,
|
||||
:time_end
|
||||
:time_end,
|
||||
:user_id
|
||||
])
|
||||
|> validate_length(:subject, count: :codepoints, max: 280)
|
||||
|> validate_length(:location, count: :codepoints, max: 280)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue