Refactor get_artikel*() functions/sql
This commit is contained in:
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
<header class="mb-6">
|
||||
<h1 class="text-lg font-semibold leading-tight text-stone-800 dark:text-stone-200"><%= @artikel.title %></h1>
|
||||
<p class="my-2"><.link href={"/autoren/#{@artikel.article.author.id}"}><%= @artikel.article.author.name %></.link>
|
||||
— <%= Calendar.strftime(@artikel.article.date, "%d.%m.%Y") %></p>
|
||||
<p class="my-2"><.link href={~p"/autoren/#{@artikel.author_id}"}><%= @artikel.author %></.link>
|
||||
— <%= Calendar.strftime(@artikel.date_org, "%d.%m.%Y") %></p>
|
||||
<div>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 %>
|
||||
</.link><br>
|
||||
</div>
|
||||
<div class="my-2">
|
||||
@ -14,4 +14,4 @@
|
||||
|
||||
<div class="article w-full mx-auto max-w-xs"><%= @artikel.public_content |> raw %></div>
|
||||
|
||||
<.back navigate={~p"/autoren/#{@artikel.article.author}"}>Back to Autor</.back>
|
||||
<.back navigate={~p"/autoren/#{@artikel.author_id}"}>Back to Autor</.back>
|
||||
|
||||
Reference in New Issue
Block a user