-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsent_controller.ex
98 lines (80 loc) · 2.6 KB
/
sent_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
defmodule AppWeb.SentController do
use AppWeb, :controller
alias App.Ctx
alias App.Ctx.Sent
def index(conn, _params) do
sent = Ctx.list_sent()
render(conn, "index.html", sent: sent)
end
def new(conn, _params) do
changeset = Ctx.change_sent(%Sent{})
render(conn, "new.html", changeset: changeset)
end
def create(conn, %{"sent" => sent_params}) do
case Ctx.create_sent(sent_params) do
{:ok, sent} ->
conn
|> put_flash(:info, "Sent created successfully.")
|> redirect(to: Routes.sent_path(conn, :show, sent))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
def show(conn, %{"id" => id}) do
sent = Ctx.get_sent!(id)
render(conn, "show.html", sent: sent)
end
def edit(conn, %{"id" => id}) do
sent = Ctx.get_sent!(id)
changeset = Ctx.change_sent(sent)
render(conn, "edit.html", sent: sent, changeset: changeset)
end
def update(conn, %{"id" => id, "sent" => sent_params}) do
sent = Ctx.get_sent!(id)
case Ctx.update_sent(sent, sent_params) do
{:ok, sent} ->
conn
|> put_flash(:info, "Sent updated successfully.")
|> redirect(to: Routes.sent_path(conn, :show, sent))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "edit.html", sent: sent, changeset: changeset)
end
end
def delete(conn, %{"id" => id}) do
sent = Ctx.get_sent!(id)
{:ok, _sent} = Ctx.delete_sent(sent)
conn
|> put_flash(:info, "Sent deleted successfully.")
|> redirect(to: Routes.sent_path(conn, :index))
end
# test handler function I used while getting Heroku working:
def hello(conn, _params) do
data = %{"hello" => "world"}
conn
|> put_resp_header("content-type", "application/json;")
|> send_resp(200, Jason.encode!(data, pretty: true))
end
def unauthorized(conn, _params) do
conn
|> send_resp(401, "unauthorized")
|> halt()
end
def process_jwt(conn, _params) do
jwt = List.first(Plug.Conn.get_req_header(conn, "authorization"))
if is_nil(jwt) do
unauthorized(conn, nil)
else
case Enum.count(String.split(jwt, ".")) == 3 do
true -> # valid JWT proceed to verifying it
{:ok, claims} = App.Token.verify_and_validate(jwt)
sent = App.Ctx.upsert_sent(claims)
data = %{"id" => sent.id}
conn
|> put_resp_header("content-type", "application/json;")
|> send_resp(200, Jason.encode!(data, pretty: true))
false -> # invalid JWT return 401
unauthorized(conn, nil)
end
end
end
end