drf ๋ฐฑ์๋๊ฐ๋ฐ + ํ๋ก ํธ๊ฐ๋ฐ
: ์ด๋ฒ ํ๋ก์ ํธ๋ ํํ๋ก์ ํธ๋ก ์์ํฉ๋๋ค.
* ๊ฒ์๋ฌผ/๋๊ธ ๊ณต๊ฐ๊ธฐ๋ฅ ๋ฐ ํฌํ๊ธฐ๋ฅ
models.py
class ArticleLike(models.Model):
like_category = models.CharField("๊ณต๊ฐ์ข
๋ฅ", max_length=50, default="")
class CommentLike(models.Model):
like_category = models.CharField("", max_length=50, default="")
class Vote(models.Model):
category = models.CharField("๊ณต๊ฐ์ข
๋ฅ", max_length=50, default="")
class ArticleLikeBridge(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
article = models.ForeignKey(Article, on_delete=models.CASCADE)
like = models.ForeignKey(ArticleLike, on_delete=models.CASCADE, null=True)
category = models.CharField("๊ฒ์๊ธ ๊ณต๊ฐ ์นดํ
๊ณ ๋ฆฌ ์ด๋ฆ", max_length=50, default="")
class ArticleVoteBridge(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
article = models.ForeignKey(Article, on_delete=models.CASCADE)
vote = models.ForeignKey(Vote, on_delete=models.CASCADE, null=True)
category = models.CharField("๊ฒ์๊ธ ํฌํ ์นดํ
๊ณ ๋ฆฌ ์ด๋ฆ", max_length=50, default="")
class CommentLikeBridge(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
comment = models.ForeignKey(Comment, on_delete=models.CASCADE)
like = models.ForeignKey(CommentLike, on_delete=models.CASCADE, null=True)
category = models.CharField("์ฝ๋ฉํธ ๊ณต๊ฐ ์นดํ
๊ณ ๋ฆฌ ์ด๋ฆ", max_length=50, default="")
views.py
# ๊ฒ์๊ธ ๊ณต๊ฐ
class ArticleLikeView(APIView):
authentication_classes = [JWTAuthentication]
def post(self, request, article_id):
like = ArticleLike.objects.create()
article_title = ArticleModel.objects.get(id=article_id)
all = list(ArticleLikeBridge.objects.all().values())
print(all)
all_id = []
for obj in all:
all_id.append(obj['user_id'])
if request.user.id in all_id:
article_like = ArticleLikeBridge.objects.get(user_id=request.user.id)
article_like.delete()
return Response({'message': f'{request.user}๋๊ป์ {article_title.article_title}์ ๊ณต๊ฐ์ ์ทจ์ํ์
จ์ต๋๋ค.'})
else:
article_like = ArticleLikeBridge(
article_id = article_id,
user_id = request.user.id,
like_id = like.id,
category = request.data.get('category')
)
article_like.save()
return Response({'message': f'{request.user}๋๊ป์ {article_title.article_title}์ {article_like.category}ํ์
จ์ต๋๋ค.'})
# ๊ฒ์๊ธ ํฌํ
class ArticleVoteBridgeView(APIView):
authentication_classes = [JWTAuthentication]
def post(self, request, article_id):
vote = VoteModel.objects.create()
article_title = ArticleModel.objects.get(id=article_id)
all = list(ArticleVoteBridge.objects.all().values())
all_id = []
for obj in all:
all_id.append(obj['user_id'])
if request.user.id in all_id:
article_vote = ArticleVoteBridge.objects.get(user_id=request.user.id)
article_vote.delete()
return Response({'message': f'{request.user}๋๊ป์ {article_title.article_title}์ ํฌํ๋ฅผ ์ทจ์ํ์
จ์ต๋๋ค.'})
else:
article_vote = ArticleVoteBridge(
article_id = article_id,
user_id = request.user.id,
vote_id = vote.id,
category = request.data.get('category',"")
)
article_vote.save()
return Response({'message': f'{request.user}๋๊ป์ {article_title.article_title}์ {article_vote.category} ํฌํํ์
จ์ต๋๋ค.'})
# ๋๊ธ ๊ณต๊ฐ
class CommentLikeView(APIView):
authentication_classes = [JWTAuthentication]
def post(self, request, comment_id):
like = CommentLike.objects.create()
comment = CommentModel.objects.get(id=comment_id)
contents = comment.comment_contents
all = list(CommentLikeBridge.objects.all().values())
print(all)
all_id = []
for obj in all:
all_id.append(obj['user_id'])
if request.user.id in all_id:
comment_like = CommentLikeBridge.objects.get(user_id=request.user.id)
comment_like.delete()
return Response({'message': f'{request.user}๋๊ป์ {contents[0:10]}...๋๊ธ์ ๊ณต๊ฐ์ ์ทจ์ํ์
จ์ต๋๋ค.'})
else:
comment_like = CommentLikeBridge(
comment_id = comment_id,
user_id = request.user.id,
like_id = like.id,
category = request.data.get('category')
)
comment_like.save()
return Response({'message': f'{request.user}๋๊ป์ {contents[0:10]}...๋๊ธ์ {comment_like.category}ํ์
จ์ต๋๋ค.'})
# ๊ณต๊ฐ์ ๊ฒ์๊ธ ํ3 ๋ฆฌ์คํ
class MostLikedArticleView(APIView):
def get(self, request):
articles = list(ArticleModel.objects.all().values())
articles_id = []
for article in articles:
articles_id.append(article['id'])
like_counts = []
for id in articles_id:
like_count = ArticleLikeBridge.objects.filter(article_id=id).count()
like_counts.append(like_count)
count_list = { name:value for name, value in zip(articles_id, like_counts)}
like_rank = sorted(count_list.items(), key=lambda x: x[1], reverse=True)[:3]
first = like_rank[0][0]
second = like_rank[1][0]
third = like_rank[2][0]
ranking = [first, second, third]
article_rank = ArticleModel.objects.filter(id__in = ranking)
return Response(ArticleSerializer(article_rank, many=True).data)
# ๊ณต๊ฐ์ ๋๊ธ ํ3 ๋ฆฌ์คํ
class MostLikedCommentView(APIView):
def get(self, request):
comments = list(CommentModel.objects.all().values())
comments_id = []
for comment in comments:
comments_id.append(comment['id'])
like_counts = []
for id in comments_id:
like_count = CommentLikeBridge.objects.filter(comment_id=id).count()
like_counts.append(like_count)
count_list = { name:value for name, value in zip(comments_id, like_counts)}
like_rank = sorted(count_list.items(), key=lambda x: x[1], reverse=True)[:3]
first = like_rank[0][0]
second = like_rank[1][0]
third = like_rank[2][0]
ranking = [first, second, third]
comment_rank = CommentModel.objects.filter(id__in = ranking)
return Response(CommentSerializer(comment_rank, many=True).data)
# ํฌํ์ ํ3 ๋ฆฌ์คํ
class MostVotedArticleView(APIView):
def get(self, request):
articles = list(ArticleModel.objects.all().values())
articles_id = []
for article in articles:
articles_id.append(article['id'])
vote_counts = []
for id in articles_id:
vote_count = ArticleVoteBridge.objects.filter(article_id=id).count()
vote_counts.append(vote_count)
count_list = { name:value for name, value in zip(articles_id, vote_counts)}
vote_rank = sorted(count_list.items(), key=lambda x: x[1], reverse=True)[:3]
first = vote_rank[0][0]
second = vote_rank[1][0]
third = vote_rank[2][0]
ranking = [first, second, third]
article_rank = ArticleModel.objects.filter(id__in = ranking)
return Response(ArticleSerializer(article_rank, many=True).data)
urls.py
urlpatterns = [
path('likeCount/', views.MostLikedArticleView.as_view()),
path('voteCount/', views.MostVotedArticleView.as_view()),
path('comment/likeCount/', views.MostLikedCommentView.as_view()),
path('<comment_id>/comment/like/', views.CommentLikeView.as_view()),
path('<article_id>/article/like/', views.ArticleLikeView.as_view()),
path('<article_id>/article/vote/', views.ArticleVoteBridgeView.as_view()),
]
๐ ์์ธํ ์ฝ๋๋ Moonmoo ์ ๊ฒ์ํ์ต๋๋ค