Refactor get_artikel*() functions/sql

This commit is contained in:
Thelonius Kort
2023-02-28 21:42:05 +01:00
parent fc0818678c
commit b87b54ec71
3 changed files with 27 additions and 12 deletions

View File

@ -4,6 +4,8 @@ defmodule Outlook.Artikel do
""" """
alias Outlook.Translations.Translation alias Outlook.Translations.Translation
alias Outlook.Articles.Article
alias Outlook.Authors.Author
import Ecto.Query, warn: false import Ecto.Query, warn: false
alias Outlook.Repo 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!(artikel) when is_struct(artikel), do: get_artikel!(artikel.id)
def get_artikel!(id) do def get_artikel!(id) do
Repo.one(from t in Translation, where: t.id == ^id and t.public == true) q = from t in Translation,
|> Repo.preload([article: :author]) 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 end
def get_artikel_by_tid(tid) do def get_artikel_by_tid(tid) do
@ -25,9 +43,5 @@ defmodule Outlook.Artikel do
|> List.last() |> List.last()
|> String.to_integer(36) |> String.to_integer(36)
|> get_artikel!() |> get_artikel!()
case artikel do
%Translation{} -> {:ok, artikel}
_ -> {:error, "Artikel does not exist, or isn't public."}
end
end end
end end

View File

@ -13,7 +13,8 @@ defmodule OutlookWeb.ArtikelController do
{:ok, artikel} -> render(conn, :show, artikel: artikel, page_title: artikel.title) {:ok, artikel} -> render(conn, :show, artikel: artikel, page_title: artikel.title)
{:error, message} -> conn {:error, message} -> conn
|> put_status(404) |> put_status(404)
|> render(OutlookWeb.ErrorHTML, "404.html") |> put_view(OutlookWeb.ErrorHTML)
|> render("404.html")
|> halt() |> halt()
end end
end end

View File

@ -1,10 +1,10 @@
<header class="mb-6"> <header class="mb-6">
<h1 class="text-lg font-semibold leading-tight text-stone-800 dark:text-stone-200"><%= @artikel.title %></h1> <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> <p class="my-2"><.link href={~p"/autoren/#{@artikel.author_id}"}><%= @artikel.author %></.link>
&nbsp;&nbsp;&nbsp; — &nbsp;&nbsp;&nbsp;<%= Calendar.strftime(@artikel.article.date, "%d.%m.%Y") %></p> &nbsp;&nbsp;&nbsp; — &nbsp;&nbsp;&nbsp;<%= Calendar.strftime(@artikel.date_org, "%d.%m.%Y") %></p>
<div>Original Artikel: <div>Original Artikel:
<.link class="hover:text-sky-700" href={@artikel.article.url} > <.link class="hover:text-sky-700" href={@artikel.url_org} >
<%= @artikel.article.title %> <%= @artikel.title_org %>
</.link><br> </.link><br>
</div> </div>
<div class="my-2"> <div class="my-2">
@ -14,4 +14,4 @@
<div class="article w-full mx-auto max-w-xs"><%= @artikel.public_content |> raw %></div> <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>