Skip to content
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

[feat] 게시글 댓글 등록, 삭제 #64

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/main/java/com/alevel/backend/controller/CommentController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.alevel.backend.controller;

import com.alevel.backend.controller.dto.CommentRequestDto;
import com.alevel.backend.controller.dto.CommentSaveDto;
import com.alevel.backend.domain.response.ResultResponse;
import com.alevel.backend.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
public class CommentController {

@Autowired
private CommentService commentService;

@Autowired
public CommentController(CommentService commentService){this.commentService=commentService;}

//댓글 등록
@PostMapping(value="/posts/{id}/comment")
public ResultResponse saveComment(@PathVariable("id") Long id, @RequestBody CommentRequestDto requestDto){
CommentSaveDto dto = new CommentSaveDto(id, requestDto.getUserid(),requestDto.getContent());
commentService.saveComment(dto);
return ResultResponse.success();
}

//댓글 삭제
@DeleteMapping(value="/posts/{id}/comment")
Copy link
Contributor

@jinnjuu jinnjuu Nov 1, 2022

Choose a reason for hiding this comment

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

url 의미가 조금 맞지 않아 수정이 필요해 보입니다. 컨트롤러에 맵핑된 id (파라미터로 받은 id) 는 게시글 id 인데, 실제 서비스에서 삭제하는 id 는 댓글 id 입니다.
컨트롤러 url 을 /posts/{post-id}/comments/{comment-id} 등으로 수정하면 좋을 것 같습니다!

public ResponseEntity deleteComment(@PathVariable("id") Long id){
commentService.delete(id);
return ResponseEntity.ok(id);
Copy link
Contributor

Choose a reason for hiding this comment

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

삭제되었을때 상태코드 같이 리턴해주시면 좋을 것 같습니다!

}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.alevel.backend.controller.dto;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
public class CommentRequestDto {
private Long userid;
private String content;

public CommentRequestDto(Long userid, String content){
this.userid=userid;
this.content=content;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.alevel.backend.controller.dto;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class CommentSaveDto {
private Long userid;
private Long postid;
private String content;

public CommentSaveDto(Long userid, Long postid, String content){
this.userid=userid;
this.postid=postid;
this.content=content;
}

}
12 changes: 6 additions & 6 deletions src/main/java/com/alevel/backend/domain/comment/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public class Comment extends BaseTimeEntity {
@Column(columnDefinition = "TEXT")
private String content;

// @Builder
// public Comment(User user, Post post,String content){
// this.user=user;
// this.post=post;
// this.content=content;
// }
@Builder
public Comment(User user, Post post,String content){
this.user=user;
this.post=post;
this.content=content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public interface CommentRepository extends JpaRepository<Comment, Long> {

@Query(value="SELECT COUNT(*) FROM comment c WHERE c.user_id=user_id",nativeQuery = true)
Integer myCommentCount(Long id);


}

50 changes: 50 additions & 0 deletions src/main/java/com/alevel/backend/service/CommentService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.alevel.backend.service;

import com.alevel.backend.controller.dto.CommentSaveDto;
import com.alevel.backend.domain.alcohol.AlcoholRepository;
import com.alevel.backend.domain.comment.Comment;
import com.alevel.backend.domain.comment.CommentRepository;
import com.alevel.backend.domain.post.Post;
import com.alevel.backend.domain.post.PostRepository;
import com.alevel.backend.domain.review.ReviewRepository;
import com.alevel.backend.domain.user.User;
import com.alevel.backend.domain.user.UserRepository;
import com.alevel.backend.exception.ExceededNumberException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

@Service
public class CommentService {
private final CommentRepository commentRepository;
private final UserRepository userRepository;

private final PostRepository postRepository;


@Autowired
public CommentService(CommentRepository commentRepository, UserRepository userRepository,PostRepository postRepository) {
this.postRepository=postRepository;
this.userRepository = userRepository;
this.commentRepository=commentRepository;
}
public void saveComment(CommentSaveDto dto) {
User user = userRepository.getReferenceById(dto.getUserid());
Post post= postRepository.getReferenceById(dto.getPostid());

if (dto.getContent().length()>100) {
throw new ExceededNumberException();
}
else{
commentRepository.save(new Comment(user, post, dto.getContent()));
}
}

@Transactional
public void delete(Long id) {
Comment comment = commentRepository.findById(id).orElseThrow(() ->
new IllegalArgumentException("존재하지 않는 댓글입니다. id=" + id));
Copy link
Contributor

Choose a reason for hiding this comment

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

에러가 로그상에만 떨어지고 있는데 리턴값으로도 에러메세지나 상태코드를 넘겨주면 프론트에서 처리하기 좋을 것 같습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image

controller와 service 수정하였습니다.

commentRepository.delete(comment);
}
}