Fakultas Ilmu Komputer UI

Commit e2e635a5 authored by Muhammad Rafif Elfazri's avatar Muhammad Rafif Elfazri
Browse files

Add like system for post

parent 2125ff4e
......@@ -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.
......
......@@ -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)
......
......@@ -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
......@@ -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
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment