-
Notifications
You must be signed in to change notification settings - Fork 1
Add Comment CRUD API #42
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
Open
JinHoooooou
wants to merge
5
commits into
code-review
Choose a base branch
from
jinho-step2-comment-CRUD
base: code-review
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e0de67e
add test to success create_comment_to_post api
JinHoooooou ca17e3b
add create_comment_to_post api url
JinHoooooou 8424277
add create_comment_to_post method
JinHoooooou 752e522
add test to fail create_comment_to_post api
JinHoooooou 66251a0
modify create_comment_to_post method
JinHoooooou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,10 @@ | ||
| from django.urls import path | ||
| from . import views | ||
|
|
||
|
|
||
| urlpatterns = [ | ||
| path("posts/", views.retrieve_post_list, name="retrieve_post_list"), | ||
| path("posts/<int:id>", views.retrieve_post_detail, name="retrieve_post_detail"), | ||
| path("posts/create", views.create_post, name="create_post"), | ||
| path("posts/<int:id>/put/update", views.update_post_with_put, name="update_post_with_put"), | ||
| path("comments/create", views.create_comment_to_post, name="create_comment_to_post"), | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,13 @@ | ||
| import json | ||
| from http.client import NOT_FOUND, OK | ||
| from http import HTTPStatus | ||
|
|
||
| from django.contrib.auth.models import User | ||
| from django.http import JsonResponse, QueryDict | ||
| from django.utils import timezone | ||
| from django.views.decorators.http import require_http_methods | ||
|
|
||
| from blog.models import Post | ||
| from blog.form import CommentForm | ||
| from blog.models import Post, Comment | ||
|
|
||
|
|
||
| def retrieve_post_list(request): | ||
|
|
@@ -87,3 +88,29 @@ def update_post_with_put(request, id): | |
|
|
||
| FYI, request.body 활용 | ||
| """ | ||
|
|
||
|
|
||
| @require_http_methods(["POST"]) | ||
| def create_comment_to_post(request): | ||
| body = request.POST | ||
| try: | ||
| post_of_comment = Post.objects.get(id=body["post_id"]) | ||
|
Comment on lines
+94
to
+97
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. post_id를 request_body로 넘겨줘도 되는데, path param으로 넘겨줘도 될 것 같음
|
||
| except Post.DoesNotExist: | ||
| return JsonResponse({"message": "Post를 찾을 수 없습니다."}, status=HTTPStatus.NOT_FOUND) | ||
| else: | ||
| comment_form = CommentForm(body) | ||
| if comment_form.is_valid(): | ||
| comment = comment_form.save(commit=False) | ||
| comment.author = body["author"] | ||
| comment.post = post_of_comment | ||
| comment.text = body["text"] | ||
| comment.save() | ||
| return JsonResponse({ | ||
| "comment": { | ||
| "id": comment.id, | ||
| "author": comment.author, | ||
| "post_id": comment.post.id, | ||
| "text": comment.text, | ||
| } | ||
| }, status=HTTPStatus.CREATED) | ||
| return JsonResponse({"message": "form is invalid"}, status=HTTPStatus.BAD_REQUEST) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
여기선 표현의 한계가 있긴 한데, 나중에는 REST 하게 작성하는 방법도 익혀두면 좋을 것 같음
ex)
POST /api/comments/ , POST /api/comments/1