Skip to content

Add Comment CRUD API#42

Open
JinHoooooou wants to merge 5 commits intocode-reviewfrom
jinho-step2-comment-CRUD
Open

Add Comment CRUD API#42
JinHoooooou wants to merge 5 commits intocode-reviewfrom
jinho-step2-comment-CRUD

Conversation

@JinHoooooou
Copy link
Contributor

@JinHoooooou JinHoooooou commented Jul 13, 2021

  1. 테스트 작성

    • request body에 유효한 author, post_id, text가 있을경우 comment 작성 성공
    • request body에 유효하지 않은 post_id,있을경우 comment 작성 실패
  2. urls.py에 path 추가

  3. create_comment_to_post 메서드 작성


질문

  1. models.py를 보면 Comment 클래스에 authorForeignKey가 아닌 TextField라서 테스트 작성시 author에 string으로 입력했는데 괜찮은지 모르겠습니다.

  2. commit 작성시

    1. api 성공에 대한 테스트코드 작성 👉 commit
    2. 테스트가 통과하도록 메서드 작성 👉 commit
    3. api 실패에 대한 테스트코드 작성 👉 commit
    4. 모든 테스트가 통과하도록 메서드 수정 👉 commit

이런식으로 commit을 작성했는데, 이 방법이 맞는건지 아니면 api에 대한 테스트코드, 모든 테스트를 통과하는 메서드를 작성하여 하나의 commit으로 작성하는게 맞는건지 궁금합니다.

  1. 제가 생각했을 때 Post와 Comment 관계가
    image
    이런식으로 comment index가 post에 종속되어야 한다고 생각하는데, 코드를 작성하다보니
    image
    이런식으로 comment index가 post와 관계없이 작성한 순서에 따라 할당됩니다.
    이것 때문에 계속 잘못된거 같은 생각이 드는데 어떻게 고쳐야할지 몰라서 질문드립니다.

@TaeHyoungKwon
Copy link
Contributor

1번 - 주어진 조건이 그런거니까 상관없음
2번 - 실패 하는 테스트코드 작성, 통과하는 테스트코드 작성 --> 여기까지 commit 1개, 리팩토링 -> 여기까지 commit 1개 -> 반복
----------> 이렇게 해야하는 이유는 step3를 해보면 알거임(실패하는 테스트만 만들고 commit을 할 수 없음 )
3번 - comment가 post에 종속되는거지 comment id가 post에 종속되는 거는 아님, comment id는 comment를 식별하는 용도로 필요한거고, 아래 그림처럼되는게 맞음

Comment on lines +94 to +97
def create_comment_to_post(request):
body = request.POST
try:
post_of_comment = Post.objects.get(id=body["post_id"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

post_id를 request_body로 넘겨줘도 되는데, path param으로 넘겨줘도 될 것 같음

POST" /api/comments/1" 요런 식으로

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"),
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments