diff --git a/lib/bdfr_browser/chat.ex b/lib/bdfr_browser/chat.ex index 3577c5d..e0f5c08 100644 --- a/lib/bdfr_browser/chat.ex +++ b/lib/bdfr_browser/chat.ex @@ -15,7 +15,7 @@ defmodule BdfrBrowser.Chat do def listing do from(c in __MODULE__, - left_join: m in assoc(c, :messages), + join: m in assoc(c, :messages), select: %{id: c.id, accounts: c.accounts, num_messages: count(m.id), latest_message: max(m.posted_at)}, order_by: [desc: max(m.posted_at)], group_by: c.id diff --git a/lib/bdfr_browser/comment.ex b/lib/bdfr_browser/comment.ex index 437da46..d1be3e3 100644 --- a/lib/bdfr_browser/comment.ex +++ b/lib/bdfr_browser/comment.ex @@ -1,6 +1,8 @@ defmodule BdfrBrowser.Comment do use Ecto.Schema + import Ecto.Query, only: [from: 2] + alias BdfrBrowser.Post @primary_key {:id, :string, autogenerate: false} @@ -16,4 +18,23 @@ defmodule BdfrBrowser.Comment do belongs_to :parent, __MODULE__ has_many :children, __MODULE__, foreign_key: :parent_id end + + def by_author(author) do + from(c in __MODULE__, + join: p in assoc(c, :post), + join: s in assoc(p, :subreddit), + select: %{ + body: c.body, + children: [], + posted_at: c.posted_at, + subreddit: s.name, + post_id: p.id, + post_title: p.title, + post_date: fragment("to_char(?, 'YYYY-MM')", p.posted_at) + }, + where: c.author == ^author, + order_by: [desc: c.posted_at], + group_by: [c.id, p.id, s.name] + ) + end end diff --git a/lib/bdfr_browser/http/plug.ex b/lib/bdfr_browser/http/plug.ex index 4e4b282..97136d5 100644 --- a/lib/bdfr_browser/http/plug.ex +++ b/lib/bdfr_browser/http/plug.ex @@ -1,7 +1,7 @@ defmodule BdfrBrowser.HTTP.Plug do use Plug.Router - alias BdfrBrowser.{Chat, Message, Repo, Post, Subreddit} + alias BdfrBrowser.{Chat, Comment, Message, Repo, Post, Subreddit} plug :match plug :dispatch @@ -88,6 +88,20 @@ defmodule BdfrBrowser.HTTP.Plug do |> send_resp(200, content) end + get "/user/:name" do + tpl_args = [ + name: name, + posts: name |> Post.by_author() |> Repo.all(), + comments: name |> Comment.by_author() |> Repo.all() + ] + + content = render_template("user", tpl_args) + + conn + |> put_resp_header("content-type", "text/html; charset=utf-8") + |> send_resp(200, content) + end + get "/static/*path" do file_path = Application.app_dir(:bdfr_browser, Path.join("priv/static", path)) diff --git a/lib/bdfr_browser/post.ex b/lib/bdfr_browser/post.ex index 26a6077..5cd1a14 100644 --- a/lib/bdfr_browser/post.ex +++ b/lib/bdfr_browser/post.ex @@ -37,7 +37,7 @@ defmodule BdfrBrowser.Post do def during_range(subreddit, start_date, end_date) do from(p in __MODULE__, - left_join: c in assoc(p, :comments), + join: c in assoc(p, :comments), select: %{id: p.id, title: p.title, author: p.author, posted_at: p.posted_at, num_comments: count(c.id)}, where: p.subreddit_id == ^subreddit.id and type(p.posted_at, :date) >= ^start_date and @@ -46,4 +46,22 @@ defmodule BdfrBrowser.Post do group_by: p.id ) end + + def by_author(author) do + from(p in __MODULE__, + join: c in assoc(p, :comments), + join: s in assoc(p, :subreddit), + select: %{ + id: p.id, + title: p.title, + posted_at: p.posted_at, + num_comments: count(c.id), + subreddit: s.name, + date: fragment("to_char(?, 'YYYY-MM')", p.posted_at) + }, + where: p.author == ^author, + order_by: [desc: p.posted_at], + group_by: [p.id, s.name] + ) + end end diff --git a/priv/templates/http/_comment.eex b/priv/templates/http/_comment.eex index cb5c187..a1333ec 100644 --- a/priv/templates/http/_comment.eex +++ b/priv/templates/http/_comment.eex @@ -9,7 +9,7 @@ <% end %> diff --git a/priv/templates/http/post.eex b/priv/templates/http/post.eex index 6ef5454..d582664 100644 --- a/priv/templates/http/post.eex +++ b/priv/templates/http/post.eex @@ -1,7 +1,7 @@ -

<%= post.title %>

+

<%= post.title %>

- <%= post.author %> + <%= post.author %> - <%= DateTime.to_iso8601(post.posted_at) %> - diff --git a/priv/templates/http/user.eex b/priv/templates/http/user.eex new file mode 100644 index 0000000..30688d0 --- /dev/null +++ b/priv/templates/http/user.eex @@ -0,0 +1,58 @@ +

<%= name %>

+ + + +
+
+
+
+ <%= for post <- posts do %> +
+
+
<%= post.title %>
+
+ <%= post.subreddit %> - + <%= post.num_comments %> comment(s) - + <%= Calendar.strftime(post.posted_at, "%Y-%m-%d") %> +
+
+
+ <% end %> +
+
+
+ +
+
+
+ <%= for comment <- comments do %> +
+
+
+ <%= BdfrBrowser.RenderUtils.comment(comment.body) %> + + +
+
+
+ <% end %> +
+
+
+