Merge branch '@feature/repost_fk' into develop

This commit is contained in:
Fergal Moran
2022-12-05 22:24:37 +00:00
13 changed files with 118 additions and 38 deletions

View File

@@ -18,6 +18,20 @@ defmodule Malarkey.Accounts.User do
on_replace: :delete
)
many_to_many(
:dislikes,
Post,
join_through: Malarkey.Timeline.PostUserDislike,
on_replace: :delete
)
many_to_many(
:reposts,
Post,
join_through: Malarkey.Timeline.PostUserReposts,
on_replace: :delete
)
timestamps()
end

View File

@@ -17,9 +17,17 @@ defmodule Malarkey.Timeline do
|> Repo.all()
|> Repo.preload(:user)
|> Repo.preload(:liked_by)
|> Repo.preload(:disliked_by)
|> Repo.preload(:reposted_by)
end
def get_post!(id), do: Repo.get!(Post, id)
def get_post!(id) do
Repo.get!(Post, id)
|> Repo.preload(:user)
|> Repo.preload(:liked_by)
|> Repo.preload(:disliked_by)
|> Repo.preload(:reposted_by)
end
def create_post(user, attrs \\ %{}) do
%Post{user_id: user.id}
@@ -52,6 +60,15 @@ defmodule Malarkey.Timeline do
|> broadcast(:post_created)
end
def add_repost(user, post) do
post
|> Repo.preload(:reposted_by)
|> Repo.preload(:user)
|> Post.changeset_add_repost(user)
|> Repo.update()
|> broadcast(:post_created)
end
def subscribe do
Phoenix.PubSub.subscribe(Malarkey.PubSub, "posts")
end

View File

@@ -5,8 +5,6 @@ defmodule Malarkey.Timeline.Post do
schema "posts" do
field :body, :string
field :repost_count, :integer, default: 0
belongs_to(:user, User)
many_to_many(
@@ -16,6 +14,20 @@ defmodule Malarkey.Timeline.Post do
on_replace: :delete
)
many_to_many(
:disliked_by,
User,
join_through: Malarkey.Timeline.PostUserDislike,
on_replace: :delete
)
many_to_many(
:reposted_by,
User,
join_through: Malarkey.Timeline.PostUserRepost,
on_replace: :delete
)
timestamps()
end
@@ -37,18 +49,15 @@ defmodule Malarkey.Timeline.Post do
|> validate_length(:body, min: 2, max: 250)
end
@spec changeset_add_like(
{map, map}
| %{
:__struct__ => atom | %{:__changeset__ => map, optional(any) => any},
optional(atom) => any
},
any,
:invalid | %{optional(:__struct__) => none, optional(atom | binary) => any}
) :: Ecto.Changeset.t()
def changeset_add_like(post, user, attrs \\ %{}) do
post
|> changeset(attrs)
|> put_assoc(:liked_by, [user])
end
def changeset_add_repost(post, user, attrs \\ %{}) do
post
|> changeset(attrs)
|> put_assoc(:reposted_by, [user])
end
end

View File

@@ -0,0 +1,10 @@
defmodule Malarkey.Timeline.PostUserDislike do
use Ecto.Schema
@primary_key false
schema "user_dislikes" do
belongs_to :user, Malarkey.Accounts.User, primary_key: true
belongs_to :post, Malarkey.Timeline.Post, primary_key: true
timestamps()
end
end

View File

@@ -0,0 +1,10 @@
defmodule Malarkey.Timeline.PostUserRepost do
use Ecto.Schema
@primary_key false
schema "user_reposts" do
belongs_to :user, Malarkey.Accounts.User, primary_key: true
belongs_to :post, Malarkey.Timeline.Post, primary_key: true
timestamps()
end
end

View File

@@ -54,12 +54,6 @@ defmodule MalarkeyWeb.PostLive.FormComponent do
save_post(socket, socket.assigns.action, post_params)
end
@impl true
def handle_event("like", _, socket) do
Timeline.add_like(socket.assigns.current_user, socket.assigns.post)
{:noreply, socket}
end
defp save_post(socket, :like, _post_params) do
case Timeline.add_like(socket.assigns.current_user, socket.assigns.post) do
{:ok, _post} ->

View File

@@ -50,6 +50,14 @@ defmodule MalarkeyWeb.PostLive.Index do
{:noreply, assign(socket, :posts, list_posts())}
end
@impl true
def handle_event("repost", %{"id" => id}, socket) do
post = Timeline.get_post!(id)
{:ok, _} = Timeline.add_repost(socket.assigns.current_user, post)
{:noreply, assign(socket, :posts, list_posts())}
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
post = Timeline.get_post!(id)

View File

@@ -12,7 +12,7 @@
<div id="posts" phx-update="prepend">
<%= if !!@current_user do %>
<h1 id="user-greeting">Hello, <%= !!@current_user.fullname %></h1>
<h1 id="user-greeting">Hi, <%= !!@current_user.fullname %></h1>
<% else %>
<h1 id="anonymous-greeting">Hello, Sailor!</h1>
<% end %>

View File

@@ -74,28 +74,16 @@ defmodule MalarkeyWeb.PostLive.PostComponent do
</div>
<div class="flex items-center mr-8 text-gray-600 hover:text-green-500">
<a
href="#"
href="#"
phx-click="retweet"
phx-target={@myself}
class="flex items-center justify-center w-8 h-8 rounded-full hover:bg-green-200 hover:text-green-500"
>
<.link phx-click={JS.push("repost", value: %{id: @post.id})}>
<%= Heroicons.icon("arrow-path-rounded-square", type: "outline", class: "w-5 h-5") %>
</a>
<span class="ml-1">6.7K</span>
</.link>
<span class="ml-1"><%= length(@post.reposted_by) %></span>
</div>
<div class="flex items-center mr-6 text-gray-600 hover:text-red-500">
<%!-- <.link phx-click="like" phx-target={@myself}>
<%= Heroicons.icon("heart", type: "outline", class: "w-5 h-5") %>
</.link> --%>
<.link phx-click={JS.push("like", value: %{id: @post.id})}>
<%= Heroicons.icon("heart", type: "outline", class: "w-5 h-5") %>
</.link>
<%!-- <.link patch={~p"/posts/#{@post}/like"}>
<%= Heroicons.icon("heart", type: "outline", class: "w-5 h-5") %>
</.link> --%>
<span class="ml-1"><%= length(@post.liked_by) %></span>
</div>

View File

@@ -4,8 +4,6 @@ defmodule Malarkey.Repo.Migrations.CreatePosts do
def change do
create table(:posts) do
add :body, :string
add :repost_count, :integer
add :user_id, references(:users, on_delete: :delete_all), null: false
timestamps()

View File

@@ -1,4 +1,4 @@
defmodule Malarkey.Repo.Migrations.AddUserFks do
defmodule Malarkey.Repo.Migrations.AddUserLikes do
use Ecto.Migration
def change do
@@ -11,6 +11,6 @@ defmodule Malarkey.Repo.Migrations.AddUserFks do
create(index(:user_likes, [:post_id]))
create(index(:user_likes, [:user_id]))
create(unique_index(:user_likes, [:user_id, :post_id], name: :user_id_post_id_unique_index))
create(unique_index(:user_likes, [:user_id, :post_id], name: :user_likes_unique_index))
end
end

View File

@@ -0,0 +1,16 @@
defmodule Malarkey.Repo.Migrations.AddUserDislikes do
use Ecto.Migration
def change do
create table(:user_dislikes, primary_key: false) do
add(:post_id, references(:posts, on_delete: :delete_all), primary_key: true)
add(:user_id, references(:users, on_delete: :delete_all), primary_key: true)
timestamps()
end
create(index(:user_dislikes, [:post_id]))
create(index(:user_dislikes, [:user_id]))
create(unique_index(:user_dislikes, [:user_id, :post_id], name: :user_dislikes_unique_index))
end
end

View File

@@ -0,0 +1,16 @@
defmodule Malarkey.Repo.Migrations.AddUserReposts do
use Ecto.Migration
def change do
create table(:user_reposts, primary_key: false) do
add(:post_id, references(:posts, on_delete: :delete_all), primary_key: true)
add(:user_id, references(:users, on_delete: :delete_all), primary_key: true)
timestamps()
end
create(index(:user_reposts, [:post_id]))
create(index(:user_reposts, [:user_id]))
create(unique_index(:user_reposts, [:user_id, :post_id], name: :user_reposts_unique_index))
end
end