From e2e635a5a7976265b8da25a2df8f623cb6e107d5 Mon Sep 17 00:00:00 2001 From: Muhammad Rafif Elfazri <rafif.elfazri@gmail.com> Date: Fri, 8 Jan 2021 23:36:36 +0700 Subject: [PATCH 1/2] Add like system for post --- diskuy_back/lib/diskuy/likes.ex | 4 +++- .../diskuy_web/controllers/post_controller.ex | 24 +++++++++++++++++++ .../controllers/post_like_controller.ex | 7 ++++++ diskuy_back/lib/diskuy_web/router.ex | 8 +++++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/diskuy_back/lib/diskuy/likes.ex b/diskuy_back/lib/diskuy/likes.ex index 4ca5897..b659d03 100644 --- a/diskuy_back/lib/diskuy/likes.ex +++ b/diskuy_back/lib/diskuy/likes.ex @@ -37,7 +37,7 @@ defmodule Diskuy.Likes do """ def get_thread_like!(id), do: Repo.get!(ThreadLike, id) - def get_thread_like_by_refer!(user_id, thread_id), do: Repo.get_by!(ThreadLike, [user_id: user_id, thread_id: thread_id]) + def get_post_like_by_refer!(user_id, thread_id), do: Repo.get_by!(ThreadLike, [user_id: user_id, thread_id: thread_id]) @doc """ Creates a thread_like. @@ -135,6 +135,8 @@ defmodule Diskuy.Likes do """ def get_post_like!(id), do: Repo.get!(PostLike, id) + def get_thread_like_by_refer!(user_id, post_id), do: Repo.get_by!(PostLike, [user_id: user_id, post_id: post_id]) + @doc """ Creates a post_like. diff --git a/diskuy_back/lib/diskuy_web/controllers/post_controller.ex b/diskuy_back/lib/diskuy_web/controllers/post_controller.ex index b48f5ef..7de76bd 100644 --- a/diskuy_back/lib/diskuy_web/controllers/post_controller.ex +++ b/diskuy_back/lib/diskuy_web/controllers/post_controller.ex @@ -3,6 +3,8 @@ defmodule DiskuyWeb.PostController do alias Diskuy.Forum alias Diskuy.Forum.Post + alias Diskuy.Likes + alias Diskuy.Likes.PostLike action_fallback DiskuyWeb.FallbackController @@ -42,6 +44,28 @@ defmodule DiskuyWeb.PostController do end end + def add_like(conn, %{"id" => id}) do + current_user = Guardian.Plug.current_resource(conn) + post = Forum.get_post!(id) + + with {:ok, %PostLike{}} <- Likes.create_post_like(%{"user_id" => current_user.id, + "post_id" => id}), + {:ok, %Post{} = post} <- Forum.update_post(post, %{"points" => (post.points+1)}) do + render(conn, "show.json", post: post) + end + end + + def delete_like(conn, %{"id" => id}) do + current_user = Guardian.Plug.current_resource(conn) + post = Forum.get_post!(id) + post_like = Likes.get_post_like_by_refer!(current_user.id, id) + + with {:ok, %PostLike{}} <- Likes.delete_post_like(post_like), + {:ok, %Post{}} <- Forum.update_post(post, %{"points" => (post.points-1)}) do + render(conn, "show.json", post: post) + end + end + defp put_user_id(conn, %{"post" => post_params}) do current_user = Guardian.Plug.current_resource(conn) new_params = Map.put(post_params, "user_id", current_user.id) diff --git a/diskuy_back/lib/diskuy_web/controllers/post_like_controller.ex b/diskuy_back/lib/diskuy_web/controllers/post_like_controller.ex index 5237ccd..a1d85a4 100644 --- a/diskuy_back/lib/diskuy_web/controllers/post_like_controller.ex +++ b/diskuy_back/lib/diskuy_web/controllers/post_like_controller.ex @@ -40,4 +40,11 @@ defmodule DiskuyWeb.PostLikeController do send_resp(conn, :no_content, "") end end + + def check_like(conn, %{"id" => id}) do + current_user = Guardian.Plug.current_resource(conn) + post_like = Likes.get_post_like_by_refer!(current_user.id, id) + render(conn, "show.json", post_like: post_like) + end + end diff --git a/diskuy_back/lib/diskuy_web/router.ex b/diskuy_back/lib/diskuy_web/router.ex index 2926218..b8a7b2e 100644 --- a/diskuy_back/lib/diskuy_web/router.ex +++ b/diskuy_back/lib/diskuy_web/router.ex @@ -22,6 +22,14 @@ defmodule DiskuyWeb.Router do options "/threads/dislike/:id", ThreadController, :options get "/threads/checklike/:id", ThreadLikeController, :check_like options "/threads/checklike/:id", ThreadLikeController, :options + + post "/post/like/:id", PostController, :add_like + options "/post/like/:id", PostController, :options + post "/post/dislike/:id", PostController, :delete_like + options "/post/dislike/:id", PostController, :options + get "/post/checklike/:id", PostLikeController, :check_like + options "/post/checklike/:id", PostLikeController, :options + end scope "/api", DiskuyWeb do -- GitLab From 0134b6f674eae085cd81a40605441db7dfd8b7f0 Mon Sep 17 00:00:00 2001 From: Muhammad Rafif Elfazri <rafif.elfazri@gmail.com> Date: Fri, 8 Jan 2021 23:37:50 +0700 Subject: [PATCH 2/2] Fix argument use --- diskuy_back/lib/diskuy_web/controllers/thread_controller.ex | 2 +- .../lib/diskuy_web/controllers/thread_like_controller.ex | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/diskuy_back/lib/diskuy_web/controllers/thread_controller.ex b/diskuy_back/lib/diskuy_web/controllers/thread_controller.ex index d5d62a0..cda5d5d 100644 --- a/diskuy_back/lib/diskuy_web/controllers/thread_controller.ex +++ b/diskuy_back/lib/diskuy_web/controllers/thread_controller.ex @@ -57,7 +57,7 @@ defmodule DiskuyWeb.ThreadController do def delete_like(conn, %{"id" => id}) do current_user = Guardian.Plug.current_resource(conn) thread = Forum.get_thread!(id) - thread_like = Likes.get_thread_like_by_refer!(id, current_user.id) + thread_like = Likes.get_thread_like_by_refer!(current_user.id, id) with {:ok, %ThreadLike{}} <- Likes.delete_thread_like(thread_like), {:ok, %Thread{} = thread} <- Forum.update_thread(thread, %{"points" => (thread.points-1)}) do diff --git a/diskuy_back/lib/diskuy_web/controllers/thread_like_controller.ex b/diskuy_back/lib/diskuy_web/controllers/thread_like_controller.ex index 93b9bfa..4b75c24 100644 --- a/diskuy_back/lib/diskuy_web/controllers/thread_like_controller.ex +++ b/diskuy_back/lib/diskuy_web/controllers/thread_like_controller.ex @@ -44,7 +44,7 @@ defmodule DiskuyWeb.ThreadLikeController do def check_like(conn, %{"id" => id}) do current_user = Guardian.Plug.current_resource(conn) - thread_like = Likes.get_thread_like_by_refer!(id, current_user.id) + thread_like = Likes.get_thread_like_by_refer!(current_user.id, id) render(conn, "show.json", thread_like: thread_like) end -- GitLab