Skip to content

Commit 269676e

Browse files
hashrocketeervnegrisoloGabriel Reis
committed
Address PR comments
Co-authored-by: Vinicius Negrisolo <vinicius.negrisolo@hashrocket.com> Co-authored-by: Gabriel Reis <gabriel.reis@hashrocket.com>
1 parent 684f7e2 commit 269676e

4 files changed

Lines changed: 19 additions & 24 deletions

File tree

lib/tilex/stats.ex

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ defmodule Tilex.Stats do
55
alias Ecto.Adapters.SQL
66
alias Tilex.Blog.Channel
77
alias Tilex.Blog.Post
8+
alias Tilex.Posts
89
alias Tilex.Repo
910

1011
def developer(%{start_date: start_date, end_date: end_date}) do
@@ -13,10 +14,9 @@ defmodule Tilex.Stats do
1314

1415
posts_query =
1516
from(p in Post,
16-
where:
17-
not is_nil(p.published_at) and p.published_at <= fragment("now()") and
18-
between(p.inserted_at, ^start_time, ^end_time)
17+
where: between(p.inserted_at, ^start_time, ^end_time)
1918
)
19+
|> Posts.published()
2020

2121
[
2222
start_date: format_date(start_date),
@@ -35,11 +35,7 @@ defmodule Tilex.Stats do
3535

3636
def all do
3737
posts_for_days = query_posts_for_days!()
38-
39-
posts_query =
40-
from(p in Post,
41-
where: not is_nil(p.published_at) and p.published_at <= fragment("now()")
42-
)
38+
posts_query = Posts.published()
4339

4440
[
4541
channels: get_posts_by_channels_count(posts_query),

lib/tilex/tracking.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ defmodule Tilex.Tracking do
2525
where: matches?(req.page, "/posts/"),
2626
where: not matches?(req.page, "/posts/.+/edit$"),
2727
where: between(req.request_time, ^start_date, ^end_date),
28-
order_by: [desc: count(req.page)],
28+
order_by: [desc: count(req.page), asc: req.page],
2929
select: %{
3030
url: req.page,
3131
view_count: count(req.page),

lib/tilex_web/controllers/feed_controller.ex

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
defmodule TilexWeb.FeedController do
22
use TilexWeb, :controller
33

4+
alias Tilex.Posts
5+
46
def index(conn, _params) do
57
posts =
6-
Repo.all(
7-
from(
8-
p in Tilex.Blog.Post,
9-
where: not is_nil(p.published_at) and p.published_at <= fragment("now()"),
10-
order_by: [desc: p.inserted_at],
11-
preload: [:channel, :developer],
12-
limit: 25
13-
)
8+
from(
9+
p in Tilex.Blog.Post,
10+
order_by: [desc: p.inserted_at],
11+
preload: [:channel, :developer],
12+
limit: 25
1413
)
14+
|> Posts.published()
15+
|> Repo.all()
1516

1617
conn
1718
|> put_resp_content_type("application/xml")

lib/tilex_web/controllers/post_controller.ex

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,9 @@ defmodule TilexWeb.PostController do
6363
def show(%{assigns: %{slug: slug}} = conn, _) do
6464
post =
6565
from(p in Post,
66-
where:
67-
not is_nil(p.published_at) and p.published_at <= fragment("now()") and
68-
p.slug == ^slug
66+
where: p.slug == ^slug
6967
)
68+
|> Posts.published()
7069
|> Repo.one!(slug: slug)
7170
|> Repo.preload([:channel])
7271
|> Repo.preload([:developer])
@@ -78,16 +77,15 @@ defmodule TilexWeb.PostController do
7877
end
7978

8079
def random(conn, _) do
81-
query =
80+
post =
8281
from(
8382
post in Post,
84-
where: not is_nil(post.published_at) and post.published_at <= fragment("now()"),
8583
order_by: fragment("random()"),
8684
limit: 1,
8785
preload: [:channel, :developer]
8886
)
89-
90-
post = Repo.one(query)
87+
|> Posts.published()
88+
|> Repo.one()
9189

9290
conn
9391
|> assign(:meta_robots, "noindex")

0 commit comments

Comments
 (0)