-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.ex
187 lines (162 loc) · 5.75 KB
/
controller.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
defmodule MyApp.AutoAdminController do
use MyApp, :controller
use MyDbDriver.{Commands, Queries}
plug :put_layout, "autoadmin.html"
plug :put_models
plug :put_selects
## Begin admin update section (you only need to make changes in this section when adding a new schema to the admin)
@models %{
"users" => Accounts.User,
"organizations" => Accounts.Organization,
"organization_invitations" => Accounts.OrganizationInvitation,
"organization_subscriptions" => Accounts.OrganizationSubscription,
"groups" => Accounts.Group,
"group_invitations" => Accounts.GroupInvitation,
"group_subscriptions" => Accounts.GroupSubscription,
"events_api_events" => ApiEvents.Events.Event,
"events_api_event_invitations" => ApiEvents.Events.EventInvitation,
"events_api_event_subscriptions" => ApiEvents.Events.EventSubscription,
# ... and any other possible schema you have!
}
@selects %{
organizations: %{
address: %{
state: UsState.to_select(:include_blank),
},
status: Accounts.OrganizationStatus.to_select(:include_blank),
},
groups: %{
status: Accounts.GroupStatus.to_select(:include_blank),
visibility: Accounts.GroupVisibility.to_select(:include_blank),
affiliation_mode: Accounts.AffiliationMode.to_select(:include_blank),
},
users: %{
role: Accounts.SystemRole.to_select(:include_blank),
status: Accounts.UserStatus.to_select(:include_blank),
notification_methods: %{multi: Accounts.NotificationMethod.to_select()},
},
events_api_events: %{
bridge_provider: ApiEvents.Events.BridgeProvider.all(),
status: ApiEvents.Events.EventStatus.all(),
}
}
## End admin update section
@repos %{
"accounts" => ApiAccounts.Repo,
"events" => ApiEvents.Repo
}
def index(conn, %{"schema" => api_schema}) do
page_num = Map.get conn.params, "page", "1"
[api, schema] = case String.split(api_schema, "_api_") do
[api, _schema] -> [api, api_schema]
[schema] -> ["accounts", schema]
end
repo = @repos[api]
schema_module = @models[schema]
{:ok, page} = schema_module
|> ExecuteQuery.paginate(repo, [page: page_num])
opts=[
page: page,
schema_module: schema_module,
schema: schema,
page_num: page_num
]
render(conn, "index.html", opts)
end
def edit(conn, %{"schema" => api_schema, "id" => id}) do
[api, schema, prefix] = case String.split(api_schema, "_api_") do
[api, _schema] -> [api, api_schema, "#{api}_api_"]
[schema] -> ["accounts", schema, ""]
end
repo = @repos[api]
schema_module = @models[schema]
page_nums = schema_module.__schema__(:associations)
|> Enum.reduce(%{}, fn association, acc ->
page_num = Map.get(conn.params, "#{association}_page", "1")
Map.put acc, association, page_num
end)
# model = schema_module.__schema__(:associations)
# |> Enum.reduce(repo.get(schema_module, id), fn a, m ->
# repo.preload(m, a)
# end)
model = schema_module.__schema__(:associations)
|> Enum.reduce(repo.get(schema_module, id), fn association, model ->
schema = schema_module.__schema__(:association, association)
case schema do
%{related_key: key} ->
page_num = Map.get page_nums, association
map = Map.put %{}, key, id
keyword = Keyword.new map
query = from r in schema.related, where: ^keyword
records = case ExecuteQuery.paginate(query, repo, [page: page_num]) do
{:ok, page} -> page.entries
_ -> []
end
Map.put model, association, records
_ ->
model
end
end)
# TODO: load changeset from session?
opts = [
changeset: Ecto.Changeset.change(model, %{}),
schema_module: schema_module,
schema: schema,
prefix: prefix,
id: id,
page_nums: page_nums
]
render(conn, "edit.html", opts)
end
# create/2 works similarly and may be considered an exercise for the reader =)
def update(conn, %{"schema" => api_schema, "id" => id, "data" => string_data}) do
[api, schema] = case String.split(api_schema, "_api_") do
[api, _schema] -> [api, api_schema]
[schema] -> ["accounts", schema]
end
repo = @repos[api]
schema_module = @models[schema]
filtered_data = Enum.reduce string_data, %{}, fn {key, val}, acc ->
case val do
"" -> acc
_ -> Map.put acc, key, val
end
end
data = for {key, val} <- filtered_data, into: %{} do
field = String.to_atom(key)
if is_map(val) do
value = for {akey, aval} <- val, into: %{} do
afield = String.to_atom(akey)
{afield, aval}
end
{field, value}
else
case schema_module.__schema__(:type, field) do
:id -> {field, String.to_integer(val)}
:utc_datetime ->
{:ok, date_time, _offset} = DateTime.from_iso8601(val)
{field, date_time}
_ -> {field, val}
end
end
end
changeset = Ecto.Changeset.change(repo.get!(schema_module, id), data)
case repo.update(changeset) do
{:ok, _updated_model} ->
conn
|> put_flash(:info, "#{String.capitalize(schema)} ID #{id} updated!")
|> redirect(to: Routes.auto_admin_path(conn, :edit, schema, id))
{:error, changeset} ->
conn
|> put_flash(:failed, "#{String.capitalize(schema)} ID #{id} failed to update!")
|> put_session(:changeset, changeset)
|> redirect(to: Routes.auto_admin_path(conn, :edit, schema, id))
end
end
defp put_models(conn, _) do
assign(conn, :models, Map.keys(@models))
end
defp put_selects(conn, _) do
assign(conn, :selects, @selects)
end
end