Add long urls for Artikel

This commit is contained in:
Thelonius Kort
2023-02-12 18:43:30 +01:00
parent 239177db50
commit b0267ef752
7 changed files with 54 additions and 10 deletions

View File

@ -18,4 +18,16 @@ defmodule Outlook.Artikel do
Repo.one(from t in Translation, where: t.id == ^id and t.public == true)
|> Repo.preload([article: :author])
end
def get_artikel_by_tid(tid) do
artikel = tid
|> String.split(~r/--(?=[0-9A-Za-z])/)
|> 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

View File

@ -4,7 +4,7 @@ defmodule Outlook.Translations.Translation do
alias Outlook.Accounts.User
alias Outlook.Articles.Article
alias Outlook.Translations.TranslationUnitsMap
alias Outlook.Translations.{TranslationUnitsMap,Translation}
schema "translations" do
field :content, TranslationUnitsMap
@ -32,4 +32,30 @@ defmodule Outlook.Translations.Translation do
name: :article_id_lang_unique_index)
|> foreign_key_constraint(:article_id)
end
def translate_unicode(str) do
mapping = %{"Ä" => "Ae",
"Ö" => "Oe",
"Ü" => "Ue",
"ä" => "ae",
"ö" => "oe",
"ü" => "ue",
"ß" => "ss"}
{:ok, re} = "[#{Map.keys(mapping) |> Enum.join}]" |> Regex.compile("u")
Regex.replace(re, str, fn(c) -> mapping[c] end)
end
def spit_title(title) do
title
|> translate_unicode()
# |> String.replace(~r/[^-0-9A-Za-z ]/u, "_")
|> String.replace(~r/[^\w\s-]/u, "")
|> String.replace(~r/(\s|-)+/u, "-")
end
defimpl Phoenix.Param, for: Translation do
def to_param(%{id: id, title: title}) do
"#{Translation.spit_title(title)}--#{Integer.to_string(id, 36) |> String.downcase()}"
end
end
end