Skip to content

Commit

Permalink
feat: comment serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
moonlitgrace committed Dec 13, 2024
1 parent 1e29110 commit 7d73fdd
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
30 changes: 30 additions & 0 deletions backend/apps/comment/api/v1/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from django.shortcuts import get_object_or_404
from rest_framework import serializers

from ...models import Comment


class CommentSerializer(serializers.ModelSerializer):
path = serializers.CharField(required=False)

class Meta:
model = Comment
fields = '__all__'

def create(self, validated_data):
data = {
'quibbler': validated_data.get('quibbler')
or self.context['request'].user_profile,
'content': validated_data['content'],
}

if path := validated_data.get('path'):
parent_instance = get_object_or_404(Comment, path__match=path)
comment_instance: Comment = Comment.objects.create_child(
parent=parent_instance, **data
)
else:
comment_instance: Comment = Comment.objects.create_child(**data)

comment_instance.save()
return comment_instance
8 changes: 8 additions & 0 deletions backend/apps/comment/api/v1/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from rest_framework import routers

from .viewsets import CommentViewSet

router = routers.DefaultRouter()
router.register(r'', CommentViewSet)

urlpatterns = router.urls
9 changes: 9 additions & 0 deletions backend/apps/comment/api/v1/viewsets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from rest_framework import viewsets

from ...models import Comment
from .serializers import CommentSerializer


class CommentViewSet(viewsets.ModelViewSet):
queryset = Comment.objects.all()
serializer_class = CommentSerializer
2 changes: 2 additions & 0 deletions backend/config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
path('u/', include('apps.user.api.v1.urls')),
# quiblet and quib endpoints
path('q/', include('apps.quiblet.api.v1.urls')),
# comments
path('comments/', include('apps.comment.api.v1.urls')),
]
),
),
Expand Down

0 comments on commit 7d73fdd

Please sign in to comment.