initial liveview/context/schema generation for Shifts

This commit is contained in:
Adam Piontek 2021-03-11 17:16:11 -05:00
parent c864ff9563
commit 290475c101
15 changed files with 648 additions and 6 deletions

View file

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

View 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

View file

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

View file

@ -0,0 +1,55 @@
defmodule Shift73kWeb.ShiftLive.FormComponent do
use Shift73kWeb, :live_component
alias Shift73k.Shifts
@impl true
def update(%{shift: shift} = assigns, socket) do
changeset = Shifts.change_shift(shift)
{:ok,
socket
|> assign(assigns)
|> assign(:changeset, changeset)}
end
@impl true
def handle_event("validate", %{"shift" => shift_params}, socket) do
changeset =
socket.assigns.shift
|> Shifts.change_shift(shift_params)
|> Map.put(:action, :validate)
{:noreply, assign(socket, :changeset, changeset)}
end
def handle_event("save", %{"shift" => shift_params}, socket) do
save_shift(socket, socket.assigns.action, shift_params)
end
defp save_shift(socket, :edit, shift_params) do
case Shifts.update_shift(socket.assigns.shift, shift_params) do
{:ok, _shift} ->
{:noreply,
socket
|> put_flash(:info, "Shift updated successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
end
end
defp save_shift(socket, :new, shift_params) do
case Shifts.create_shift(shift_params) do
{:ok, _shift} ->
{:noreply,
socket
|> put_flash(:info, "Shift created successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)}
end
end
end

View file

@ -0,0 +1,42 @@
<h2><%= @title %></h2>
<%= f = form_for @changeset, "#",
id: "shift-form",
phx_target: @myself,
phx_change: "validate",
phx_submit: "save" %>
<%= label f, :subject %>
<%= text_input f, :subject %>
<%= error_tag f, :subject %>
<%= label f, :location %>
<%= text_input f, :location %>
<%= error_tag f, :location %>
<%= label f, :description %>
<%= textarea f, :description %>
<%= error_tag f, :description %>
<%= label f, :time_zone %>
<%= text_input f, :time_zone %>
<%= error_tag f, :time_zone %>
<%= label f, :date %>
<%= date_select f, :date %>
<%= error_tag f, :date %>
<%= label f, :time_zone %>
<%= text_input f, :time_zone %>
<%= error_tag f, :time_zone %>
<%= label f, :time_start %>
<%= time_select f, :time_start %>
<%= error_tag f, :time_start %>
<%= label f, :time_end %>
<%= time_select f, :time_end %>
<%= error_tag f, :time_end %>
<%= submit "Save", phx_disable_with: "Saving..." %>
</form>

View file

@ -0,0 +1,46 @@
defmodule Shift73kWeb.ShiftLive.Index do
use Shift73kWeb, :live_view
alias Shift73k.Shifts
alias Shift73k.Shifts.Shift
@impl true
def mount(_params, _session, socket) do
{:ok, assign(socket, :shifts, list_shifts())}
end
@impl true
def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end
defp apply_action(socket, :edit, %{"id" => id}) do
socket
|> assign(:page_title, "Edit Shift")
|> assign(:shift, Shifts.get_shift!(id))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Shift")
|> assign(:shift, %Shift{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Shifts")
|> assign(:shift, nil)
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
shift = Shifts.get_shift!(id)
{:ok, _} = Shifts.delete_shift(shift)
{:noreply, assign(socket, :shifts, list_shifts())}
end
defp list_shifts do
Shifts.list_shifts()
end
end

View file

@ -0,0 +1,49 @@
<h1>Listing Shifts</h1>
<%= if @live_action in [:new, :edit] do %>
<%= live_modal @socket, Shift73kWeb.ShiftLive.FormComponent,
id: @shift.id || :new,
title: @page_title,
action: @live_action,
shift: @shift,
return_to: Routes.shift_index_path(@socket, :index) %>
<% end %>
<table>
<thead>
<tr>
<th>Subject</th>
<th>Location</th>
<th>Description</th>
<th>Time zone</th>
<th>Date</th>
<th>Time zone</th>
<th>Time start</th>
<th>Time end</th>
<th></th>
</tr>
</thead>
<tbody id="shifts">
<%= for shift <- @shifts do %>
<tr id="shift-<%= shift.id %>">
<td><%= shift.subject %></td>
<td><%= shift.location %></td>
<td><%= shift.description %></td>
<td><%= shift.time_zone %></td>
<td><%= shift.date %></td>
<td><%= shift.time_zone %></td>
<td><%= shift.time_start %></td>
<td><%= shift.time_end %></td>
<td>
<span><%= live_redirect "Show", to: Routes.shift_show_path(@socket, :show, shift) %></span>
<span><%= live_patch "Edit", to: Routes.shift_index_path(@socket, :edit, shift) %></span>
<span><%= link "Delete", to: "#", phx_click: "delete", phx_value_id: shift.id, data: [confirm: "Are you sure?"] %></span>
</td>
</tr>
<% end %>
</tbody>
</table>
<span><%= live_patch "New Shift", to: Routes.shift_index_path(@socket, :new) %></span>

View file

@ -0,0 +1,21 @@
defmodule Shift73kWeb.ShiftLive.Show do
use Shift73kWeb, :live_view
alias Shift73k.Shifts
@impl true
def mount(_params, _session, socket) do
{:ok, socket}
end
@impl true
def handle_params(%{"id" => id}, _, socket) do
{:noreply,
socket
|> assign(:page_title, page_title(socket.assigns.live_action))
|> assign(:shift, Shifts.get_shift!(id))}
end
defp page_title(:show), do: "Show Shift"
defp page_title(:edit), do: "Edit Shift"
end

View file

@ -0,0 +1,57 @@
<h1>Show Shift</h1>
<%= if @live_action in [:edit] do %>
<%= live_modal @socket, Shift73kWeb.ShiftLive.FormComponent,
id: @shift.id,
title: @page_title,
action: @live_action,
shift: @shift,
return_to: Routes.shift_show_path(@socket, :show, @shift) %>
<% end %>
<ul>
<li>
<strong>Subject:</strong>
<%= @shift.subject %>
</li>
<li>
<strong>Location:</strong>
<%= @shift.location %>
</li>
<li>
<strong>Description:</strong>
<%= @shift.description %>
</li>
<li>
<strong>Time zone:</strong>
<%= @shift.time_zone %>
</li>
<li>
<strong>Date:</strong>
<%= @shift.date %>
</li>
<li>
<strong>Time zone:</strong>
<%= @shift.time_zone %>
</li>
<li>
<strong>Time start:</strong>
<%= @shift.time_start %>
</li>
<li>
<strong>Time end:</strong>
<%= @shift.time_end %>
</li>
</ul>
<span><%= live_patch "Edit", to: Routes.shift_show_path(@socket, :edit, @shift), class: "button" %></span>
<span><%= live_redirect "Back", to: Routes.shift_index_path(@socket, :index) %></span>

View file

@ -91,10 +91,17 @@ defmodule Shift73kWeb.Router do
scope "/", Shift73kWeb do
pipe_through([:browser, :require_authenticated_user, :user])
live "/my_shifts", ShiftTemplateLive.Index, :index
live "/my_shifts/new", ShiftTemplateLive.Index, :new
live "/my_shifts/:id/edit", ShiftTemplateLive.Index, :edit
live "/my_shifts/:id/clone", ShiftTemplateLive.Index, :clone
live "/templates", ShiftTemplateLive.Index, :index
live "/templates/new", ShiftTemplateLive.Index, :new
live "/templates/:id/edit", ShiftTemplateLive.Index, :edit
live "/templates/:id/clone", ShiftTemplateLive.Index, :clone
live "/shifts", ShiftLive.Index, :index
live "/shifts/new", ShiftLive.Index, :new
live "/shifts/:id/edit", ShiftLive.Index, :edit
live "/shifts/:id", ShiftLive.Show, :show
live "/shifts/:id/show/edit", ShiftLive.Show, :edit
end
# scope "/", Shift73kWeb do

View file

@ -15,6 +15,6 @@ defmodule Shift73k.Repo.Migrations.CreateShiftTemplates do
timestamps()
end
create index(:shift_templates, [:user_id])
create index(:shift_templates, [:user_id, :subject])
end
end

View file

@ -0,0 +1,21 @@
defmodule Shift73k.Repo.Migrations.CreateShifts do
use Ecto.Migration
def change do
create table(:shifts, primary_key: false) do
add :id, :binary_id, primary_key: true
add :subject, :string, size: 280, null: false
add :location, :string, size: 280
add :description, :text
add :date, :date, null: false
add :time_zone, :string, null: false
add :time_start, :time, null: false
add :time_end, :time, null: false
add :user_id, references(:users, on_delete: :nothing, type: :binary_id)
timestamps()
end
create index(:shifts, [:user_id, :subject])
end
end

View file

@ -0,0 +1,76 @@
defmodule Shift73k.ShiftsTest do
use Shift73k.DataCase
alias Shift73k.Shifts
describe "shifts" do
alias Shift73k.Shifts.Shift
@valid_attrs %{date: ~D[2010-04-17], description: "some description", location: "some location", subject: "some subject", time_end: ~T[14:00:00], time_start: ~T[14:00:00], time_zone: "some time_zone"}
@update_attrs %{date: ~D[2011-05-18], description: "some updated description", location: "some updated location", subject: "some updated subject", time_end: ~T[15:01:01], time_start: ~T[15:01:01], time_zone: "some updated time_zone"}
@invalid_attrs %{date: nil, description: nil, location: nil, subject: nil, time_end: nil, time_start: nil, time_zone: nil}
def shift_fixture(attrs \\ %{}) do
{:ok, shift} =
attrs
|> Enum.into(@valid_attrs)
|> Shifts.create_shift()
shift
end
test "list_shifts/0 returns all shifts" do
shift = shift_fixture()
assert Shifts.list_shifts() == [shift]
end
test "get_shift!/1 returns the shift with given id" do
shift = shift_fixture()
assert Shifts.get_shift!(shift.id) == shift
end
test "create_shift/1 with valid data creates a shift" do
assert {:ok, %Shift{} = shift} = Shifts.create_shift(@valid_attrs)
assert shift.date == ~D[2010-04-17]
assert shift.description == "some description"
assert shift.location == "some location"
assert shift.subject == "some subject"
assert shift.time_end == ~T[14:00:00]
assert shift.time_start == ~T[14:00:00]
assert shift.time_zone == "some time_zone"
end
test "create_shift/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Shifts.create_shift(@invalid_attrs)
end
test "update_shift/2 with valid data updates the shift" do
shift = shift_fixture()
assert {:ok, %Shift{} = shift} = Shifts.update_shift(shift, @update_attrs)
assert shift.date == ~D[2011-05-18]
assert shift.description == "some updated description"
assert shift.location == "some updated location"
assert shift.subject == "some updated subject"
assert shift.time_end == ~T[15:01:01]
assert shift.time_start == ~T[15:01:01]
assert shift.time_zone == "some updated time_zone"
end
test "update_shift/2 with invalid data returns error changeset" do
shift = shift_fixture()
assert {:error, %Ecto.Changeset{}} = Shifts.update_shift(shift, @invalid_attrs)
assert shift == Shifts.get_shift!(shift.id)
end
test "delete_shift/1 deletes the shift" do
shift = shift_fixture()
assert {:ok, %Shift{}} = Shifts.delete_shift(shift)
assert_raise Ecto.NoResultsError, fn -> Shifts.get_shift!(shift.id) end
end
test "change_shift/1 returns a shift changeset" do
shift = shift_fixture()
assert %Ecto.Changeset{} = Shifts.change_shift(shift)
end
end
end

View file

@ -0,0 +1,116 @@
defmodule Shift73kWeb.ShiftLiveTest do
use Shift73kWeb.ConnCase
import Phoenix.LiveViewTest
alias Shift73k.Shifts
@create_attrs %{date: ~D[2010-04-17], description: "some description", location: "some location", subject: "some subject", time_end: ~T[14:00:00], time_start: ~T[14:00:00], time_zone: "some time_zone"}
@update_attrs %{date: ~D[2011-05-18], description: "some updated description", location: "some updated location", subject: "some updated subject", time_end: ~T[15:01:01], time_start: ~T[15:01:01], time_zone: "some updated time_zone"}
@invalid_attrs %{date: nil, description: nil, location: nil, subject: nil, time_end: nil, time_start: nil, time_zone: nil}
defp fixture(:shift) do
{:ok, shift} = Shifts.create_shift(@create_attrs)
shift
end
defp create_shift(_) do
shift = fixture(:shift)
%{shift: shift}
end
describe "Index" do
setup [:create_shift]
test "lists all shifts", %{conn: conn, shift: shift} do
{:ok, _index_live, html} = live(conn, Routes.shift_index_path(conn, :index))
assert html =~ "Listing Shifts"
assert html =~ shift.description
end
test "saves new shift", %{conn: conn} do
{:ok, index_live, _html} = live(conn, Routes.shift_index_path(conn, :index))
assert index_live |> element("a", "New Shift") |> render_click() =~
"New Shift"
assert_patch(index_live, Routes.shift_index_path(conn, :new))
assert index_live
|> form("#shift-form", shift: @invalid_attrs)
|> render_change() =~ "can&apos;t be blank"
{:ok, _, html} =
index_live
|> form("#shift-form", shift: @create_attrs)
|> render_submit()
|> follow_redirect(conn, Routes.shift_index_path(conn, :index))
assert html =~ "Shift created successfully"
assert html =~ "some description"
end
test "updates shift in listing", %{conn: conn, shift: shift} do
{:ok, index_live, _html} = live(conn, Routes.shift_index_path(conn, :index))
assert index_live |> element("#shift-#{shift.id} a", "Edit") |> render_click() =~
"Edit Shift"
assert_patch(index_live, Routes.shift_index_path(conn, :edit, shift))
assert index_live
|> form("#shift-form", shift: @invalid_attrs)
|> render_change() =~ "can&apos;t be blank"
{:ok, _, html} =
index_live
|> form("#shift-form", shift: @update_attrs)
|> render_submit()
|> follow_redirect(conn, Routes.shift_index_path(conn, :index))
assert html =~ "Shift updated successfully"
assert html =~ "some updated description"
end
test "deletes shift in listing", %{conn: conn, shift: shift} do
{:ok, index_live, _html} = live(conn, Routes.shift_index_path(conn, :index))
assert index_live |> element("#shift-#{shift.id} a", "Delete") |> render_click()
refute has_element?(index_live, "#shift-#{shift.id}")
end
end
describe "Show" do
setup [:create_shift]
test "displays shift", %{conn: conn, shift: shift} do
{:ok, _show_live, html} = live(conn, Routes.shift_show_path(conn, :show, shift))
assert html =~ "Show Shift"
assert html =~ shift.description
end
test "updates shift within modal", %{conn: conn, shift: shift} do
{:ok, show_live, _html} = live(conn, Routes.shift_show_path(conn, :show, shift))
assert show_live |> element("a", "Edit") |> render_click() =~
"Edit Shift"
assert_patch(show_live, Routes.shift_show_path(conn, :edit, shift))
assert show_live
|> form("#shift-form", shift: @invalid_attrs)
|> render_change() =~ "can&apos;t be blank"
{:ok, _, html} =
show_live
|> form("#shift-form", shift: @update_attrs)
|> render_submit()
|> follow_redirect(conn, Routes.shift_show_path(conn, :show, shift))
assert html =~ "Shift updated successfully"
assert html =~ "some updated description"
end
end
end