Refactoring of Admin context
This commit is contained in:
@@ -1,27 +1,42 @@
|
||||
defmodule Mobilizon.Admin.ActionLog do
|
||||
@moduledoc """
|
||||
ActionLog entity schema
|
||||
Represents an action log entity.
|
||||
"""
|
||||
|
||||
use Ecto.Schema
|
||||
|
||||
import Ecto.Changeset
|
||||
|
||||
alias Mobilizon.Actors.Actor
|
||||
|
||||
@required_attrs [:action, :target_type, :target_id, :changes, :actor_id]
|
||||
@type t :: %__MODULE__{
|
||||
action: String.t(),
|
||||
target_type: String.t(),
|
||||
target_id: integer,
|
||||
changes: map,
|
||||
actor: Actor.t()
|
||||
}
|
||||
|
||||
@required_attrs [:action, :target_type, :target_id, :actor_id]
|
||||
@optional_attrs [:changes]
|
||||
@attrs @required_attrs ++ @optional_attrs
|
||||
|
||||
schema "admin_action_logs" do
|
||||
field(:action, :string)
|
||||
field(:target_type, :string)
|
||||
field(:target_id, :integer)
|
||||
field(:changes, :map)
|
||||
|
||||
belongs_to(:actor, Actor)
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
@doc false
|
||||
@spec changeset(t | Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
|
||||
def changeset(action_log, attrs) do
|
||||
action_log
|
||||
|> cast(attrs, @required_attrs)
|
||||
|> validate_required(@required_attrs -- [:changes])
|
||||
|> cast(attrs, @attrs)
|
||||
|> validate_required(@required_attrs)
|
||||
end
|
||||
end
|
||||
|
||||
35
lib/mobilizon/admin/admin.ex
Normal file
35
lib/mobilizon/admin/admin.ex
Normal file
@@ -0,0 +1,35 @@
|
||||
defmodule Mobilizon.Admin do
|
||||
@moduledoc """
|
||||
The Admin context.
|
||||
"""
|
||||
|
||||
import Ecto.Query
|
||||
|
||||
alias Mobilizon.Admin.ActionLog
|
||||
alias Mobilizon.Storage.{Page, Repo}
|
||||
|
||||
@doc """
|
||||
Returns the list of action logs.
|
||||
"""
|
||||
@spec list_action_logs(integer | nil, integer | nil) :: [ActionLog.t()]
|
||||
def list_action_logs(page \\ nil, limit \\ nil) do
|
||||
list_action_logs_query()
|
||||
|> Page.paginate(page, limit)
|
||||
|> Repo.all()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a action_log.
|
||||
"""
|
||||
@spec create_action_log(map) :: {:ok, ActionLog.t()} | {:error, Ecto.Changeset.t()}
|
||||
def create_action_log(attrs \\ %{}) do
|
||||
%ActionLog{}
|
||||
|> ActionLog.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
@spec list_action_logs_query :: Ecto.Query.t()
|
||||
defp list_action_logs_query do
|
||||
from(r in ActionLog, preload: [:actor])
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user