-
-
Notifications
You must be signed in to change notification settings - Fork 206
/
simple.ex
66 lines (52 loc) · 1.57 KB
/
simple.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
defmodule Ash.DataLayer.Simple do
@moduledoc """
A data layer that simply returns structs
This is the data layer that is used under the hood
by embedded resources
"""
@transformers [
Ash.DataLayer.Simple.Transformers.ValidateDslSections
]
use Ash.Dsl.Extension, transformers: @transformers, sections: []
def can?(_, :create), do: true
def can?(_, :update), do: true
def can?(_, :destroy), do: true
def can?(_, :sort), do: true
def can?(_, {:sort, _}), do: true
def can?(_, :filter), do: true
def can?(_, :boolean_filter), do: true
def can?(_, {:filter_expr, _}), do: true
def can?(_, _), do: false
defmodule Query do
@moduledoc false
defstruct [:data, :resource, :filter, :api, sort: []]
end
def resource_to_query(resource, api) do
%Query{data: [], resource: resource, api: api}
end
def run_query(%{data: data, sort: sort, api: api, filter: filter}, _resource) do
{:ok,
data
|> Enum.filter(&Ash.Filter.Runtime.matches?(api, &1, filter))
|> Ash.Actions.Sort.runtime_sort(sort)}
end
def filter(query, filter, _resource) do
{:ok, %{query | filter: filter}}
end
def sort(query, sort, _resource) do
{:ok, %{query | sort: sort}}
end
def set_context(_resource, query, context) do
data = Map.get(context, :data) || []
{:ok, %{query | data: data}}
end
def create(_resource, changeset) do
Ash.Changeset.apply_attributes(changeset)
end
def update(_resource, changeset) do
Ash.Changeset.apply_attributes(changeset)
end
def destroy(_resource, _changeset) do
:ok
end
end