View Source OnePiece.Commanded.QueryHandler behaviour (OnePiece.Commanded v0.19.1)

Defines a module as a "Query Handler". For more information about queries, please read the following:

Summary

Callbacks

Handle the incoming params and return the result data.

Types

@type error() :: any()
@type view_model() :: any()

Callbacks

@callback handle(params :: any()) :: {:ok, view_model()} | {:error, error()}

Handle the incoming params and return the result data.

Functions

Link to this macro

__using__(opts \\ [])

View Source (macro)
@spec __using__(opts :: []) :: any()

Convert the module into a OnePiece.Commanded.QueryHandler.

Usage

defmodule MyQueryHandler do
  use OnePiece.Commanded.QueryHandler
  import Ecto.Query, only: [from: 2]

  @impl OnePiece.Commanded.QueryHandler
  def handle(params) do
    query = from u in User,
              where: u.age > 18 or is_nil(params.email),
              select: u

    {:ok, Repo.all(query)}
  end
end