Add autoren and artikel for public viewing
This commit is contained in:
21
lib/outlook/artikel.ex
Normal file
21
lib/outlook/artikel.ex
Normal file
@ -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
|
||||
34
lib/outlook/autoren.ex
Normal file
34
lib/outlook/autoren.ex
Normal file
@ -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
|
||||
Reference in New Issue
Block a user