Skip to content

Commit ce74e9b

Browse files
Adds comments to stories
1 parent 3219cd9 commit ce74e9b

File tree

11 files changed

+102
-17
lines changed

11 files changed

+102
-17
lines changed

mix.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"ecto": {:hex, :ecto, "2.1.4", "d1ba932813ec0e0d9db481ef2c17777f1cefb11fc90fa7c142ff354972dfba7e", [:mix], [{:db_connection, "~> 1.1", [hex: :db_connection, repo: "hexpm", optional: true]}, {:decimal, "~> 1.2", [hex: :decimal, repo: "hexpm", optional: false]}, {:mariaex, "~> 0.8.0", [hex: :mariaex, repo: "hexpm", optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.13.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"},
1313
"fs": {:hex, :fs, "0.9.2", "ed17036c26c3f70ac49781ed9220a50c36775c6ca2cf8182d123b6566e49ec59", [:rebar], [], "hexpm"},
1414
"gettext": {:hex, :gettext, "0.13.1", "5e0daf4e7636d771c4c71ad5f3f53ba09a9ae5c250e1ab9c42ba9edccc476263", [:mix], [], "hexpm"},
15+
"gravity": {:hex, :gravity, "1.0.0", "59766085dc8895803f758b595d61df2ab6440b4d3d47f95b36797d999fe63893", [:mix], [], "hexpm"},
1516
"hackney": {:hex, :hackney, "1.8.6", "21a725db3569b3fb11a6af17d5c5f654052ce9624219f1317e8639183de4a423", [:rebar3], [{:certifi, "1.2.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "5.0.2", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
1617
"hound": {:hex, :hound, "1.0.3", "bf1859fcb855bf7a3b84c632ba68f04c43bfeb16efebf0080b6c1efb960c30c6", [:mix], [{:hackney, "~> 1.5", [hex: :hackney, repo: "hexpm", optional: false]}, {:poison, ">= 1.4.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"},
1718
"idna": {:hex, :idna, "5.0.2", "ac203208ada855d95dc591a764b6e87259cb0e2a364218f215ad662daa8cd6b4", [:rebar3], [{:unicode_util_compat, "0.2.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},

priv/repo/seeds.exs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,12 @@ Fmylife.Repo.delete_all Fmylife.Story
4747
%{body: "The quick brown fox jumps over the lazy dog. #{n}"})
4848
|> Fmylife.Repo.insert!
4949
end)
50+
51+
Fmylife.Repo.delete_all Fmylife.Comment
52+
(1..900) |> Enum.each(fn n ->
53+
Fmylife.Comment.changeset(%Fmylife.Comment{
54+
user_id: :rand.uniform(10),
55+
story_id: :rand.uniform(60)},
56+
%{body: "Locavore cardigan street art vice iPhone woke. #{n}"})
57+
|> Fmylife.Repo.insert!
58+
end)

web/commanders/story_commander.ex

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
defmodule Fmylife.StoryCommander do
22
use Drab.Commander
3-
alias Fmylife.{Like, Repo}
3+
alias Fmylife.{Like, Repo, Comment, User}
44

55
access_session :user_id
66

7+
def publish_comment(socket, sender) do
8+
user = get_session(socket, :user_id)
9+
story_id = socket |> select(data: "storyId", from: this(sender))
10+
comment = socket |> select(:val, from: "#comment_body")
11+
changeset = Comment.changeset(%Comment{user_id: user, story_id: story_id}, %{body: comment})
12+
13+
case Repo.insert(changeset) do
14+
{:ok, comment} ->
15+
comment = Repo.preload(comment, [:user])
16+
html = render_to_string("_comment.html", [comment: comment])
17+
socket
18+
|> insert!(html, prepend: "#comments")
19+
|> update(:val, set: "", on: "#comment_body")
20+
{:error, changeset} ->
21+
socket |> exec_js("alert('You need to fill the form')")
22+
end
23+
end
24+
725
def like(socket, dom_sender) do
826
user = get_session(socket, :user_id)
927
story_id = socket |> select(data: "storyId", from: this(dom_sender))

web/controllers/comment_controller.ex

Lines changed: 0 additions & 9 deletions
This file was deleted.

web/controllers/story_controller.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,12 @@ defmodule Fmylife.StoryController do
4242
end
4343

4444
def show(conn, %{"id" => id}) do
45-
story = Repo.get!(Story, id)
46-
render(conn, :show, story: story)
45+
changeset = Comment.changeset(%Comment{})
46+
story = Repo.preload(
47+
Repo.get!(Story, id),
48+
[:user, {:comments, :user}]
49+
)
50+
render(conn, :show, story: story, changeset: changeset)
4751
end
4852

4953
def top_stories(conn, _params) do

web/models/comment.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,12 @@ defmodule Fmylife.Comment do
1717
|> cast(params, [:body])
1818
|> validate_required([:body])
1919
end
20+
21+
def count(story_id) do
22+
hd Fmylife.Repo.all(
23+
from c in Fmylife.Comment,
24+
select: count(c.id),
25+
where: [story_id: ^story_id]
26+
)
27+
end
2028
end

web/templates/story/_comment.html.eex

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<div class="media">
2+
<div class="media-left">
3+
<a href="#">
4+
<img class="media-object" src="<%= gravatar_for(@comment.user.email) %>" alt="<%= @comment.user.email %>">
5+
</a>
6+
</div>
7+
<div class="media-body">
8+
<div class="panel panel-default">
9+
<div class="panel-heading">
10+
<%= @comment.user.name %>
11+
<span class="text-muted pull-right">
12+
<small><%= time_ago_in_words(@comment.inserted_at) %></small>
13+
</span>
14+
</div>
15+
<div class="panel-body">
16+
<%= @comment.body %>
17+
</div>
18+
</div>
19+
</div>
20+
</div><!-- /.media -->
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<div class="form-group">
2+
<label class="control-label" for="comment_Add_a_comment">
3+
Add a comment
4+
</label>
5+
<textarea class="form-control" id="comment_body" rows="6"></textarea>
6+
</div>
7+
8+
<div class="form-group text-center">
9+
<button drab-click="publish_comment" data-story-id="<%= @story %>" id="comment-publish" class="btn btn-primary" type="submit">
10+
Publish
11+
</button>
12+
</div>

web/templates/story/_stories.html.eex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020
<span id="total-dislikes-<%= @story.id %>" class="text-warning">
2121
(<%= Fmylife.Like.total_dislikes(@story.id) %>)
2222
</span>
23-
<a href="#" class="btn btn-link btn-xs">Comments</a><small><em> (200)</em></small>
23+
<%= link "Comments", to: story_path(@conn, :show, @story), class: "btn btn-link btn-xs" %></a><small><em> (<%= Fmylife.Comment.count(@story.id) %>)</em></small>
2424
<div class="text-muted pull-right"><small>Created by: <%= @story.user.name %>&nbsp;<%= time_ago_in_words(@story.inserted_at) %></small></div>
2525
</div><!-- /.well -->

web/templates/story/show.html.eex

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
<div class="container">
22
<div class="row">
3-
<hr>
4-
<div class="row">
5-
<%= @story.body %>
6-
</div>
3+
<div class="col-md-8 col-md-offset-2">
4+
<%= render_one @story, StoryView, "_stories.html", conn: @conn, current_user: @current_user %>
5+
6+
<div class="form-area">
7+
<div class="well">
8+
<%= render "_comment_form.html", story: @story.id %>
9+
</div>
10+
</div>
11+
<div id="comments">
12+
<%= render_many @story.comments, StoryView, "_comment.html", as: :comment %>
13+
</div>
14+
</div><!-- /.col-md-8 col-md-offset-2 -->
715
</div>
816
</div>

0 commit comments

Comments
 (0)