Skip to content

Commit 70b8805

Browse files
author
Willian Yassunaka
committed
adding comment model
1 parent 3642682 commit 70b8805

7 files changed

Lines changed: 39 additions & 1 deletion

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
defmodule Discuss.Repo.Migrations.AddComments do
2+
use Ecto.Migration
3+
4+
def change do
5+
create table(:comments) do
6+
add :content, :string
7+
add :user_id, references(:users)
8+
add :topic_id, references(:topics)
9+
10+
timestamps()
11+
end
12+
end
13+
end

discuss/web/controllers/topic_controller.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ defmodule Discuss.TopicController do
1010
render conn, "index.html", topics: topics
1111
end
1212

13+
def show(conn, %{"id" => topic_id}) do
14+
topic = Repo.get!(Topic, topic_id)
15+
render conn, "show.html", topic: topic
16+
end
17+
1318
def new(conn, _params) do
1419
changeset = Topic.changeset(%Topic{}, %{})
1520

discuss/web/models/comment.ex

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
defmodule Discuss.Comment do
2+
use Discuss.Web, :model
3+
4+
schema "comments" do
5+
field :content, :string
6+
belongs_to :user, Discuss.User
7+
belongs_to :topic, Discuss.Topic
8+
9+
timestamps()
10+
end
11+
12+
def changeset(struct, params \\ %{}) do
13+
struct
14+
|> cast(params, [:content])
15+
|> validate_required([:content])
16+
end
17+
end

discuss/web/models/topic.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ defmodule Discuss.Topic do
44
schema "topics" do
55
field :title, :string
66
belongs_to :user, Discuss.User
7+
has_many :comments, Discuss.Comment
78
end
89

910
def changeset(struct, params \\ %{}) do

discuss/web/models/user.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ defmodule Discuss.User do
66
field :provider, :string
77
field :token, :string
88
has_many :topics, Discuss.Topic
9+
has_many :comments, Discuss.Comment
910

1011
timestamps()
1112
end

discuss/web/templates/topic/index.html.eex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<ul class="collection">
44
<%= for topic <- @topics do %>
55
<li class="collection-item">
6-
<%= topic.title %>
6+
<%= link topic.title, to: topic_path(@conn, :show, topic) %>
77

88
<%= if @conn.assigns.user.id == topic.user_id do %>
99
<div class="right">
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%= @topic.title %>

0 commit comments

Comments
 (0)