This repository was archived by the owner on Sep 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathprofile_controller.ex
More file actions
64 lines (53 loc) · 1.64 KB
/
profile_controller.ex
File metadata and controls
64 lines (53 loc) · 1.64 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
defmodule RealWorldWeb.ProfileController do
use RealWorldWeb, :controller
use RealWorldWeb.GuardedController
alias RealWorld.Accounts.{Users, User}
action_fallback(RealWorldWeb.FallbackController)
plug(Guardian.Plug.EnsureAuthenticated when action in [:follow, :unfollow])
def show(conn, %{"username" => username}, current_user) do
case Users.get_by_username(username) do
user = %User{} ->
conn
|> put_status(:ok)
|> render("show.json", user: user, following: Users.is_following?(current_user, user))
nil ->
conn
|> put_status(:not_found)
|> render(RealWorldWeb.ErrorView, "404.json")
end
end
def follow(conn, %{"username" => username}, current_user) do
case Users.get_by_username(username) do
followee = %User{} ->
Users.follow(current_user, followee)
conn
|> put_status(:ok)
|> render(
"show.json",
user: followee,
following: Users.is_following?(current_user, followee)
)
nil ->
conn
|> put_status(:not_found)
|> render(RealWorldWeb.ErrorView, "404.json")
end
end
def unfollow(conn, %{"username" => username}, current_user) do
case Users.get_by_username(username) do
followee = %User{} ->
Users.unfollow(current_user, followee)
conn
|> put_status(:ok)
|> render(
"show.json",
user: followee,
following: Users.is_following?(current_user, followee)
)
nil ->
conn
|> put_status(:not_found)
|> render(RealWorldWeb.ErrorView, "404.json")
end
end
end