Autogen Yield liveviews

This commit is contained in:
2023-06-08 21:07:00 +03:00
parent 090053c312
commit 3a983516c9
13 changed files with 578 additions and 0 deletions

View File

@@ -0,0 +1,104 @@
defmodule Osuuspuutarha.Harvest do
@moduledoc """
The Harvest context.
"""
import Ecto.Query, warn: false
alias Osuuspuutarha.Repo
alias Osuuspuutarha.Harvest.Yield
@doc """
Returns the list of yields.
## Examples
iex> list_yields()
[%Yield{}, ...]
"""
def list_yields do
Repo.all(Yield)
end
@doc """
Gets a single yield.
Raises `Ecto.NoResultsError` if the Yield does not exist.
## Examples
iex> get_yield!(123)
%Yield{}
iex> get_yield!(456)
** (Ecto.NoResultsError)
"""
def get_yield!(id), do: Repo.get!(Yield, id)
@doc """
Creates a yield.
## Examples
iex> create_yield(%{field: value})
{:ok, %Yield{}}
iex> create_yield(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_yield(attrs \\ %{}) do
%Yield{}
|> Yield.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a yield.
## Examples
iex> update_yield(yield, %{field: new_value})
{:ok, %Yield{}}
iex> update_yield(yield, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_yield(%Yield{} = yield, attrs) do
yield
|> Yield.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a yield.
## Examples
iex> delete_yield(yield)
{:ok, %Yield{}}
iex> delete_yield(yield)
{:error, %Ecto.Changeset{}}
"""
def delete_yield(%Yield{} = yield) do
Repo.delete(yield)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking yield changes.
## Examples
iex> change_yield(yield)
%Ecto.Changeset{data: %Yield{}}
"""
def change_yield(%Yield{} = yield, attrs \\ %{}) do
Yield.changeset(yield, attrs)
end
end

View File

@@ -0,0 +1,20 @@
defmodule Osuuspuutarha.Harvest.Yield do
use Ecto.Schema
import Ecto.Changeset
schema "yields" do
field :amount, :decimal
field :date, :date
field :plant, Ecto.Enum, values: [:salad, :carrot, :cabbage]
field :unit, Ecto.Enum, values: [:kg, :kpl]
timestamps()
end
@doc false
def changeset(yield, attrs) do
yield
|> cast(attrs, [:date, :plant, :amount, :unit])
|> validate_required([:date, :plant, :amount, :unit])
end
end

View File

@@ -0,0 +1,55 @@
defmodule OsuuspuutarhaWeb.YieldLive.FormComponent do
use OsuuspuutarhaWeb, :live_component
alias Osuuspuutarha.Harvest
@impl true
def update(%{yield: yield} = assigns, socket) do
changeset = Harvest.change_yield(yield)
{:ok,
socket
|> assign(assigns)
|> assign(:changeset, changeset)}
end
@impl true
def handle_event("validate", %{"yield" => yield_params}, socket) do
changeset =
socket.assigns.yield
|> Harvest.change_yield(yield_params)
|> Map.put(:action, :validate)
{:noreply, assign(socket, :changeset, changeset)}
end
def handle_event("save", %{"yield" => yield_params}, socket) do
save_yield(socket, socket.assigns.action, yield_params)
end
defp save_yield(socket, :edit, yield_params) do
case Harvest.update_yield(socket.assigns.yield, yield_params) do
{:ok, _yield} ->
{:noreply,
socket
|> put_flash(:info, "Yield updated successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
end
end
defp save_yield(socket, :new, yield_params) do
case Harvest.create_yield(yield_params) do
{:ok, _yield} ->
{:noreply,
socket
|> put_flash(:info, "Yield 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,32 @@
<div>
<h2><%= @title %></h2>
<.form
let={f}
for={@changeset}
id="yield-form"
phx-target={@myself}
phx-change="validate"
phx-submit="save">
<%= label f, :date %>
<%= date_input f, :date %>
<%= error_tag f, :date %>
<%= label f, :plant %>
<%= select f, :plant, Ecto.Enum.values(Osuuspuutarha.Harvest.Yield, :plant), prompt: "Choose a value" %>
<%= error_tag f, :plant %>
<%= label f, :amount %>
<%= number_input f, :amount, step: "any" %>
<%= error_tag f, :amount %>
<%= label f, :unit %>
<%= select f, :unit, Ecto.Enum.values(Osuuspuutarha.Harvest.Yield, :unit), prompt: "Choose a value" %>
<%= error_tag f, :unit %>
<div>
<%= submit "Save", phx_disable_with: "Saving..." %>
</div>
</.form>
</div>

View File

@@ -0,0 +1,46 @@
defmodule OsuuspuutarhaWeb.YieldLive.Index do
use OsuuspuutarhaWeb, :live_view
alias Osuuspuutarha.Harvest
alias Osuuspuutarha.Harvest.Yield
@impl true
def mount(_params, _session, socket) do
{:ok, assign(socket, :yields, list_yields())}
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 Yield")
|> assign(:yield, Harvest.get_yield!(id))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Yield")
|> assign(:yield, %Yield{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Yields")
|> assign(:yield, nil)
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
yield = Harvest.get_yield!(id)
{:ok, _} = Harvest.delete_yield(yield)
{:noreply, assign(socket, :yields, list_yields())}
end
defp list_yields do
Harvest.list_yields()
end
end

View File

@@ -0,0 +1,45 @@
<h1>Listing Yields</h1>
<%= if @live_action in [:new, :edit] do %>
<.modal return_to={Routes.yield_index_path(@socket, :index)}>
<.live_component
module={OsuuspuutarhaWeb.YieldLive.FormComponent}
id={@yield.id || :new}
title={@page_title}
action={@live_action}
yield={@yield}
return_to={Routes.yield_index_path(@socket, :index)}
/>
</.modal>
<% end %>
<table>
<thead>
<tr>
<th>Date</th>
<th>Plant</th>
<th>Amount</th>
<th>Unit</th>
<th></th>
</tr>
</thead>
<tbody id="yields">
<%= for yield <- @yields do %>
<tr id={"yield-#{yield.id}"}>
<td><%= yield.date %></td>
<td><%= yield.plant %></td>
<td><%= yield.amount %></td>
<td><%= yield.unit %></td>
<td>
<span><%= live_redirect "Show", to: Routes.yield_show_path(@socket, :show, yield) %></span>
<span><%= live_patch "Edit", to: Routes.yield_index_path(@socket, :edit, yield) %></span>
<span><%= link "Delete", to: "#", phx_click: "delete", phx_value_id: yield.id, data: [confirm: "Are you sure?"] %></span>
</td>
</tr>
<% end %>
</tbody>
</table>
<span><%= live_patch "New Yield", to: Routes.yield_index_path(@socket, :new) %></span>

View File

@@ -0,0 +1,21 @@
defmodule OsuuspuutarhaWeb.YieldLive.Show do
use OsuuspuutarhaWeb, :live_view
alias Osuuspuutarha.Harvest
@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(:yield, Harvest.get_yield!(id))}
end
defp page_title(:show), do: "Show Yield"
defp page_title(:edit), do: "Edit Yield"
end

View File

@@ -0,0 +1,41 @@
<h1>Show Yield</h1>
<%= if @live_action in [:edit] do %>
<.modal return_to={Routes.yield_show_path(@socket, :show, @yield)}>
<.live_component
module={OsuuspuutarhaWeb.YieldLive.FormComponent}
id={@yield.id}
title={@page_title}
action={@live_action}
yield={@yield}
return_to={Routes.yield_show_path(@socket, :show, @yield)}
/>
</.modal>
<% end %>
<ul>
<li>
<strong>Date:</strong>
<%= @yield.date %>
</li>
<li>
<strong>Plant:</strong>
<%= @yield.plant %>
</li>
<li>
<strong>Amount:</strong>
<%= @yield.amount %>
</li>
<li>
<strong>Unit:</strong>
<%= @yield.unit %>
</li>
</ul>
<span><%= live_patch "Edit", to: Routes.yield_show_path(@socket, :edit, @yield), class: "button" %></span> |
<span><%= live_redirect "Back", to: Routes.yield_index_path(@socket, :index) %></span>

View File

@@ -45,6 +45,13 @@ defmodule OsuuspuutarhaWeb.Router do
live "/tilaukset/:id", OrderLive.Show, :show live "/tilaukset/:id", OrderLive.Show, :show
live "/tilaukset/:id/nayta/muokkaa", OrderLive.Show, :edit live "/tilaukset/:id/nayta/muokkaa", OrderLive.Show, :edit
live "/korjuut", YieldLive.Index, :index
live "/korjuut/uusi", YieldLive.Index, :new
live "/korjuut/:id/muokkaa", YieldLive.Index, :edit
live "/korjuut/:id", YieldLive.Show, :show
live "/korjuut/:id/nayta/muokkaa", YieldLive.Show, :edit
end end
scope "/lataukset", as: :exports, alias: OsuuspuutarhaWeb.Exports do scope "/lataukset", as: :exports, alias: OsuuspuutarhaWeb.Exports do

View File

@@ -0,0 +1,14 @@
defmodule Osuuspuutarha.Repo.Migrations.CreateYields do
use Ecto.Migration
def change do
create table(:yields) do
add :date, :date
add :plant, :string
add :amount, :decimal
add :unit, :string
timestamps()
end
end
end

View File

@@ -0,0 +1,65 @@
defmodule Osuuspuutarha.HarvestTest do
use Osuuspuutarha.DataCase
alias Osuuspuutarha.Harvest
describe "yields" do
alias Osuuspuutarha.Harvest.Yield
import Osuuspuutarha.HarvestFixtures
@invalid_attrs %{amount: nil, date: nil, plant: nil, unit: nil}
test "list_yields/0 returns all yields" do
yield = yield_fixture()
assert Harvest.list_yields() == [yield]
end
test "get_yield!/1 returns the yield with given id" do
yield = yield_fixture()
assert Harvest.get_yield!(yield.id) == yield
end
test "create_yield/1 with valid data creates a yield" do
valid_attrs = %{amount: "120.5", date: ~D[2023-06-07], plant: :salad, unit: :kg}
assert {:ok, %Yield{} = yield} = Harvest.create_yield(valid_attrs)
assert yield.amount == Decimal.new("120.5")
assert yield.date == ~D[2023-06-07]
assert yield.plant == :salad
assert yield.unit == :kg
end
test "create_yield/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Harvest.create_yield(@invalid_attrs)
end
test "update_yield/2 with valid data updates the yield" do
yield = yield_fixture()
update_attrs = %{amount: "456.7", date: ~D[2023-06-08], plant: :carrot, unit: :kpl}
assert {:ok, %Yield{} = yield} = Harvest.update_yield(yield, update_attrs)
assert yield.amount == Decimal.new("456.7")
assert yield.date == ~D[2023-06-08]
assert yield.plant == :carrot
assert yield.unit == :kpl
end
test "update_yield/2 with invalid data returns error changeset" do
yield = yield_fixture()
assert {:error, %Ecto.Changeset{}} = Harvest.update_yield(yield, @invalid_attrs)
assert yield == Harvest.get_yield!(yield.id)
end
test "delete_yield/1 deletes the yield" do
yield = yield_fixture()
assert {:ok, %Yield{}} = Harvest.delete_yield(yield)
assert_raise Ecto.NoResultsError, fn -> Harvest.get_yield!(yield.id) end
end
test "change_yield/1 returns a yield changeset" do
yield = yield_fixture()
assert %Ecto.Changeset{} = Harvest.change_yield(yield)
end
end
end

View File

@@ -0,0 +1,105 @@
defmodule OsuuspuutarhaWeb.YieldLiveTest do
use OsuuspuutarhaWeb.ConnCase
import Phoenix.LiveViewTest
import Osuuspuutarha.HarvestFixtures
@create_attrs %{amount: "120.5", date: %{day: 7, month: 6, year: 2023}, plant: :salad, unit: :kg}
@update_attrs %{amount: "456.7", date: %{day: 8, month: 6, year: 2023}, plant: :carrot, unit: :kpl}
@invalid_attrs %{amount: nil, date: %{day: 30, month: 2, year: 2023}, plant: nil, unit: nil}
defp create_yield(_) do
yield = yield_fixture()
%{yield: yield}
end
describe "Index" do
setup [:create_yield]
test "lists all yields", %{conn: conn} do
{:ok, _index_live, html} = live(conn, Routes.yield_index_path(conn, :index))
assert html =~ "Listing Yields"
end
test "saves new yield", %{conn: conn} do
{:ok, index_live, _html} = live(conn, Routes.yield_index_path(conn, :index))
assert index_live |> element("a", "New Yield") |> render_click() =~
"New Yield"
assert_patch(index_live, Routes.yield_index_path(conn, :new))
assert index_live
|> form("#yield-form", yield: @invalid_attrs)
|> render_change() =~ "is invalid"
{:ok, _, html} =
index_live
|> form("#yield-form", yield: @create_attrs)
|> render_submit()
|> follow_redirect(conn, Routes.yield_index_path(conn, :index))
assert html =~ "Yield created successfully"
end
test "updates yield in listing", %{conn: conn, yield: yield} do
{:ok, index_live, _html} = live(conn, Routes.yield_index_path(conn, :index))
assert index_live |> element("#yield-#{yield.id} a", "Edit") |> render_click() =~
"Edit Yield"
assert_patch(index_live, Routes.yield_index_path(conn, :edit, yield))
assert index_live
|> form("#yield-form", yield: @invalid_attrs)
|> render_change() =~ "is invalid"
{:ok, _, html} =
index_live
|> form("#yield-form", yield: @update_attrs)
|> render_submit()
|> follow_redirect(conn, Routes.yield_index_path(conn, :index))
assert html =~ "Yield updated successfully"
end
test "deletes yield in listing", %{conn: conn, yield: yield} do
{:ok, index_live, _html} = live(conn, Routes.yield_index_path(conn, :index))
assert index_live |> element("#yield-#{yield.id} a", "Delete") |> render_click()
refute has_element?(index_live, "#yield-#{yield.id}")
end
end
describe "Show" do
setup [:create_yield]
test "displays yield", %{conn: conn, yield: yield} do
{:ok, _show_live, html} = live(conn, Routes.yield_show_path(conn, :show, yield))
assert html =~ "Show Yield"
end
test "updates yield within modal", %{conn: conn, yield: yield} do
{:ok, show_live, _html} = live(conn, Routes.yield_show_path(conn, :show, yield))
assert show_live |> element("a", "Edit") |> render_click() =~
"Edit Yield"
assert_patch(show_live, Routes.yield_show_path(conn, :edit, yield))
assert show_live
|> form("#yield-form", yield: @invalid_attrs)
|> render_change() =~ "is invalid"
{:ok, _, html} =
show_live
|> form("#yield-form", yield: @update_attrs)
|> render_submit()
|> follow_redirect(conn, Routes.yield_show_path(conn, :show, yield))
assert html =~ "Yield updated successfully"
end
end
end

View File

@@ -0,0 +1,23 @@
defmodule Osuuspuutarha.HarvestFixtures do
@moduledoc """
This module defines test helpers for creating
entities via the `Osuuspuutarha.Harvest` context.
"""
@doc """
Generate a yield.
"""
def yield_fixture(attrs \\ %{}) do
{:ok, yield} =
attrs
|> Enum.into(%{
amount: "120.5",
date: ~D[2023-06-07],
plant: :salad,
unit: :kg
})
|> Osuuspuutarha.Harvest.create_yield()
yield
end
end