-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: develop
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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") | ||
public ResponseEntity deleteComment(@PathVariable("id") Long id){ | ||
commentService.delete(id); | ||
return ResponseEntity.ok(id); | ||
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. 삭제되었을때 상태코드 같이 리턴해주시면 좋을 것 같습니다! |
||
} | ||
|
||
|
||
} |
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; | ||
} | ||
|
||
} |
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)); | ||
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. 에러가 로그상에만 떨어지고 있는데 리턴값으로도 에러메세지나 상태코드를 넘겨주면 프론트에서 처리하기 좋을 것 같습니다. 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. |
||
commentRepository.delete(comment); | ||
} | ||
} |
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.
url 의미가 조금 맞지 않아 수정이 필요해 보입니다. 컨트롤러에 맵핑된 id (파라미터로 받은 id) 는 게시글 id 인데, 실제 서비스에서 삭제하는 id 는 댓글 id 입니다.
컨트롤러 url 을
/posts/{post-id}/comments/{comment-id}
등으로 수정하면 좋을 것 같습니다!