From 61a2792dbed62a58b46d549ae6f27178d38e89b3 Mon Sep 17 00:00:00 2001
From: codevictory
Date: Sat, 12 Jun 2021 00:16:35 +0300
Subject: [PATCH] Editable info paragraphs.
---
lib/runosaari/pages.ex | 109 ++++++++++++++++++
lib/runosaari/pages/info.ex | 18 +++
.../controllers/info_controller.ex | 67 +++++++++++
lib/runosaari_web/router.ex | 4 +-
.../templates/index/admin.html.eex | 6 +-
.../templates/info/admin.html.eex | 30 +++++
.../templates/info/edit.html.eex | 5 +
.../templates/info/form.html.eex | 19 +++
.../info.html.eex => info/index.html.eex} | 15 +--
lib/runosaari_web/templates/info/new.html.eex | 5 +
.../templates/info/show.html.eex | 20 ++++
.../templates/layout/app.html.eex | 4 +-
lib/runosaari_web/views/info_view.ex | 3 +
mix.exs | 2 +-
.../20210609191911_create_info_paragraphs.exs | 13 +++
test/runosaari/pages_test.exs | 61 ++++++++++
.../controllers/info_controller_test.exs | 88 ++++++++++++++
17 files changed, 450 insertions(+), 19 deletions(-)
create mode 100644 lib/runosaari/pages/info.ex
create mode 100644 lib/runosaari_web/controllers/info_controller.ex
create mode 100644 lib/runosaari_web/templates/info/admin.html.eex
create mode 100644 lib/runosaari_web/templates/info/edit.html.eex
create mode 100644 lib/runosaari_web/templates/info/form.html.eex
rename lib/runosaari_web/templates/{page/info.html.eex => info/index.html.eex} (69%)
create mode 100644 lib/runosaari_web/templates/info/new.html.eex
create mode 100644 lib/runosaari_web/templates/info/show.html.eex
create mode 100644 lib/runosaari_web/views/info_view.ex
create mode 100644 priv/repo/migrations/20210609191911_create_info_paragraphs.exs
create mode 100644 test/runosaari_web/controllers/info_controller_test.exs
diff --git a/lib/runosaari/pages.ex b/lib/runosaari/pages.ex
index 7a7a7ad..f990501 100644
--- a/lib/runosaari/pages.ex
+++ b/lib/runosaari/pages.ex
@@ -114,4 +114,113 @@ defmodule Runosaari.Pages do
def change_index(%Index{} = index, attrs \\ %{}) do
Index.changeset(index, attrs)
end
+
+ alias Runosaari.Pages.Info
+
+ @doc """
+ Returns the list of info_paragraphs.
+
+ ## Examples
+
+ iex> list_info_paragraphs()
+ [%Info{}, ...]
+
+ """
+ def list_info_paragraphs do
+ Repo.all(Info)
+ end
+
+ @doc """
+ Returns the list of info_paragraphs.
+
+ ## Examples
+
+ iex> list_info_paragraphs()
+ [%Info{}, ...]
+
+ """
+ def list_sorted_info_paragraphs do
+ Repo.all(Info |> order_by(:seqnum))
+ end
+
+ @doc """
+ Gets a single info.
+
+ Raises `Ecto.NoResultsError` if the Info does not exist.
+
+ ## Examples
+
+ iex> get_info!(123)
+ %Info{}
+
+ iex> get_info!(456)
+ ** (Ecto.NoResultsError)
+
+ """
+ def get_info!(id), do: Repo.get!(Info, id)
+
+ @doc """
+ Creates a info.
+
+ ## Examples
+
+ iex> create_info(%{field: value})
+ {:ok, %Info{}}
+
+ iex> create_info(%{field: bad_value})
+ {:error, %Ecto.Changeset{}}
+
+ """
+ def create_info(attrs \\ %{}) do
+ %Info{}
+ |> Info.changeset(attrs)
+ |> Repo.insert()
+ end
+
+ @doc """
+ Updates a info.
+
+ ## Examples
+
+ iex> update_info(info, %{field: new_value})
+ {:ok, %Info{}}
+
+ iex> update_info(info, %{field: bad_value})
+ {:error, %Ecto.Changeset{}}
+
+ """
+ def update_info(%Info{} = info, attrs) do
+ info
+ |> Info.changeset(attrs)
+ |> Repo.update()
+ end
+
+ @doc """
+ Deletes a info.
+
+ ## Examples
+
+ iex> delete_info(info)
+ {:ok, %Info{}}
+
+ iex> delete_info(info)
+ {:error, %Ecto.Changeset{}}
+
+ """
+ def delete_info(%Info{} = info) do
+ Repo.delete(info)
+ end
+
+ @doc """
+ Returns an `%Ecto.Changeset{}` for tracking info changes.
+
+ ## Examples
+
+ iex> change_info(info)
+ %Ecto.Changeset{data: %Info{}}
+
+ """
+ def change_info(%Info{} = info, attrs \\ %{}) do
+ Info.changeset(info, attrs)
+ end
end
diff --git a/lib/runosaari/pages/info.ex b/lib/runosaari/pages/info.ex
new file mode 100644
index 0000000..eab72d3
--- /dev/null
+++ b/lib/runosaari/pages/info.ex
@@ -0,0 +1,18 @@
+defmodule Runosaari.Pages.Info do
+ use Ecto.Schema
+ import Ecto.Changeset
+
+ schema "info_paragraphs" do
+ field :content, :string
+ field :seqnum, :integer, default: 999
+
+ timestamps()
+ end
+
+ @doc false
+ def changeset(info, attrs) do
+ info
+ |> cast(attrs, [:content, :seqnum])
+ |> validate_required([:content, :seqnum])
+ end
+end
diff --git a/lib/runosaari_web/controllers/info_controller.ex b/lib/runosaari_web/controllers/info_controller.ex
new file mode 100644
index 0000000..d9f1699
--- /dev/null
+++ b/lib/runosaari_web/controllers/info_controller.ex
@@ -0,0 +1,67 @@
+defmodule RunosaariWeb.InfoController do
+ use RunosaariWeb, :controller
+
+ alias Runosaari.Pages
+ alias Runosaari.Pages.Info
+
+ def index(conn, _params) do
+ info_paragraphs = Pages.list_sorted_info_paragraphs()
+ render(conn, "index.html", info_paragraphs: info_paragraphs)
+ end
+
+ def admin(conn, _params) do
+ info_paragraphs = Pages.list_info_paragraphs()
+ render(conn, "admin.html", info_paragraphs: info_paragraphs)
+ end
+
+ def new(conn, _params) do
+ changeset = Pages.change_info(%Info{})
+ render(conn, "new.html", changeset: changeset)
+ end
+
+ def create(conn, %{"info" => info_params}) do
+ case Pages.create_info(info_params) do
+ {:ok, info} ->
+ conn
+ |> put_flash(:info, "Info created successfully.")
+ |> redirect(to: Routes.admin_info_path(conn, :admin))
+
+ {:error, %Ecto.Changeset{} = changeset} ->
+ render(conn, "new.html", changeset: changeset)
+ end
+ end
+
+ def show(conn, %{"id" => id}) do
+ info = Pages.get_info!(id)
+ render(conn, "show.html", info: info)
+ end
+
+ def edit(conn, %{"id" => id}) do
+ info = Pages.get_info!(id)
+ changeset = Pages.change_info(info)
+ render(conn, "edit.html", info: info, changeset: changeset)
+ end
+
+ def update(conn, %{"id" => id, "info" => info_params}) do
+ info = Pages.get_info!(id)
+
+ case Pages.update_info(info, info_params) do
+ {:ok, info} ->
+ conn
+ |> put_flash(:info, "Info updated successfully.")
+ |> redirect(to: Routes.admin_info_path(conn, :show, info))
+
+ {:error, %Ecto.Changeset{} = changeset} ->
+ render(conn, "edit.html", info: info, changeset: changeset)
+ end
+ end
+
+ def delete(conn, %{"id" => id}) do
+ info = Pages.get_info!(id)
+ {:ok, _info} = Pages.delete_info(info)
+
+ conn
+ |> put_flash(:info, "Info deleted successfully.")
+ |> redirect(to: Routes.admin_info_path(conn, :admin))
+ end
+end
diff --git a/lib/runosaari_web/router.ex b/lib/runosaari_web/router.ex
index 50a899d..d731bcb 100644
--- a/lib/runosaari_web/router.ex
+++ b/lib/runosaari_web/router.ex
@@ -17,13 +17,13 @@ defmodule RunosaariWeb.Router do
pipe_through :browser
resources "/", IndexController, only: [:index]
- get "/info", PageController, :info
get "/covid19", PageController, :covid19
get "/privacy", PageController, :privacy
resources "/performers", PerformerController, only: [:index, :show]
resources "/performances", PerformanceController, only: [:index]
resources "/visitors", VisitorController, only: [:new, :create]
get "/confirmation", VisitorController, :confirmation
+ resources "/info", InfoController, only: [:index]
end
scope "/admin", RunosaariWeb, as: :admin do
@@ -36,6 +36,8 @@ defmodule RunosaariWeb.Router do
resources "/performers", PerformerController, except: [:index, :show]
resources "/performances", PerformanceController, except: [:index]
resources "/visitors", VisitorController, except: [:new, :create]
+ resources "/info", InfoController, except: [:index]
+ get "/info", InfoController, :admin
end
# Other scopes may use custom stacks.
diff --git a/lib/runosaari_web/templates/index/admin.html.eex b/lib/runosaari_web/templates/index/admin.html.eex
index 0f24541..b992e82 100644
--- a/lib/runosaari_web/templates/index/admin.html.eex
+++ b/lib/runosaari_web/templates/index/admin.html.eex
@@ -1,5 +1,5 @@
- HALLINTA - Etusivu
+ HALLINTA - Etusivu
@@ -19,12 +19,12 @@
|
<%= link "Lisätietoja", to: Routes.admin_index_path(@conn, :show, index) %>
<%= link "Muokkaa", to: Routes.admin_index_path(@conn, :edit, index) %>
- <%= link "Poista", to: Routes.admin_index_path(@conn, :delete, index), method: :delete, data: [confirm: "Are you sure?"] %>
+ <%= link "Poista", to: Routes.admin_index_path(@conn, :delete, index), method: :delete, data: [confirm: "Oletko varma?"] %>
|
<% end %>
- <%= link "Uusi Kappale", to: Routes.admin_index_path(@conn, :new) %>
+ <%= link "Uusi kappale", to: Routes.admin_index_path(@conn, :new) %>
diff --git a/lib/runosaari_web/templates/info/admin.html.eex b/lib/runosaari_web/templates/info/admin.html.eex
new file mode 100644
index 0000000..57eca6e
--- /dev/null
+++ b/lib/runosaari_web/templates/info/admin.html.eex
@@ -0,0 +1,30 @@
+
+ HALLINTA - Infon kappaleet
+
+
+
+
+ | Sisältö |
+ Prioriteetti |
+
+ |
+
+
+
+ <%= for info <- @info_paragraphs do %>
+
+ | <%= info.content %> |
+ <%= info.seqnum %> |
+
+
+ <%= link "Lisätietoja", to: Routes.admin_info_path(@conn, :show, info) %>
+ <%= link "Muokkaa", to: Routes.admin_info_path(@conn, :edit, info) %>
+ <%= link "Poista", to: Routes.admin_info_path(@conn, :delete, info), method: :delete, data: [confirm: "Oletko varma?"] %>
+ |
+
+ <% end %>
+
+
+
+ <%= link "Uusi kappale", to: Routes.admin_info_path(@conn, :new) %>
+
diff --git a/lib/runosaari_web/templates/info/edit.html.eex b/lib/runosaari_web/templates/info/edit.html.eex
new file mode 100644
index 0000000..3912848
--- /dev/null
+++ b/lib/runosaari_web/templates/info/edit.html.eex
@@ -0,0 +1,5 @@
+Muokkaa kappaletta
+
+<%= render "form.html", Map.put(assigns, :action, Routes.admin_info_path(@conn, :update, @info)) %>
+
+<%= link "Takaisin", to: Routes.admin_info_path(@conn, :admin) %>
diff --git a/lib/runosaari_web/templates/info/form.html.eex b/lib/runosaari_web/templates/info/form.html.eex
new file mode 100644
index 0000000..91b069b
--- /dev/null
+++ b/lib/runosaari_web/templates/info/form.html.eex
@@ -0,0 +1,19 @@
+<%= form_for @changeset, @action, fn f -> %>
+ <%= if @changeset.action do %>
+
+
Jokin kentistä on tyhjä.
+
+ <% end %>
+
+ <%= label f, :content, "Sisältö" %>
+ <%= text_input f, :content %>
+ <%= error_tag f, :content %>
+
+ <%= label f, :seqnum, "Prioritetti (1 on korkein)" %>
+ <%= number_input f, :seqnum %>
+ <%= error_tag f, :seqnum %>
+
+
+ <%= submit "Tallenna" %>
+
+<% end %>
diff --git a/lib/runosaari_web/templates/page/info.html.eex b/lib/runosaari_web/templates/info/index.html.eex
similarity index 69%
rename from lib/runosaari_web/templates/page/info.html.eex
rename to lib/runosaari_web/templates/info/index.html.eex
index 75bc98e..7d4fed0 100644
--- a/lib/runosaari_web/templates/page/info.html.eex
+++ b/lib/runosaari_web/templates/info/index.html.eex
@@ -9,18 +9,9 @@
Kirjan talo – Bokens hus ry, Livonsaaren kyläyhdistys ry ja
Saaristohotelli Vaihela.
-
- Tapahtuma tuo korkeatasoista kotimaista runoutta helposti
- lähestyttävässä muodossa maaseudun uusille yleisöille, niin
- paikallisille saaristolaisille kuin mökkiläisille, kutsuvieraille ja
- kauempaa tulleillekin. Samalla se voimistaa paikallista
- yhteisöllisyyttä ja taiteen tekemistä ja kokemista vaikeina
- korona-aikoina.
-
-
- Esitykset striimataan Suomeen ja mahdollisesti myös kansainvälisten
- yhteistyökumppanien sivuille.
-
+ <%= for info <- @info_paragraphs do %>
+ <%= info.content %>
+ <% end %>
Ota yhteyttä: Inkeri Aula & Katariina Vuorinen,
info@runosaari.net
diff --git a/lib/runosaari_web/templates/info/new.html.eex b/lib/runosaari_web/templates/info/new.html.eex
new file mode 100644
index 0000000..981e05d
--- /dev/null
+++ b/lib/runosaari_web/templates/info/new.html.eex
@@ -0,0 +1,5 @@
+
New Info
+
+<%= render "form.html", Map.put(assigns, :action, Routes.admin_info_path(@conn, :create)) %>
+
+<%= link "Back", to: Routes.admin_info_path(@conn, :admin) %>
diff --git a/lib/runosaari_web/templates/info/show.html.eex b/lib/runosaari_web/templates/info/show.html.eex
new file mode 100644
index 0000000..420ab1d
--- /dev/null
+++ b/lib/runosaari_web/templates/info/show.html.eex
@@ -0,0 +1,20 @@
+
+ Kappaleen tiedot
+
+
+
+ -
+ Sisältö:
+ <%= @info.content %>
+
+
+ -
+ Prioriteetti:
+ <%= @info.seqnum %>
+
+
+
+
+ <%= link "Muokkaa", to: Routes.admin_info_path(@conn, :edit, @info) %>
+ <%= link "Takaisin", to: Routes.admin_info_path(@conn, :admin) %>
+
diff --git a/lib/runosaari_web/templates/layout/app.html.eex b/lib/runosaari_web/templates/layout/app.html.eex
index a7d6b0a..d092540 100644
--- a/lib/runosaari_web/templates/layout/app.html.eex
+++ b/lib/runosaari_web/templates/layout/app.html.eex
@@ -19,7 +19,7 @@
<%= link "Etusivu", to: Routes.index_path(@conn, :index) %>
<%= link "Ohjelma", to: Routes.performance_path(@conn, :index) %>
<%= link "Esiintyjät", to: Routes.performer_path(@conn, :index) %>
- <%= link "Info", to: Routes.page_path(@conn, :info) %>
+ <%= link "Info", to: Routes.info_path(@conn, :index) %>
<%= link "Covid-19", to: Routes.page_path(@conn, :covid19) %>
<%= link "Ilmoittautuminen", to: Routes.visitor_path(@conn, :new) %>
@@ -30,7 +30,7 @@
<%= link "Etusivu", to: "#{Routes.index_path(@conn, :index)}#logo-start" %>
<%= link "Ohjelma", to: "#{Routes.performance_path(@conn, :index)}#calendar-start" %>
<%= link "Esiintyjät", to: "#{Routes.performer_path(@conn, :index)}#performers-start" %>
- <%= link "Info", to: "#{Routes.page_path(@conn, :info)}#contact-start" %>
+ <%= link "Info", to: "#{Routes.info_path(@conn, :index )}#contact-start" %>
<%= link "Covid-19", to: "#{Routes.page_path(@conn, :covid19)}#covid-start" %>
<%= link "Ilmoittautuminen", to: "#{Routes.visitor_path(@conn, :new)}#registration-start" %>
diff --git a/lib/runosaari_web/views/info_view.ex b/lib/runosaari_web/views/info_view.ex
new file mode 100644
index 0000000..04e5f55
--- /dev/null
+++ b/lib/runosaari_web/views/info_view.ex
@@ -0,0 +1,3 @@
+defmodule RunosaariWeb.InfoView do
+ use RunosaariWeb, :view
+end
diff --git a/mix.exs b/mix.exs
index 8fe8aea..f54c8ce 100644
--- a/mix.exs
+++ b/mix.exs
@@ -4,7 +4,7 @@ defmodule Runosaari.MixProject do
def project do
[
app: :runosaari,
- version: "0.3.0",
+ version: "0.4.0",
elixir: "~> 1.7",
elixirc_paths: elixirc_paths(Mix.env()),
compilers: [:phoenix, :gettext] ++ Mix.compilers(),
diff --git a/priv/repo/migrations/20210609191911_create_info_paragraphs.exs b/priv/repo/migrations/20210609191911_create_info_paragraphs.exs
new file mode 100644
index 0000000..f50b631
--- /dev/null
+++ b/priv/repo/migrations/20210609191911_create_info_paragraphs.exs
@@ -0,0 +1,13 @@
+defmodule Runosaari.Repo.Migrations.CreateInfoParagraphs do
+ use Ecto.Migration
+
+ def change do
+ create table(:info_paragraphs) do
+ add :content, :string, size: 3000
+ add :seqnum, :integer
+
+ timestamps()
+ end
+
+ end
+end
diff --git a/test/runosaari/pages_test.exs b/test/runosaari/pages_test.exs
index d432309..3103098 100644
--- a/test/runosaari/pages_test.exs
+++ b/test/runosaari/pages_test.exs
@@ -63,4 +63,65 @@ defmodule Runosaari.PagesTest do
assert %Ecto.Changeset{} = Pages.change_index(index)
end
end
+
+ describe "info_paragraphs" do
+ alias Runosaari.Pages.Info
+
+ @valid_attrs %{content: "some content", seqnum: 42}
+ @update_attrs %{content: "some updated content", seqnum: 43}
+ @invalid_attrs %{content: nil, seqnum: nil}
+
+ def info_fixture(attrs \\ %{}) do
+ {:ok, info} =
+ attrs
+ |> Enum.into(@valid_attrs)
+ |> Pages.create_info()
+
+ info
+ end
+
+ test "list_info_paragraphs/0 returns all info_paragraphs" do
+ info = info_fixture()
+ assert Pages.list_info_paragraphs() == [info]
+ end
+
+ test "get_info!/1 returns the info with given id" do
+ info = info_fixture()
+ assert Pages.get_info!(info.id) == info
+ end
+
+ test "create_info/1 with valid data creates a info" do
+ assert {:ok, %Info{} = info} = Pages.create_info(@valid_attrs)
+ assert info.content == "some content"
+ assert info.seqnum == 42
+ end
+
+ test "create_info/1 with invalid data returns error changeset" do
+ assert {:error, %Ecto.Changeset{}} = Pages.create_info(@invalid_attrs)
+ end
+
+ test "update_info/2 with valid data updates the info" do
+ info = info_fixture()
+ assert {:ok, %Info{} = info} = Pages.update_info(info, @update_attrs)
+ assert info.content == "some updated content"
+ assert info.seqnum == 43
+ end
+
+ test "update_info/2 with invalid data returns error changeset" do
+ info = info_fixture()
+ assert {:error, %Ecto.Changeset{}} = Pages.update_info(info, @invalid_attrs)
+ assert info == Pages.get_info!(info.id)
+ end
+
+ test "delete_info/1 deletes the info" do
+ info = info_fixture()
+ assert {:ok, %Info{}} = Pages.delete_info(info)
+ assert_raise Ecto.NoResultsError, fn -> Pages.get_info!(info.id) end
+ end
+
+ test "change_info/1 returns a info changeset" do
+ info = info_fixture()
+ assert %Ecto.Changeset{} = Pages.change_info(info)
+ end
+ end
end
diff --git a/test/runosaari_web/controllers/info_controller_test.exs b/test/runosaari_web/controllers/info_controller_test.exs
new file mode 100644
index 0000000..71dd058
--- /dev/null
+++ b/test/runosaari_web/controllers/info_controller_test.exs
@@ -0,0 +1,88 @@
+defmodule RunosaariWeb.InfoControllerTest do
+ use RunosaariWeb.ConnCase
+
+ alias Runosaari.Pages
+
+ @create_attrs %{content: "some content", seqnum: 42}
+ @update_attrs %{content: "some updated content", seqnum: 43}
+ @invalid_attrs %{content: nil, seqnum: nil}
+
+ def fixture(:info) do
+ {:ok, info} = Pages.create_info(@create_attrs)
+ info
+ end
+
+ describe "index" do
+ test "lists all info_paragraphs", %{conn: conn} do
+ conn = get(conn, Routes.info_path(conn, :index))
+ assert html_response(conn, 200) =~ "Listing Info paragraphs"
+ end
+ end
+
+ describe "new info" do
+ test "renders form", %{conn: conn} do
+ conn = get(conn, Routes.info_path(conn, :new))
+ assert html_response(conn, 200) =~ "New Info"
+ end
+ end
+
+ describe "create info" do
+ test "redirects to show when data is valid", %{conn: conn} do
+ conn = post(conn, Routes.info_path(conn, :create), info: @create_attrs)
+
+ assert %{id: id} = redirected_params(conn)
+ assert redirected_to(conn) == Routes.info_path(conn, :show, id)
+
+ conn = get(conn, Routes.info_path(conn, :show, id))
+ assert html_response(conn, 200) =~ "Show Info"
+ end
+
+ test "renders errors when data is invalid", %{conn: conn} do
+ conn = post(conn, Routes.info_path(conn, :create), info: @invalid_attrs)
+ assert html_response(conn, 200) =~ "New Info"
+ end
+ end
+
+ describe "edit info" do
+ setup [:create_info]
+
+ test "renders form for editing chosen info", %{conn: conn, info: info} do
+ conn = get(conn, Routes.info_path(conn, :edit, info))
+ assert html_response(conn, 200) =~ "Edit Info"
+ end
+ end
+
+ describe "update info" do
+ setup [:create_info]
+
+ test "redirects when data is valid", %{conn: conn, info: info} do
+ conn = put(conn, Routes.info_path(conn, :update, info), info: @update_attrs)
+ assert redirected_to(conn) == Routes.info_path(conn, :show, info)
+
+ conn = get(conn, Routes.info_path(conn, :show, info))
+ assert html_response(conn, 200) =~ "some updated content"
+ end
+
+ test "renders errors when data is invalid", %{conn: conn, info: info} do
+ conn = put(conn, Routes.info_path(conn, :update, info), info: @invalid_attrs)
+ assert html_response(conn, 200) =~ "Edit Info"
+ end
+ end
+
+ describe "delete info" do
+ setup [:create_info]
+
+ test "deletes chosen info", %{conn: conn, info: info} do
+ conn = delete(conn, Routes.info_path(conn, :delete, info))
+ assert redirected_to(conn) == Routes.info_path(conn, :index)
+ assert_error_sent 404, fn ->
+ get(conn, Routes.info_path(conn, :show, info))
+ end
+ end
+ end
+
+ defp create_info(_) do
+ info = fixture(:info)
+ %{info: info}
+ end
+end