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] 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