From 3a983516c9da1122e87263d3f3210668529cdcd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Veikko=20Lintuj=C3=A4rvi?= Date: Thu, 8 Jun 2023 21:07:00 +0300 Subject: [PATCH] Autogen Yield liveviews --- lib/osuuspuutarha/harvest.ex | 104 +++++++++++++++++ lib/osuuspuutarha/harvest/yield.ex | 20 ++++ .../live/yield_live/form_component.ex | 55 +++++++++ .../live/yield_live/form_component.html.heex | 32 ++++++ .../live/yield_live/index.ex | 46 ++++++++ .../live/yield_live/index.html.heex | 45 ++++++++ lib/osuuspuutarha_web/live/yield_live/show.ex | 21 ++++ .../live/yield_live/show.html.heex | 41 +++++++ lib/osuuspuutarha_web/router.ex | 7 ++ .../20230608175223_create_yields.exs | 14 +++ test/osuuspuutarha/harvest_test.exs | 65 +++++++++++ .../live/yield_live_test.exs | 105 ++++++++++++++++++ test/support/fixtures/harvest_fixtures.ex | 23 ++++ 13 files changed, 578 insertions(+) create mode 100644 lib/osuuspuutarha/harvest.ex create mode 100644 lib/osuuspuutarha/harvest/yield.ex create mode 100644 lib/osuuspuutarha_web/live/yield_live/form_component.ex create mode 100644 lib/osuuspuutarha_web/live/yield_live/form_component.html.heex create mode 100644 lib/osuuspuutarha_web/live/yield_live/index.ex create mode 100644 lib/osuuspuutarha_web/live/yield_live/index.html.heex create mode 100644 lib/osuuspuutarha_web/live/yield_live/show.ex create mode 100644 lib/osuuspuutarha_web/live/yield_live/show.html.heex create mode 100644 priv/repo/migrations/20230608175223_create_yields.exs create mode 100644 test/osuuspuutarha/harvest_test.exs create mode 100644 test/osuuspuutarha_web/live/yield_live_test.exs create mode 100644 test/support/fixtures/harvest_fixtures.ex diff --git a/lib/osuuspuutarha/harvest.ex b/lib/osuuspuutarha/harvest.ex new file mode 100644 index 0000000..959e438 --- /dev/null +++ b/lib/osuuspuutarha/harvest.ex @@ -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 diff --git a/lib/osuuspuutarha/harvest/yield.ex b/lib/osuuspuutarha/harvest/yield.ex new file mode 100644 index 0000000..cee1c4a --- /dev/null +++ b/lib/osuuspuutarha/harvest/yield.ex @@ -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 diff --git a/lib/osuuspuutarha_web/live/yield_live/form_component.ex b/lib/osuuspuutarha_web/live/yield_live/form_component.ex new file mode 100644 index 0000000..0f820dd --- /dev/null +++ b/lib/osuuspuutarha_web/live/yield_live/form_component.ex @@ -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 diff --git a/lib/osuuspuutarha_web/live/yield_live/form_component.html.heex b/lib/osuuspuutarha_web/live/yield_live/form_component.html.heex new file mode 100644 index 0000000..d8770a2 --- /dev/null +++ b/lib/osuuspuutarha_web/live/yield_live/form_component.html.heex @@ -0,0 +1,32 @@ +
+

<%= @title %>

+ + <.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 %> + +
+ <%= submit "Save", phx_disable_with: "Saving..." %> +
+ +
diff --git a/lib/osuuspuutarha_web/live/yield_live/index.ex b/lib/osuuspuutarha_web/live/yield_live/index.ex new file mode 100644 index 0000000..3931a10 --- /dev/null +++ b/lib/osuuspuutarha_web/live/yield_live/index.ex @@ -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 diff --git a/lib/osuuspuutarha_web/live/yield_live/index.html.heex b/lib/osuuspuutarha_web/live/yield_live/index.html.heex new file mode 100644 index 0000000..3dc1fa3 --- /dev/null +++ b/lib/osuuspuutarha_web/live/yield_live/index.html.heex @@ -0,0 +1,45 @@ +

Listing Yields

+ +<%= 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)} + /> + +<% end %> + + + + + + + + + + + + + + <%= for yield <- @yields do %> + + + + + + + + + <% end %> + +
DatePlantAmountUnit
<%= yield.date %><%= yield.plant %><%= yield.amount %><%= yield.unit %> + <%= live_redirect "Show", to: Routes.yield_show_path(@socket, :show, yield) %> + <%= live_patch "Edit", to: Routes.yield_index_path(@socket, :edit, yield) %> + <%= link "Delete", to: "#", phx_click: "delete", phx_value_id: yield.id, data: [confirm: "Are you sure?"] %> +
+ +<%= live_patch "New Yield", to: Routes.yield_index_path(@socket, :new) %> diff --git a/lib/osuuspuutarha_web/live/yield_live/show.ex b/lib/osuuspuutarha_web/live/yield_live/show.ex new file mode 100644 index 0000000..0f8c2fd --- /dev/null +++ b/lib/osuuspuutarha_web/live/yield_live/show.ex @@ -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 diff --git a/lib/osuuspuutarha_web/live/yield_live/show.html.heex b/lib/osuuspuutarha_web/live/yield_live/show.html.heex new file mode 100644 index 0000000..46d4a12 --- /dev/null +++ b/lib/osuuspuutarha_web/live/yield_live/show.html.heex @@ -0,0 +1,41 @@ +

Show Yield

+ +<%= 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)} + /> + +<% end %> + + + +<%= live_patch "Edit", to: Routes.yield_show_path(@socket, :edit, @yield), class: "button" %> | +<%= live_redirect "Back", to: Routes.yield_index_path(@socket, :index) %> diff --git a/lib/osuuspuutarha_web/router.ex b/lib/osuuspuutarha_web/router.ex index ec1aabc..c939712 100644 --- a/lib/osuuspuutarha_web/router.ex +++ b/lib/osuuspuutarha_web/router.ex @@ -45,6 +45,13 @@ defmodule OsuuspuutarhaWeb.Router do live "/tilaukset/:id", OrderLive.Show, :show 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 scope "/lataukset", as: :exports, alias: OsuuspuutarhaWeb.Exports do diff --git a/priv/repo/migrations/20230608175223_create_yields.exs b/priv/repo/migrations/20230608175223_create_yields.exs new file mode 100644 index 0000000..ca99fc0 --- /dev/null +++ b/priv/repo/migrations/20230608175223_create_yields.exs @@ -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 diff --git a/test/osuuspuutarha/harvest_test.exs b/test/osuuspuutarha/harvest_test.exs new file mode 100644 index 0000000..8cfd103 --- /dev/null +++ b/test/osuuspuutarha/harvest_test.exs @@ -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 diff --git a/test/osuuspuutarha_web/live/yield_live_test.exs b/test/osuuspuutarha_web/live/yield_live_test.exs new file mode 100644 index 0000000..3957fee --- /dev/null +++ b/test/osuuspuutarha_web/live/yield_live_test.exs @@ -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 diff --git a/test/support/fixtures/harvest_fixtures.ex b/test/support/fixtures/harvest_fixtures.ex new file mode 100644 index 0000000..b91a0a8 --- /dev/null +++ b/test/support/fixtures/harvest_fixtures.ex @@ -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