Skip to content

Commit

Permalink
feat: 답글 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
dkfla committed Aug 6, 2024
1 parent 30bdeda commit 025e0d1
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 27 deletions.
1 change: 1 addition & 0 deletions mustgou/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
path("", include("restaurants.urls")),
path("auth/", include("accounts.urls")),
path("", include("friends.urls")),
path("", include("reviews.urls")),
]
50 changes: 50 additions & 0 deletions reviews/migrations/0005_remove_review_parent_reply.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Generated by Django 4.2.14 on 2024-08-06 15:08

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("reviews", "0004_remove_review_parent_id_review_parent_and_more"),
]

operations = [
migrations.RemoveField(
model_name="review",
name="parent",
),
migrations.CreateModel(
name="Reply",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("content", models.CharField(max_length=255)),
("date", models.DateTimeField(auto_now_add=True)),
(
"review",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="replies",
to="reviews.review",
),
),
(
"user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
],
),
]
10 changes: 7 additions & 3 deletions reviews/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ class Review(models.Model):
content = models.CharField(max_length=255)
recommend_count = models.IntegerField(default=0)
decommend_count = models.IntegerField(default=0)
parent = models.ForeignKey(
"self", null=True, blank=True, on_delete=models.CASCADE, related_name="replies"
)
date = models.DateTimeField(auto_now_add=True)


class Reply(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
review = models.ForeignKey(Review, on_delete=models.CASCADE, related_name="replies")
content = models.CharField(max_length=255)
date = models.DateTimeField(auto_now_add=True)


Expand Down
8 changes: 7 additions & 1 deletion reviews/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from rest_framework import serializers
from .models import Review, Recommend
from .models import Review, Reply, Recommend


class ReviewSerializer(serializers.ModelSerializer):
Expand All @@ -8,6 +8,12 @@ class Meta:
fields = "__all__"


class ReplySerializer(serializers.ModelSerializer):
class Meta:
model = Reply
fields = "__all__"


class RecommendSerializer(serializers.ModelSerializer):
class Meta:
model = Recommend
Expand Down
7 changes: 7 additions & 0 deletions reviews/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.urls import path
from . import views


urlpatterns = [
path("reviews/<int:pk>/", views.reply_write, name="reply-write"),
]
49 changes: 26 additions & 23 deletions reviews/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from accounts.models import User
from restaurants.models import Restaurant
from .models import Review
from .serializers import ReviewSerializer
from .serializers import ReviewSerializer, ReplySerializer

# from rest_framework.authentication import TokenAuthentication
# from rest_framework.permissions import IsAuthenticated
Expand All @@ -23,31 +23,34 @@ def review_write(request, pk):
{"error": "Restaurant not found"}, status=status.HTTP_404_NOT_FOUND
)

request.data["user"] = user.id
request.data["restaurant"] = restaurant.id

try:
data = request.data
except ValueError:
return Response({"detail": "Invalid JSON"}, status=status.HTTP_400_BAD_REQUEST)

parent_id = data.get("parent")
if parent_id:
try:
parent_review = Review.objects.get(id=parent_id)
if parent_review.parent is not None:
return Response(
{"error": "Replies to replies are not allowed"},
status=status.HTTP_400_BAD_REQUEST,
)
data["parent"] = parent_review.id
except Review.DoesNotExist:
return Response(
{"error": "Parent review not found"}, status=status.HTTP_404_NOT_FOUND
)
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"])
# @authentication_classes([TokenAuthentication])
# @permission_classes([IsAuthenticated])
def reply_write(request, pk):
user = User.objects.get(id=21) # 임시 유저 지정, 추후 삭제

try:
review = Review.objects.get(id=pk)
except Review.DoesNotExist:
return Response({"error": "Review not found"}, status=status.HTTP_404_NOT_FOUND)

data = request.data
data["user"] = user.id
data["review"] = review.id

serializer = ReplySerializer(data=data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

0 comments on commit 025e0d1

Please sign in to comment.