Simplify moderation logs context

This commit is contained in:
Liam 2024-07-15 20:43:45 -04:00
parent 08405d1d5b
commit dd5a118d8c
2 changed files with 10 additions and 33 deletions

View file

@ -9,40 +9,24 @@ defmodule Philomena.ModerationLogs do
alias Philomena.ModerationLogs.ModerationLog alias Philomena.ModerationLogs.ModerationLog
@doc """ @doc """
Returns the list of moderation_logs. Returns a paginated list of moderation logs as a `m:Scrivener.Page`.
## Examples ## Examples
iex> list_moderation_logs() iex> list_moderation_logs(page_size: 15)
[%ModerationLog{}, ...] [%ModerationLog{}, ...]
""" """
def list_moderation_logs(conn) do def list_moderation_logs(pagination) do
ModerationLog ModerationLog
|> where([ml], ml.created_at > ago(2, "week")) |> where([ml], ml.created_at >= ago(2, "week"))
|> preload(:user) |> preload(:user)
|> order_by(desc: :created_at) |> order_by(desc: :created_at)
|> Repo.paginate(conn.assigns.scrivener) |> Repo.paginate(pagination)
end end
@doc """ @doc """
Gets a single moderation_log. Creates a moderation log.
Raises `Ecto.NoResultsError` if the Moderation log does not exist.
## Examples
iex> get_moderation_log!(123)
%ModerationLog{}
iex> get_moderation_log!(456)
** (Ecto.NoResultsError)
"""
def get_moderation_log!(id), do: Repo.get!(ModerationLog, id)
@doc """
Creates a moderation_log.
## Examples ## Examples
@ -60,21 +44,14 @@ defmodule Philomena.ModerationLogs do
end end
@doc """ @doc """
Deletes a moderation_log. Removes moderation logs created more than 2 weeks ago.
## Examples ## Examples
iex> delete_moderation_log(moderation_log) iex> cleanup!()
{:ok, %ModerationLog{}} {31, nil}
iex> delete_moderation_log(moderation_log)
{:error, %Ecto.Changeset{}}
""" """
def delete_moderation_log(%ModerationLog{} = moderation_log) do
Repo.delete(moderation_log)
end
def cleanup! do def cleanup! do
ModerationLog ModerationLog
|> where([ml], ml.created_at < ago(2, "week")) |> where([ml], ml.created_at < ago(2, "week"))

View file

@ -9,7 +9,7 @@ defmodule PhilomenaWeb.ModerationLogController do
preload: [:user] preload: [:user]
def index(conn, _params) do def index(conn, _params) do
moderation_logs = ModerationLogs.list_moderation_logs(conn) moderation_logs = ModerationLogs.list_moderation_logs(conn.assigns.scrivener)
render(conn, "index.html", title: "Moderation Logs", moderation_logs: moderation_logs) render(conn, "index.html", title: "Moderation Logs", moderation_logs: moderation_logs)
end end
end end