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 patharticle_controller.ex
More file actions
112 lines (91 loc) · 2.73 KB
/
article_controller.ex
File metadata and controls
112 lines (91 loc) · 2.73 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
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
defmodule RealWorldWeb.ArticleController do
use RealWorldWeb, :controller
use RealWorldWeb.GuardedController
alias RealWorld.{Blog, Repo}
alias RealWorld.Blog.{Article, Favorite}
action_fallback(RealWorldWeb.FallbackController)
plug(
Guardian.Plug.EnsureAuthenticated
when action in [
:create,
:update,
:delete,
:favorite
]
)
def index(conn, params, user) do
articles =
Blog.list_articles(params)
|> Repo.preload([:author, :favorites])
|> Blog.load_favorites(user)
render(conn, "index.json", articles: articles)
end
def feed(conn, _params, user) do
articles =
user
|> Blog.feed()
|> Repo.preload([:author, :favorites])
render(conn, "index.json", articles: articles)
end
def create(conn, %{"article" => params}, user) do
with {:ok, %Article{} = article} <- Blog.create_article(create_params(params, user)) do
article =
article
|> Repo.preload([:author, :favorites])
conn
|> put_status(:created)
|> put_resp_header("location", Routes.article_path(conn, :show, article))
|> render("show.json", article: article)
end
end
defp create_params(params, user) do
params
|> Map.merge(%{"user_id" => user.id})
end
def show(conn, %{"id" => slug}, user) do
article =
slug
|> Blog.get_article_by_slug!()
|> Repo.preload([:author, :favorites])
|> Blog.load_favorite(user)
render(conn, "show.json", article: article)
end
def update(conn, %{"id" => id, "article" => article_params}, user) do
article =
id
|> Blog.get_article!()
|> Repo.preload([:author, :favorites])
|> Blog.load_favorite(user)
with {:ok, %Article{} = article} <- Blog.update_article(article, article_params) do
render(conn, "show.json", article: article)
end
end
def favorite(conn, %{"slug" => slug}, user) do
article =
slug
|> Blog.get_article_by_slug!()
with {:ok, %Favorite{}} <- Blog.favorite(user, article) do
article =
article
|> Repo.preload([:author, :favorites])
|> Blog.load_favorite(user)
render(conn, "show.json", article: Blog.load_favorite(article, user))
end
end
def unfavorite(conn, %{"slug" => slug}, user) do
article =
slug
|> Blog.get_article_by_slug!()
with {:ok, _} <- Blog.unfavorite(article, user) do
article =
article
|> Repo.preload([:author, :favorites])
|> Blog.load_favorite(user)
render(conn, "show.json", article: Blog.load_favorite(article, user))
end
end
def delete(conn, %{"id" => slug}, _user) do
Blog.delete_article(slug)
send_resp(conn, :no_content, "")
end
end