Skip to content

Commit

Permalink
Reuse Pageable module across all controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
vnegrisolo committed Dec 16, 2022
1 parent ed1e814 commit 48a6246
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 28 deletions.
10 changes: 2 additions & 8 deletions lib/tilex/pageable.ex
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
defmodule Tilex.Pageable do
defmacro __using__(_params) do
quote do
import Tilex.Pageable
end
end

def robust_page(%{"page" => page}) do
case Integer.parse(page) do
:error -> 1
{integer, _remainder} -> integer
{integer, ""} -> integer
_ -> 1
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/tilex_web/controllers/api/post_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule TilexWeb.Api.PostController do
"

use TilexWeb, :controller
use Tilex.Pageable
import Tilex.Pageable
alias Tilex.Posts

@doc """
Expand Down
8 changes: 2 additions & 6 deletions lib/tilex_web/controllers/channel_controller.ex
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
defmodule TilexWeb.ChannelController do
use TilexWeb, :controller

import Tilex.Pageable
alias Tilex.Posts

def show(conn, %{"name" => channel_name} = params) do
page =
params
|> Map.get("page", "1")
|> String.to_integer()

page = robust_page(params)
{posts, posts_count, channel} = Posts.by_channel(channel_name, page)

render(
Expand Down
8 changes: 2 additions & 6 deletions lib/tilex_web/controllers/developer_controller.ex
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
defmodule TilexWeb.DeveloperController do
use TilexWeb, :controller

import Tilex.Pageable
alias Tilex.Blog.Developer
alias Tilex.Posts
alias Tilex.Repo
alias Tilex.Auth

def show(conn, %{"name" => username} = params) do
page =
params
|> Map.get("page", "1")
|> String.to_integer()

page = robust_page(params)
{posts, posts_count, developer} = Posts.by_developer(username, page)

render(
Expand Down
13 changes: 6 additions & 7 deletions lib/tilex_web/controllers/post_controller.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
defmodule TilexWeb.PostController do
use TilexWeb, :controller
use Tilex.Pageable
import Tilex.Pageable
import Ecto.Query

alias Guardian.Plug
alias Tilex.Blog.Channel
alias Tilex.Notifications
alias Tilex.Liking
Expand All @@ -14,7 +13,7 @@ defmodule TilexWeb.PostController do
plug(:extract_slug when action in [:show, :edit, :update])

plug(
Plug.EnsureAuthenticated,
Guardian.Plug.EnsureAuthenticated,
[error_handler: __MODULE__]
when action in ~w(new create edit update)a
)
Expand Down Expand Up @@ -84,7 +83,7 @@ defmodule TilexWeb.PostController do
end

def new(conn, _params) do
current_user = Plug.current_resource(conn)
current_user = Guardian.Plug.current_resource(conn)
changeset = Post.changeset(%Post{})

conn
Expand All @@ -110,7 +109,7 @@ defmodule TilexWeb.PostController do
end

def create(conn, %{"post" => params}) do
developer = Plug.current_resource(conn)
developer = Guardian.Plug.current_resource(conn)

sanitized_params =
params
Expand All @@ -136,7 +135,7 @@ defmodule TilexWeb.PostController do
end

def edit(conn, _params) do
current_user = Plug.current_resource(conn)
current_user = Guardian.Plug.current_resource(conn)

post =
case current_user.admin do
Expand All @@ -159,7 +158,7 @@ defmodule TilexWeb.PostController do
end

def update(conn, %{"post" => params}) do
current_user = Plug.current_resource(conn)
current_user = Guardian.Plug.current_resource(conn)

post =
case current_user.admin do
Expand Down

0 comments on commit 48a6246

Please sign in to comment.