diff --git a/diskuy_back/lib/diskuy/likes.ex b/diskuy_back/lib/diskuy/likes.ex index 4ca589744ac20b11acb52e3b57fe6d491a9cd789..b659d0327351414ada0c2373058c3f4dfd6cb1a8 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 b48f5ef907dee75d6698eff6a480008d7899079e..7de76bd9bbbdff1299fa97cd10a00d4d75657d1f 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 5237ccd57d824b5f9cc8d36c6c7e5777452235b0..a1d85a4ee96fb39c66f2a8efb95541e8e2099030 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/controllers/thread_controller.ex b/diskuy_back/lib/diskuy_web/controllers/thread_controller.ex index d5d62a04138f45437aa074def7fa0f0fdc5d3d80..cda5d5d80e0ecd2f9c55396140c05618053ee6b3 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 93b9bfa88ff34c37e49e8d4608fd80ee71cef5d2..4b75c2414a90e307d2cc5584150bd70b5dfa1f2b 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 diff --git a/diskuy_back/lib/diskuy_web/router.ex b/diskuy_back/lib/diskuy_web/router.ex index 29262180f6479c1e602ac1307ff9ea7574f33c38..b8a7b2ea23ef15cdc94975dedffb563e744fe5c1 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