-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathhelpers.ex
More file actions
70 lines (62 loc) · 1.78 KB
/
helpers.ex
File metadata and controls
70 lines (62 loc) · 1.78 KB
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
defmodule LightningWeb.API.Helpers do
@moduledoc """
Helpers for the API views
"""
alias LightningWeb.Router.Helpers, as: Routes
def pagination_links(conn, page) do
%{
first: pagination_link(conn, page, :first),
last: pagination_link(conn, page, :last),
next: pagination_link(conn, page, :next),
prev: pagination_link(conn, page, :prev)
}
end
def pagination_link(conn, _page, :first) do
url_for(conn, conn.query_params |> Map.put("page", 1))
end
def pagination_link(conn, page, :last) do
url_for(conn, conn.query_params |> Map.put("page", page.total_pages))
end
def pagination_link(conn, page, :next) do
if page.page_number < page.total_pages do
url_for(
conn,
conn.query_params
|> Map.put("page", page.page_number + 1)
)
end
end
def pagination_link(conn, page, :prev) do
if page.page_number > 1 do
url_for(
conn,
conn.query_params
|> Map.put(
"page",
page.page_number - 1
)
)
end
end
def url_for(conn, params \\ %{}) do
%URI{
URI.new!(Routes.url(conn) <> conn.request_path)
| query: conn.query_params |> Map.merge(params) |> Plug.Conn.Query.encode()
}
|> URI.to_string()
end
@doc """
Validates that the given value is a well-formed UUID.
Returns `:ok` on success or `{:error, :bad_request}` when the value
cannot be parsed as a UUID. Use this in API controllers before passing
an ID to the database layer, which would raise `Ecto.Query.CastError`
for invalid values.
"""
@spec validate_uuid(any()) :: :ok | {:error, :bad_request}
def validate_uuid(id) do
case Ecto.UUID.dump(to_string(id)) do
{:ok, _bin} -> :ok
:error -> {:error, :bad_request}
end
end
end