diff --git a/config/dev.exs b/config/dev.exs index ffc8d46..c08a744 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -58,8 +58,8 @@ config :outlook, OutlookWeb.Endpoint, patterns: [ ~r"priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$", ~r"priv/gettext/.*(po)$", - ~r"lib/outlook_web/(live|views|components)/.*(ex)$", - ~r"lib/outlook_web/templates/.*(eex)$" + ~r"lib/outlook_web/(live|controllers|components)/.*(ex)$", + ~r"lib/outlook_web/controllers/.*/.*(eex)$" ] ] diff --git a/lib/outlook/artikel.ex b/lib/outlook/artikel.ex new file mode 100644 index 0000000..bc842c8 --- /dev/null +++ b/lib/outlook/artikel.ex @@ -0,0 +1,21 @@ +defmodule Outlook.Artikel do + @moduledoc """ + The Artikel context. + """ + + alias Outlook.Translations.Translation + + import Ecto.Query, warn: false + alias Outlook.Repo + + def list_artikel do + Repo.all(from t in Translation, where: t.public == true) + |> Repo.preload([article: :author]) + end + + def get_artikel!(artikel) when is_struct(artikel), do: get_artikel!(artikel.id) + def get_artikel!(id) do + Repo.one(from t in Translation, where: t.id == ^id and t.public == true) + |> Repo.preload([article: :author]) + end +end diff --git a/lib/outlook/autoren.ex b/lib/outlook/autoren.ex new file mode 100644 index 0000000..f73ced7 --- /dev/null +++ b/lib/outlook/autoren.ex @@ -0,0 +1,34 @@ +defmodule Outlook.Autoren do + @moduledoc """ + The Autoren context. + """ + + import Ecto.Query, warn: false + alias Outlook.Repo + + alias Outlook.Articles.Article + alias Outlook.Translations.Translation + + alias Outlook.Authors.Author + + def list_autoren do + Repo.all(Author) + end + + def get_autor!(id) do + Repo.get!(Author, id) + |> Repo.preload([articles: [:translations]]) + end + + @doc "This is ugly" + def list_artikel(author) when is_struct(author), do: list_artikel(author.id) + def list_artikel(author_id) do + aids = Repo.all(from a in Article, + select: [:id], + where: a.author_id == ^author_id) + |> Enum.map(fn a -> a.id end) + Repo.all(from t in Translation, + select: [t.title, t.teaser, t.date, t.user_id], + where: t.article_id in ^aids and t.public == true) + end +end diff --git a/lib/outlook_web/controllers/artikel_controller.ex b/lib/outlook_web/controllers/artikel_controller.ex new file mode 100644 index 0000000..d3bd091 --- /dev/null +++ b/lib/outlook_web/controllers/artikel_controller.ex @@ -0,0 +1,15 @@ +defmodule OutlookWeb.ArtikelController do + use OutlookWeb, :controller + + alias Outlook.Artikel + + def index(conn, _params) do + artikel = Artikel.list_artikel() + render(conn, :index, artikel: artikel) + end + + def show(conn, %{"id" => id}) do + artikel = Artikel.get_artikel!(id) + render(conn, :show, artikel: artikel) + end +end diff --git a/lib/outlook_web/controllers/artikel_html.ex b/lib/outlook_web/controllers/artikel_html.ex new file mode 100644 index 0000000..1d0b594 --- /dev/null +++ b/lib/outlook_web/controllers/artikel_html.ex @@ -0,0 +1,5 @@ +defmodule OutlookWeb.ArtikelHTML do + use OutlookWeb, :html + + embed_templates "artikel_html/*" +end diff --git a/lib/outlook_web/controllers/artikel_html/index.html.heex b/lib/outlook_web/controllers/artikel_html/index.html.heex new file mode 100644 index 0000000..611af4a --- /dev/null +++ b/lib/outlook_web/controllers/artikel_html/index.html.heex @@ -0,0 +1,19 @@ +<.header> + Listing Artikel + <:actions> + + + +<.table id="artikel" rows={@artikel} row_click={&JS.navigate(~p"/artikel/#{&1}")}> + <:col :let={artikel} label="Title"><%= artikel.title %> + <:col :let={artikel} label="Teaser"><%= artikel.teaser %> + <%!-- <:col :let={artikel} label="Translator"><%= artikel.translator %> --%> + <:col :let={artikel} label="Unauthorized"><%= artikel.unauthorized %> + <:col :let={artikel} label="Public content"><%= artikel.public_content %> + <:col :let={artikel} label="Date"><%= Calendar.strftime(artikel.date, "%d.%m.%Y") %> + <:action :let={artikel}> +