Fakultas Ilmu Komputer UI
Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Functional Programming
Diskuy-Backend
Commits
e2e635a5
Commit
e2e635a5
authored
Jan 08, 2021
by
Muhammad Rafif Elfazri
Browse files
Add like system for post
parent
2125ff4e
Changes
4
Hide whitespace changes
Inline
Side-by-side
diskuy_back/lib/diskuy/likes.ex
View file @
e2e635a5
...
...
@@ -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.
...
...
diskuy_back/lib/diskuy_web/controllers/post_controller.ex
View file @
e2e635a5
...
...
@@ -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
)
...
...
diskuy_back/lib/diskuy_web/controllers/post_like_controller.ex
View file @
e2e635a5
...
...
@@ -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
diskuy_back/lib/diskuy_web/router.ex
View file @
e2e635a5
...
...
@@ -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
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment