Skip to content
This repository has been archived by the owner on Sep 27, 2022. It is now read-only.

Avoid using the pipe operator just once. #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions lib/real_world/accounts/users.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ defmodule RealWorld.Accounts.Users do
end

def unfollow(user, followee) do
relation =
UserFollower
|> Repo.get_by(user_id: user.id, followee_id: followee.id)
relation = Repo.get_by(UserFollower, user_id: user.id, followee_id: followee.id)

case relation do
nil ->
Expand Down
18 changes: 6 additions & 12 deletions lib/real_world_web/controllers/article_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ defmodule RealWorldWeb.ArticleController do

def index(conn, params, user) do
articles =
Blog.list_articles(params)
params
|> Blog.list_articles()
|> Repo.preload([:author, :favorites])
|> Blog.load_favorites(user)

Expand All @@ -37,9 +38,7 @@ defmodule RealWorldWeb.ArticleController do

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])
article = Repo.preload(article, [:author, :favorites])

conn
|> put_status(:created)
Expand All @@ -49,8 +48,7 @@ defmodule RealWorldWeb.ArticleController do
end

defp create_params(params, user) do
params
|> Map.merge(%{"user_id" => user.id})
Map.merge(params, %{"user_id" => user.id})
end

def show(conn, %{"id" => slug}, user) do
Expand All @@ -76,9 +74,7 @@ defmodule RealWorldWeb.ArticleController do
end

def favorite(conn, %{"slug" => slug}, user) do
article =
slug
|> Blog.get_article_by_slug!()
article = Blog.get_article_by_slug!(slug)

with {:ok, %Favorite{}} <- Blog.favorite(user, article) do
article =
Expand All @@ -91,9 +87,7 @@ defmodule RealWorldWeb.ArticleController do
end

def unfavorite(conn, %{"slug" => slug}, user) do
article =
slug
|> Blog.get_article_by_slug!()
article = Blog.get_article_by_slug!(slug)

with {:ok, _} <- Blog.unfavorite(article, user) do
article =
Expand Down
7 changes: 3 additions & 4 deletions lib/real_world_web/controllers/comment_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ defmodule RealWorldWeb.CommentController do
plug(Guardian.Plug.EnsureAuthenticated when action in [:create, :update, :delete])

def index(conn, %{"article_id" => slug}, _user) do
article = Blog.get_article_by_slug!(slug)
comments = Blog.list_comments(article)

comments =
comments
slug
|> Blog.get_article_by_slug!()
|> Blog.list_comments()
|> RealWorld.Repo.preload(:author)

render(conn, "index.json", comments: comments)
Expand Down
6 changes: 2 additions & 4 deletions lib/real_world_web/controllers/profile_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ defmodule RealWorldWeb.ProfileController do
def follow(conn, %{"username" => username}, current_user) do
case Users.get_by_username(username) do
followee = %User{} ->
current_user
|> Users.follow(followee)
Users.follow(current_user, followee)

conn
|> put_status(:ok)
Expand All @@ -46,8 +45,7 @@ defmodule RealWorldWeb.ProfileController do
def unfollow(conn, %{"username" => username}, current_user) do
case Users.get_by_username(username) do
followee = %User{} ->
current_user
|> Users.unfollow(followee)
Users.unfollow(current_user, followee)

conn
|> put_status(:ok)
Expand Down
3 changes: 1 addition & 2 deletions lib/real_world_web/controllers/session_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ defmodule RealWorldWeb.SessionController do
def create(conn, params) do
case Auth.find_user_and_check_password(params) do
{:ok, user} ->
{:ok, jwt, _full_claims} =
user |> RealWorldWeb.Guardian.encode_and_sign(%{}, token_type: :token)
{:ok, jwt, _full_claims} = RealWorldWeb.Guardian.encode_and_sign(user, %{}, token_type: :token)

conn
|> put_status(:created)
Expand Down
3 changes: 1 addition & 2 deletions lib/real_world_web/controllers/user_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ defmodule RealWorldWeb.UserController do
def create(conn, %{"user" => user_params}, _) do
case Auth.register(user_params) do
{:ok, user} ->
{:ok, jwt, _full_claims} =
user |> RealWorldWeb.Guardian.encode_and_sign(%{}, token_type: :token)
{:ok, jwt, _full_claims} = RealWorldWeb.Guardian.encode_and_sign(user, %{}, token_type: :token)

conn
|> put_status(:created)
Expand Down