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

refactor : 코드 수정 #51

Merged
merged 1 commit into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
41 changes: 21 additions & 20 deletions src/main/java/ne/ordinary/dd/controller/CommentController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.RequiredArgsConstructor;
import ne.ordinary.dd.model.CommentLikeDTO;
import ne.ordinary.dd.model.CommentRequestDTO;
import ne.ordinary.dd.model.ResponseDTO;
import ne.ordinary.dd.service.CommentLikeService;
import ne.ordinary.dd.service.CommentService;
import org.springframework.http.ResponseEntity;
Expand All @@ -17,43 +18,43 @@ public class CommentController {
private final CommentLikeService commentLikeService;

@PostMapping("/{feedId}")
public ResponseEntity<String> commentCreate(@PathVariable("feedId") Long feedId, @RequestBody CommentRequestDTO commentRequestDTO){
public ResponseDTO<String> commentCreate(@PathVariable("feedId") Long feedId, @RequestBody CommentRequestDTO commentRequestDTO){
commentService.saveComment(feedId, commentRequestDTO);
return ResponseEntity.ok("댓글이 작성되었습니다");
return new ResponseDTO<String>("댓글이 작성되었습니다");
}
@PostMapping("/{feedId}/{parentId}")
public ResponseEntity<String> reCommentSave(@PathVariable("feedId") Long feedId, @PathVariable("parentId") Long commentId, @RequestBody CommentRequestDTO commentRequestDTO){
public ResponseDTO<String> reCommentSave(@PathVariable("feedId") Long feedId, @PathVariable("parentId") Long commentId, @RequestBody CommentRequestDTO commentRequestDTO){
commentService.saveRecomment(feedId, commentId, commentRequestDTO);
return ResponseEntity.ok("대댓글이 작성되었습니다");
return new ResponseDTO<String>("대댓글이 작성되었습니다");
}
@PatchMapping("/{commentId}")
public ResponseEntity<String> updateComment(@PathVariable("commentId") Long commentId, @RequestBody CommentRequestDTO commentRequestDTO){
public ResponseDTO<String> updateComment(@PathVariable("commentId") Long commentId, @RequestBody CommentRequestDTO commentRequestDTO){
commentService.updateComment(commentId, commentRequestDTO);
return ResponseEntity.ok("댓글이 수정되었습니다");
return new ResponseDTO<String>("댓글이 수정되었습니다");
}
@DeleteMapping("/{commentId}")
public ResponseEntity<String> delete(@PathVariable("commentId") Long commentId){
public ResponseDTO<String> delete(@PathVariable("commentId") Long commentId){
commentService.deleteComment(commentId);
return ResponseEntity.ok("댓글이 삭제되었습니다");
return new ResponseDTO<String>("댓글이 삭제되었습니다");
}
@PostMapping("/{commentId}/Tlike")
public ResponseEntity<String> addTlike(@PathVariable("commentId") Long commentId, @RequestBody CommentLikeDTO commentLikeDTO){
public ResponseDTO<String> addTlike(@PathVariable("commentId") Long commentId, @RequestBody CommentLikeDTO commentLikeDTO){
commentLikeService.addTLike(commentId, commentLikeDTO);
return ResponseEntity.ok("공감하였습니다");
return new ResponseDTO<String>("공감하였습니다");
}
@PostMapping("/{commentId}/Flike")
public ResponseEntity<String> addFlike(@PathVariable("commentId") Long commentId, @RequestBody CommentLikeDTO commentLikeDTO){
public ResponseDTO<String> addFlike(@PathVariable("commentId") Long commentId, @RequestBody CommentLikeDTO commentLikeDTO){
commentLikeService.addFLike(commentId, commentLikeDTO);
return ResponseEntity.ok("공감하였습니다");
return new ResponseDTO<String>("공감하였습니다");
}
@DeleteMapping("/{commentId}/Tlike")
public ResponseEntity<String> removeTLike(@PathVariable("commentId") Long commentId){
commentLikeService.removeTLike(commentId);
return ResponseEntity.ok("공감이 취소되었습니다");
@DeleteMapping("/{commentId}/{userId}/Tlike")
public ResponseDTO<String> removeTLike(@PathVariable("commentId") Long commentId, @PathVariable("userId") Long userId){
commentLikeService.removeTLike(commentId, userId);
return new ResponseDTO<String>("공감이 취소되었습니다");
}
@DeleteMapping("/{commentId}/Flike")
public ResponseEntity<String> removeFLike(@PathVariable("commentId") Long commentId){
commentLikeService.removeFLike(commentId);
return ResponseEntity.ok("공감이 취소되었습니다");
@DeleteMapping("/{commentId}/{userId}/Flike")
public ResponseDTO<String> removeFLike(@PathVariable("commentId") Long commentId, @PathVariable("userId") Long userId){
commentLikeService.removeFLike(commentId, userId);
return new ResponseDTO<String>("공감이 취소되었습니다");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public interface CommentLikeRepository extends JpaRepository<CommentLike, Long>

@Query("SELECT cl " +
"from CommentLike cl " +
"where cl.comment.writer.id = :userId and cl.comment.commentId = :commentId ")
Optional<CommentLike> findCommentLikeByUserId(@Param("userId") Long userId, @Param("commentId") Long commentId);
"where cl.user.id = :userId and cl.comment.commentId = :commentId ")
Optional<CommentLike> findCommentLikeByUserCommentId(@Param("userId") Long userId, @Param("commentId") Long commentId);

void delete(CommentLike commentLike);

Expand Down
15 changes: 7 additions & 8 deletions src/main/java/ne/ordinary/dd/service/CommentLikeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ne.ordinary.dd.domain.Comment;
import ne.ordinary.dd.domain.CommentLike;
import ne.ordinary.dd.domain.Notice;
import ne.ordinary.dd.domain.User;
import ne.ordinary.dd.model.CommentLikeDTO;
import ne.ordinary.dd.repository.CommentLikeRepository;
import ne.ordinary.dd.repository.CommentRepository;
Expand All @@ -24,7 +25,7 @@ public class CommentLikeService {

public void addTLike(Long commentId, CommentLikeDTO commentLikeDTO){

Optional<CommentLike> TLike = commentLikeRepository.findCommentLikeByUserId(commentLikeDTO.getUserId(), commentId);
Optional<CommentLike> TLike = commentLikeRepository.findCommentLikeByUserCommentId(commentLikeDTO.getUserId(), commentId);
Optional<Comment> comment = commentRepository.findById(commentId);

if (TLike.isEmpty()){
Expand All @@ -40,7 +41,7 @@ public void addTLike(Long commentId, CommentLikeDTO commentLikeDTO){
}
public void addFLike(Long commentId, CommentLikeDTO commentLikeDTO){

Optional<CommentLike> FLike = commentLikeRepository.findCommentLikeByCommentId(commentId);
Optional<CommentLike> FLike = commentLikeRepository.findCommentLikeByUserCommentId(commentLikeDTO.getUserId(), commentId);
Optional<Comment> comment = commentRepository.findById(commentId);

if (FLike.isEmpty()){
Expand All @@ -55,16 +56,14 @@ public void addFLike(Long commentId, CommentLikeDTO commentLikeDTO){
}
}

public void removeTLike(Long commentId){
Optional<CommentLike> TLike = commentLikeRepository.findCommentLikeByCommentId(commentId);
Optional<Comment> comment = commentRepository.findById(commentId);
public void removeTLike(Long commentId, Long userId){
Optional<CommentLike> TLike = commentLikeRepository.findCommentLikeByUserCommentId(commentId, userId);
if (TLike.isEmpty()){
commentLikeRepository.delete(TLike.get());
}
}
public void removeFLike(Long commentId){
Optional<CommentLike> FLike = commentLikeRepository.findCommentLikeByCommentId(commentId);
Optional<Comment> comment = commentRepository.findById(commentId);
public void removeFLike(Long commentId, Long userId){
Optional<CommentLike> FLike = commentLikeRepository.findCommentLikeByUserCommentId(commentId, userId);
if (FLike.isEmpty()){
commentLikeRepository.delete(FLike.get());
}
Expand Down