Browse Source

fix: order posts by date desc

master
Inhji Y. 10 months ago
parent
commit
71449ccb62
  1. 12
      apps/blog/lib/blog.ex
  2. 4
      apps/tomie_web/lib/tomie_web/controllers/home_controller.ex
  3. 6
      apps/tomie_web/lib/tomie_web/templates/home/index.html.eex

12
apps/blog/lib/blog.ex

@ -26,12 +26,21 @@ defmodule Blog do
Db.Repo.get!(Post, id)
|> Db.Repo.preload(@preloads)
def list_posts(limit \\ 10) do
Post
|> where(type: "post")
|> limit(^limit)
|> order_by([p], desc: p.inserted_at)
|> Db.Repo.all()
|> Db.Repo.preload(@preloads)
end
def list_notes(limit \\ 10) do
Post
|> where(type: "post")
|> where([p], is_nil(p.title))
|> limit(^limit)
|> order_by([p], desc: p.inserted_at)
|> Db.Repo.all()
|> Db.Repo.preload(@preloads)
end
@ -41,6 +50,7 @@ defmodule Blog do
|> where(type: "post")
|> where([p], not is_nil(p.title))
|> limit(^limit)
|> order_by([p], desc: p.inserted_at)
|> Db.Repo.all()
|> Db.Repo.preload(@preloads)
end

4
apps/tomie_web/lib/tomie_web/controllers/home_controller.ex

@ -10,7 +10,7 @@ defmodule TomieWeb.HomeController do
def index(conn, _params) do
bookmarks = Bookmarks.list_bookmarks("", "recent", 3)
posts = Blog.list_posts(1)
notes = Blog.list_notes(1)
articles = Blog.list_articles(3)
listens = Listens.Listens.list_listens(3)
user = Tomie.Users.get_user!(1)
@ -18,7 +18,7 @@ defmodule TomieWeb.HomeController do
conn
|> render("index.html",
bookmarks: bookmarks,
posts: posts,
notes: notes,
articles: articles,
listens: listens,
page_title: "Home",

6
apps/tomie_web/lib/tomie_web/templates/home/index.html.eex

@ -45,9 +45,9 @@
<h3>Notes</h3>
<dl>
<%= for p <- @posts do %>
<dt><%= raw p.content_html %></dt>
<dd><%= Timex.from_now(p.inserted_at) %></dd>
<%= for n <- @notes do %>
<dt><%= raw n.content_html %></dt>
<dd><%= Timex.from_now(n.inserted_at) %></dd>
<% end %>
</dl>
</div>

Loading…
Cancel
Save