diff --git a/lib/outlook/artikel.ex b/lib/outlook/artikel.ex index f0e9b8e..2d369ce 100644 --- a/lib/outlook/artikel.ex +++ b/lib/outlook/artikel.ex @@ -4,6 +4,8 @@ defmodule Outlook.Artikel do """ alias Outlook.Translations.Translation + alias Outlook.Articles.Article + alias Outlook.Authors.Author import Ecto.Query, warn: false alias Outlook.Repo @@ -15,8 +17,24 @@ defmodule Outlook.Artikel do 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]) + q = from t in Translation, + join: a in Article, on: t.article_id == a.id, + join: au in Author, on: a.author_id == au.id, + select: [ + title: t.title, + date: t.date, + public_content: t.public_content, + title_org: a.title, + url_org: a.url, + date_org: a.date, + author: au.name, + author_id: au.id + ], + where: t.id == ^id and t.public == true + case Repo.one(q) do + nil -> {:error, "Artikel does not exist, or isn't public."} + artikel -> {:ok, artikel |> Enum.into(%{})} + end end def get_artikel_by_tid(tid) do @@ -25,9 +43,5 @@ defmodule Outlook.Artikel do |> List.last() |> String.to_integer(36) |> get_artikel!() - case artikel do - %Translation{} -> {:ok, artikel} - _ -> {:error, "Artikel does not exist, or isn't public."} - end end end diff --git a/lib/outlook_web/controllers/artikel_controller.ex b/lib/outlook_web/controllers/artikel_controller.ex index 7fba3a5..125996c 100644 --- a/lib/outlook_web/controllers/artikel_controller.ex +++ b/lib/outlook_web/controllers/artikel_controller.ex @@ -13,7 +13,8 @@ defmodule OutlookWeb.ArtikelController do {:ok, artikel} -> render(conn, :show, artikel: artikel, page_title: artikel.title) {:error, message} -> conn |> put_status(404) - |> render(OutlookWeb.ErrorHTML, "404.html") + |> put_view(OutlookWeb.ErrorHTML) + |> render("404.html") |> halt() end end diff --git a/lib/outlook_web/controllers/artikel_html/show.html.heex b/lib/outlook_web/controllers/artikel_html/show.html.heex index 31fd293..82cac5e 100644 --- a/lib/outlook_web/controllers/artikel_html/show.html.heex +++ b/lib/outlook_web/controllers/artikel_html/show.html.heex @@ -1,10 +1,10 @@

<%= @artikel.title %>

-

<.link href={"/autoren/#{@artikel.article.author.id}"}><%= @artikel.article.author.name %> -     —    <%= Calendar.strftime(@artikel.article.date, "%d.%m.%Y") %>

+

<.link href={~p"/autoren/#{@artikel.author_id}"}><%= @artikel.author %> +     —    <%= Calendar.strftime(@artikel.date_org, "%d.%m.%Y") %>

Original Artikel: - <.link class="hover:text-sky-700" href={@artikel.article.url} > - <%= @artikel.article.title %> + <.link class="hover:text-sky-700" href={@artikel.url_org} > + <%= @artikel.title_org %>
@@ -14,4 +14,4 @@
<%= @artikel.public_content |> raw %>
-<.back navigate={~p"/autoren/#{@artikel.article.author}"}>Back to Autor +<.back navigate={~p"/autoren/#{@artikel.author_id}"}>Back to Autor