diff --git a/pet_promotion/comment/admin.py b/pet_promotion/comment/admin.py index adcd00a..22f77f4 100644 --- a/pet_promotion/comment/admin.py +++ b/pet_promotion/comment/admin.py @@ -1,4 +1,2 @@ from django.contrib import admin -from .models import Comment -# Register your models here. -admin.site.register(Comment) \ No newline at end of file +# Register your models here. \ No newline at end of file diff --git a/pet_promotion/comment/models.py b/pet_promotion/comment/models.py index 2a6a9c2..581f2a7 100644 --- a/pet_promotion/comment/models.py +++ b/pet_promotion/comment/models.py @@ -1,10 +1,4 @@ from django.db import models +from board.models import Post -# Create your models here. - -class Comment(models.Model): - comment = models.TextField() - create_date = models.DateField(auto_now_add=True) - update_date = models.DateField(auto_now_add=True) - post = models.ForeignKey(Post, blank=False, null=False, on_delete=models.CASCADE) - user = models.ForeignKey("user.User", related_name="user", on_delete=models.CASCADE, db_column="user_id") \ No newline at end of file +# Create your models here. \ No newline at end of file diff --git a/pet_promotion/comment/serializers.py b/pet_promotion/comment/serializers.py index 3c87046..26d92b9 100644 --- a/pet_promotion/comment/serializers.py +++ b/pet_promotion/comment/serializers.py @@ -1,6 +1,6 @@ from rest_framework import serializers - -from .models import Comment +from board.models import Post +from board.models import Comment class CommentSerializer(serializers.ModelSerializer): diff --git a/pet_promotion/comment/views.py b/pet_promotion/comment/views.py index 5800461..d226ed4 100644 --- a/pet_promotion/comment/views.py +++ b/pet_promotion/comment/views.py @@ -1,14 +1,16 @@ from django.shortcuts import render +from django.shortcuts import get_object_or_404 +from rest_framework.response import Response +from rest_framework.decorators import api_view from rest_framework import viewsets - from .serializers import CommentSerializer -from .models import Comment +from board.models import Comment @api_view(['POST']) def comment_create(request, post_id): serializer = CommentSerializer(data=request.data) if serializer.is_valid(raise_exception=True): - serializer.save(post=post_id)#post필드 값으로 post_id입력(어떤 게시물의 댓글인지 알기위함) + serializer.save(post_id=post_id)#post필드 값으로 post_id입력(어떤 게시물의 댓글인지 알기위함) return Response(serializer.data) @api_view(['PUT','DELETE']) #PUT : 데이터 수정할 때, DELETE : 데이터 삭제 할 때 @@ -21,4 +23,10 @@ def comment_update_and_delete(request, post_id, comment_id): return Response({'message':'update'}) else: comment.delete() - return Response({'message':'delete'}) \ No newline at end of file + return Response({'message':'delete'}) + +@api_view(['GET']) +def comment_list(request, post_id): + comment = Comment.objects.filter(post=post_id) + serializer = CommentSerializer(comment, many=True) + return Response(serializer.data) \ No newline at end of file diff --git a/pet_promotion/projects/settings.py b/pet_promotion/projects/settings.py index 494da49..720a998 100644 --- a/pet_promotion/projects/settings.py +++ b/pet_promotion/projects/settings.py @@ -43,6 +43,7 @@ #App 등록 'user.apps.UserConfig', 'board.apps.BoardConfig', + 'comment.apps.CommentConfig', #DRF Settings 'rest_framework', diff --git a/pet_promotion/projects/urls.py b/pet_promotion/projects/urls.py index 47b5b10..d15a4ad 100644 --- a/pet_promotion/projects/urls.py +++ b/pet_promotion/projects/urls.py @@ -22,6 +22,9 @@ from board.views import ( PostUpdateAPIView, PostDeleteAPIView, PostViewSet ,PostCreateViewSet, ) +from comment.views import ( + comment_create, comment_update_and_delete, comment_list +) router = routers.DefaultRouter() router.register('post', PostViewSet, basename='post') @@ -31,7 +34,13 @@ path('api/', include(router.urls)), path('admin/', admin.site.urls), path('user/', include('user.urls')), + url('api/post/(?P[\w-]+)/edit/$', PostUpdateAPIView.as_view(), name='post_update'), url('api/post/(?P[\w-]+)/delete/$', PostDeleteAPIView.as_view(), name='post_delete'), + + #comment + path('post//', comment_list), + path('post//comments/', comment_create), + path('post//comments//',comment_update_and_delete), ]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)