From 18938e4005417aa43352d2b571556eb44bc1bfa6 Mon Sep 17 00:00:00 2001 From: Shim Kyumin Date: Wed, 7 Aug 2024 11:41:50 +0900 Subject: [PATCH 1/4] =?UTF-8?q?fix:=20Recommend=20=EB=AA=A8=EB=8D=B8?= =?UTF-8?q?=EC=97=90=EC=84=9C=20recommend=20=ED=95=84=EB=93=9C=EB=A5=BC=20?= =?UTF-8?q?INT=20=ED=95=84=EB=93=9C=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0006_alter_recommend_unique_together.py | 18 ++++++++++++++++++ .../0007_alter_recommend_recommend.py | 17 +++++++++++++++++ reviews/models.py | 5 ++++- 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 reviews/migrations/0006_alter_recommend_unique_together.py create mode 100644 reviews/migrations/0007_alter_recommend_recommend.py diff --git a/reviews/migrations/0006_alter_recommend_unique_together.py b/reviews/migrations/0006_alter_recommend_unique_together.py new file mode 100644 index 0000000..cd1ae2e --- /dev/null +++ b/reviews/migrations/0006_alter_recommend_unique_together.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.14 on 2024-08-07 02:24 + +from django.conf import settings +from django.db import migrations + + +class Migration(migrations.Migration): + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ("reviews", "0005_remove_review_parent_reply"), + ] + + operations = [ + migrations.AlterUniqueTogether( + name="recommend", + unique_together={("user", "review")}, + ), + ] diff --git a/reviews/migrations/0007_alter_recommend_recommend.py b/reviews/migrations/0007_alter_recommend_recommend.py new file mode 100644 index 0000000..e4f248a --- /dev/null +++ b/reviews/migrations/0007_alter_recommend_recommend.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2.14 on 2024-08-07 02:37 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("reviews", "0006_alter_recommend_unique_together"), + ] + + operations = [ + migrations.AlterField( + model_name="recommend", + name="recommend", + field=models.IntegerField(default=2), + ), + ] diff --git a/reviews/models.py b/reviews/models.py index c5d3cae..5878f4f 100644 --- a/reviews/models.py +++ b/reviews/models.py @@ -26,4 +26,7 @@ class Reply(models.Model): class Recommend(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) review = models.ForeignKey(Review, on_delete=models.CASCADE) - recommend = models.BooleanField() + recommend = models.IntegerField(default=2) # 0: 싫어요, 1: 좋아요, 2: 기본 + + class Meta: + unique_together = ("user", "review") From 49138b84f026181a1ad38c616070709f69f26c4f Mon Sep 17 00:00:00 2001 From: Shim Kyumin Date: Wed, 7 Aug 2024 11:43:54 +0900 Subject: [PATCH 2/4] =?UTF-8?q?feat:=20=EB=A6=AC=EB=B7=B0=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20=EC=8B=9C=EB=A6=AC=EC=96=BC=EB=9D=BC=EC=9D=B4?= =?UTF-8?q?=EC=A0=80=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- reviews/serializers.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/reviews/serializers.py b/reviews/serializers.py index 204ee65..6f4a78e 100644 --- a/reviews/serializers.py +++ b/reviews/serializers.py @@ -18,3 +18,20 @@ class RecommendSerializer(serializers.ModelSerializer): class Meta: model = Recommend fields = "__all__" + + +class ReviewListSerializer(serializers.ModelSerializer): + user_name = serializers.CharField(source="user.name", read_only=True) + replies_count = serializers.IntegerField(source="replies.count", read_only=True) + + class Meta: + model = Review + fields = [ + "id", + "user_name", + "content", + "recommend_count", + "decommend_count", + "date", + "replies_count", + ] From 6a1e752da3935d1b80e833ff0f9986f0183cb9fb Mon Sep 17 00:00:00 2001 From: Shim Kyumin Date: Wed, 7 Aug 2024 11:45:57 +0900 Subject: [PATCH 3/4] =?UTF-8?q?feat:=20=EB=A6=AC=EB=B7=B0=20=ED=8F=89?= =?UTF-8?q?=EA=B0=80=20url=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- restaurants/urls.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/restaurants/urls.py b/restaurants/urls.py index 86362d5..c3d0f06 100644 --- a/restaurants/urls.py +++ b/restaurants/urls.py @@ -1,6 +1,6 @@ from django.urls import path, include, re_path from . import views -from reviews.views import review_write +from reviews.views import review, evaluate_review urlpatterns = [ @@ -19,5 +19,10 @@ name="restaurant-detail", ), path("restaurants/", views.user_restaurant_list, name="user-restaurant-list"), - path("restaurants//reviews/", review_write, name="review-write"), + path("restaurants//reviews/", review, name="review-write"), + path( + "restaurants//reviews//", + evaluate_review, + name="evaluate-review", + ), ] From a28117886728be9e55a0178bf9fa605678ec0a92 Mon Sep 17 00:00:00 2001 From: Shim Kyumin Date: Wed, 7 Aug 2024 11:46:50 +0900 Subject: [PATCH 4/4] =?UTF-8?q?feat:=20=EB=A6=AC=EB=B7=B0=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20=EB=B0=8F=20=ED=8F=89=EA=B0=80=20=EA=B8=B0=EB=8A=A5?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- reviews/views.py | 100 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 81 insertions(+), 19 deletions(-) diff --git a/reviews/views.py b/reviews/views.py index 64baac4..838c3b8 100644 --- a/reviews/views.py +++ b/reviews/views.py @@ -3,35 +3,47 @@ from rest_framework import status from accounts.models import User from restaurants.models import Restaurant -from .models import Review -from .serializers import ReviewSerializer, ReplySerializer +from .models import Review, Recommend +from .serializers import ReviewSerializer, ReplySerializer, ReviewListSerializer # from rest_framework.authentication import TokenAuthentication # from rest_framework.permissions import IsAuthenticated -@api_view(["POST"]) +@api_view(["GET", "POST"]) # @authentication_classes([TokenAuthentication]) # @permission_classes([IsAuthenticated]) -def review_write(request, pk): - user = User.objects.get(id=21) +def review(request, pk): + """ + 한줄평 더보기 기능 (GET) + 한줄평 작성 기능 (POST) + """ + if request.method == "GET": + # 리뷰 조회 기능 + reviews = Review.objects.filter(restaurant_id=pk).order_by("-date") + serializer = ReviewListSerializer(reviews, many=True) + return Response({"results": serializer.data}, status=status.HTTP_200_OK) - try: - restaurant = Restaurant.objects.get(pk=pk) - except Restaurant.DoesNotExist: - return Response( - {"error": "Restaurant not found"}, status=status.HTTP_404_NOT_FOUND - ) + elif request.method == "POST": + # 리뷰 작성 기능 + try: + restaurant = Restaurant.objects.get(pk=pk) + except Restaurant.DoesNotExist: + return Response( + {"error": "Restaurant not found"}, status=status.HTTP_404_NOT_FOUND + ) - data = request.data - data["user"] = user.id - data["restaurant"] = restaurant.id + user = User.objects.get(id=21) # 임시 유저 지정, 추후 삭제 - serializer = ReviewSerializer(data=data) - if serializer.is_valid(): - serializer.save(user=user, restaurant=restaurant) - return Response(serializer.data, status=status.HTTP_201_CREATED) - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + data = request.data + data["user"] = user.id + data["restaurant"] = restaurant.id + + serializer = ReviewSerializer(data=data) + if serializer.is_valid(): + serializer.save(user=user, restaurant=restaurant) + return Response(serializer.data, status=status.HTTP_201_CREATED) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) @api_view(["POST"]) @@ -54,3 +66,53 @@ def reply_write(request, pk): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + + +@api_view(["POST"]) +def evaluate_review(request, restaurant_id, review_id): + """ + 한줄평 평가 기능 (좋아요/싫어요) + """ + try: + review = Review.objects.get(id=review_id, restaurant_id=restaurant_id) + user = User.objects.get(id=21) # 임시 유저 지정, 추후 삭제 + + # 사용자가 이미 평가했는지 확인 + recommend_entry, created = Recommend.objects.get_or_create( + user=user, review=review + ) + + evaluation = request.data.get("evaluation") + if evaluation == "1": + # 좋아요 + if created or recommend_entry.recommend != 1: + review.recommend_count += 1 + if recommend_entry.recommend == 0: # 이전에 싫어요였던 경우 + review.decommend_count -= 1 + recommend_entry.recommend = 1 + + elif evaluation == "0": + # 싫어요 + if created or recommend_entry.recommend != 0: + review.decommend_count += 1 + if recommend_entry.recommend == 1: # 이전에 좋아요였던 경우 + review.recommend_count -= 1 + recommend_entry.recommend = 0 + + else: + return Response( + {"message": "Invalid evaluation value"}, + status=status.HTTP_400_BAD_REQUEST, + ) + + review.save() + recommend_entry.save() + + return Response( + {"message": "Review evaluated successfully"}, status=status.HTTP_200_OK + ) + + except Review.DoesNotExist: + return Response( + {"message": "Review not found"}, status=status.HTTP_404_NOT_FOUND + )