diff --git a/lib/runosaari/pages.ex b/lib/runosaari/pages.ex new file mode 100644 index 0000000..8ca00c7 --- /dev/null +++ b/lib/runosaari/pages.ex @@ -0,0 +1,104 @@ +defmodule Runosaari.Pages do + @moduledoc """ + The Pages context. + """ + + import Ecto.Query, warn: false + alias Runosaari.Repo + + alias Runosaari.Pages.Index + + @doc """ + Returns the list of index_paragraphs. + + ## Examples + + iex> list_index_paragraphs() + [%Index{}, ...] + + """ + def list_index_paragraphs do + Repo.all(Index) + end + + @doc """ + Gets a single index. + + Raises `Ecto.NoResultsError` if the Index does not exist. + + ## Examples + + iex> get_index!(123) + %Index{} + + iex> get_index!(456) + ** (Ecto.NoResultsError) + + """ + def get_index!(id), do: Repo.get!(Index, id) + + @doc """ + Creates a index. + + ## Examples + + iex> create_index(%{field: value}) + {:ok, %Index{}} + + iex> create_index(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_index(attrs \\ %{}) do + %Index{} + |> Index.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a index. + + ## Examples + + iex> update_index(index, %{field: new_value}) + {:ok, %Index{}} + + iex> update_index(index, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_index(%Index{} = index, attrs) do + index + |> Index.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a index. + + ## Examples + + iex> delete_index(index) + {:ok, %Index{}} + + iex> delete_index(index) + {:error, %Ecto.Changeset{}} + + """ + def delete_index(%Index{} = index) do + Repo.delete(index) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking index changes. + + ## Examples + + iex> change_index(index) + %Ecto.Changeset{data: %Index{}} + + """ + def change_index(%Index{} = index, attrs \\ %{}) do + Index.changeset(index, attrs) + end +end diff --git a/lib/runosaari/pages/index.ex b/lib/runosaari/pages/index.ex new file mode 100644 index 0000000..290e782 --- /dev/null +++ b/lib/runosaari/pages/index.ex @@ -0,0 +1,18 @@ +defmodule Runosaari.Pages.Index do + use Ecto.Schema + import Ecto.Changeset + + schema "index_paragraphs" do + field :content, :string + field :seqnum, :integer, default: 999 + + timestamps() + end + + @doc false + def changeset(index, attrs) do + index + |> cast(attrs, [:content, :seqnum]) + |> validate_required([:content, :seqnum]) + end +end diff --git a/lib/runosaari_web/controllers/index_controller.ex b/lib/runosaari_web/controllers/index_controller.ex new file mode 100644 index 0000000..5fd113b --- /dev/null +++ b/lib/runosaari_web/controllers/index_controller.ex @@ -0,0 +1,67 @@ +defmodule RunosaariWeb.IndexController do + use RunosaariWeb, :controller + + alias Runosaari.Pages + alias Runosaari.Pages.Index + + def index(conn, _params) do + index_paragraphs = Pages.list_index_paragraphs() + render(conn, "index.html", index_paragraphs: index_paragraphs) + end + + def admin(conn, _params) do + index_paragraphs = Pages.list_index_paragraphs() + render(conn, "admin.html", index_paragraphs: index_paragraphs) + end + + def new(conn, _params) do + changeset = Pages.change_index(%Index{}) + render(conn, "new.html", changeset: changeset) + end + + def create(conn, %{"index" => index_params}) do + case Pages.create_index(index_params) do + {:ok, index} -> + conn + |> put_flash(:info, "Index created successfully.") + |> redirect(to: Routes.admin_index_path(conn, :show, index)) + + {:error, %Ecto.Changeset{} = changeset} -> + render(conn, "new.html", changeset: changeset) + end + end + + def show(conn, %{"id" => id}) do + index = Pages.get_index!(id) + render(conn, "show.html", index: index) + end + + def edit(conn, %{"id" => id}) do + index = Pages.get_index!(id) + changeset = Pages.change_index(index) + render(conn, "edit.html", index: index, changeset: changeset) + end + + def update(conn, %{"id" => id, "index" => index_params}) do + index = Pages.get_index!(id) + + case Pages.update_index(index, index_params) do + {:ok, index} -> + conn + |> put_flash(:info, "Index updated successfully.") + |> redirect(to: Routes.admin_index_path(conn, :show, index)) + + {:error, %Ecto.Changeset{} = changeset} -> + render(conn, "edit.html", index: index, changeset: changeset) + end + end + + def delete(conn, %{"id" => id}) do + index = Pages.get_index!(id) + {:ok, _index} = Pages.delete_index(index) + + conn + |> put_flash(:info, "Index deleted successfully.") + |> redirect(to: Routes.index_path(conn, :index)) + end +end diff --git a/lib/runosaari_web/controllers/page_controller.ex b/lib/runosaari_web/controllers/page_controller.ex index 4d63fa3..f9f71fd 100644 --- a/lib/runosaari_web/controllers/page_controller.ex +++ b/lib/runosaari_web/controllers/page_controller.ex @@ -1,10 +1,6 @@ defmodule RunosaariWeb.PageController do use RunosaariWeb, :controller - def index(conn, _params) do - render(conn, "index.html") - end - def info(conn, _params) do render(conn, "info.html") end diff --git a/lib/runosaari_web/router.ex b/lib/runosaari_web/router.ex index 30602b7..2738575 100644 --- a/lib/runosaari_web/router.ex +++ b/lib/runosaari_web/router.ex @@ -16,7 +16,7 @@ defmodule RunosaariWeb.Router do scope "/", RunosaariWeb do pipe_through :browser - get "/", PageController, :index + resources "/", IndexController, only: [:index] get "/info", PageController, :info get "/covid19", PageController, :covid19 get "/privacy", PageController, :privacy @@ -29,6 +29,8 @@ defmodule RunosaariWeb.Router do scope "/admin", RunosaariWeb, as: :admin do pipe_through :browser + get "/index", IndexController, :admin + resources "/index", IndexController, except: [:index] get "/performers", PerformerController, :admin get "/performances", PerformanceController, :admin resources "/performers", PerformerController, except: [:index] diff --git a/lib/runosaari_web/templates/index/admin.html.eex b/lib/runosaari_web/templates/index/admin.html.eex new file mode 100644 index 0000000..0f24541 --- /dev/null +++ b/lib/runosaari_web/templates/index/admin.html.eex @@ -0,0 +1,30 @@ +
+

HALLINTA - Etusivu

+ + + + + + + + + + + + <%= for index <- @index_paragraphs do %> + + + + + + + <% end %> + +
SisältöPrioriteetti
<%= index.content %><%= index.seqnum %> + <%= 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 "Uusi Kappale", to: Routes.admin_index_path(@conn, :new) %> +
diff --git a/lib/runosaari_web/templates/index/edit.html.eex b/lib/runosaari_web/templates/index/edit.html.eex new file mode 100644 index 0000000..f9be3ae --- /dev/null +++ b/lib/runosaari_web/templates/index/edit.html.eex @@ -0,0 +1,5 @@ +

Muokkaa kappaletta

+ +<%= render "form.html", Map.put(assigns, :action, Routes.admin_index_path(@conn, :update, @index)) %> + +<%= link "Takaisin", to: Routes.index_path(@conn, :index) %> diff --git a/lib/runosaari_web/templates/index/form.html.eex b/lib/runosaari_web/templates/index/form.html.eex new file mode 100644 index 0000000..c8a5a78 --- /dev/null +++ b/lib/runosaari_web/templates/index/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ö" %> + <%= textarea f, :content %> + <%= error_tag f, :content %> + + <%= label f, :seqnum, "Prioriteetti (1 on korkein)" %> + <%= number_input f, :seqnum %> + <%= error_tag f, :seqnum %> + +
+ <%= submit "Tallenna" %> +
+<% end %> diff --git a/lib/runosaari_web/templates/index/index.html.eex b/lib/runosaari_web/templates/index/index.html.eex new file mode 100644 index 0000000..a0734b0 --- /dev/null +++ b/lib/runosaari_web/templates/index/index.html.eex @@ -0,0 +1,28 @@ + + +
+

Runosaari 2021

+

+ ~ 22.-24.7. Livonsaari & Velkuanmaa ~
+ Eksymisretki omaan luontoosi, metsänpeiton suojaan! +

+ <%= for index <- @index_paragraphs do %> +

<%= index.content %>

+ <% end %> +

+ [Ohjelmaa päivitetään muuttuvan tilanteen mukaan] +

+
diff --git a/lib/runosaari_web/templates/index/new.html.eex b/lib/runosaari_web/templates/index/new.html.eex new file mode 100644 index 0000000..08295a8 --- /dev/null +++ b/lib/runosaari_web/templates/index/new.html.eex @@ -0,0 +1,7 @@ +
+

Luo kappale

+ + <%= render "form.html", Map.put(assigns, :action, Routes.admin_index_path(@conn, :create)) %> + + <%= link "Takaisin", to: Routes.index_path(@conn, :index) %> +
diff --git a/lib/runosaari_web/templates/index/show.html.eex b/lib/runosaari_web/templates/index/show.html.eex new file mode 100644 index 0000000..8678749 --- /dev/null +++ b/lib/runosaari_web/templates/index/show.html.eex @@ -0,0 +1,20 @@ +
+

Kappaleen tiedot

+ + + + <%= link "Muokkaa", to: Routes.admin_index_path(@conn, :edit, @index) %> + <%= link "Takaisin", to: Routes.index_path(@conn, :index) %> +
diff --git a/lib/runosaari_web/templates/layout/app.html.eex b/lib/runosaari_web/templates/layout/app.html.eex index 38287ee..54f841c 100644 --- a/lib/runosaari_web/templates/layout/app.html.eex +++ b/lib/runosaari_web/templates/layout/app.html.eex @@ -15,7 +15,7 @@