Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/reviewdetail #70

Merged
merged 4 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions restaurants/urls.py
Original file line number Diff line number Diff line change
@@ -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 = [
Expand All @@ -19,5 +19,10 @@
name="restaurant-detail",
),
path("restaurants/", views.user_restaurant_list, name="user-restaurant-list"),
path("restaurants/<int:pk>/reviews/", review_write, name="review-write"),
path("restaurants/<int:pk>/reviews/", review, name="review-write"),
path(
"restaurants/<int:restaurant_id>/reviews/<int:review_id>/",
evaluate_review,
name="evaluate-review",
),
]
18 changes: 18 additions & 0 deletions reviews/migrations/0006_alter_recommend_unique_together.py
Original file line number Diff line number Diff line change
@@ -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")},
),
]
17 changes: 17 additions & 0 deletions reviews/migrations/0007_alter_recommend_recommend.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
5 changes: 4 additions & 1 deletion reviews/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
17 changes: 17 additions & 0 deletions reviews/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
100 changes: 81 additions & 19 deletions reviews/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand All @@ -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
)
Loading