From 5dc57971a08c15137d196062a12c8ab719c6bf22 Mon Sep 17 00:00:00 2001 From: sungone <20200999@sungshin.ac.kr> Date: Mon, 7 Aug 2023 00:19:58 +0900 Subject: [PATCH 1/4] =?UTF-8?q?:sparkles:=20Feat=20#66=20:=20[=EB=8F=99?= =?UTF-8?q?=EB=84=A4=EC=A0=95=EB=B3=B4]=20=EB=8C=93=EA=B8=80=20=EB=93=B1?= =?UTF-8?q?=EB=A1=9D=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/DongnaeCommentController.java | 23 +++++++ .../domain/dongnae/dto/DongnaeCommentDto.java | 13 ++++ .../respository/DongnaeCommentRepository.java | 7 +++ .../service/DongnaeCommentService.java | 61 +++++++++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeCommentController.java create mode 100644 src/main/java/com/umc/DongnaeFriend/domain/dongnae/dto/DongnaeCommentDto.java create mode 100644 src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeCommentService.java diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeCommentController.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeCommentController.java new file mode 100644 index 0000000..af6d763 --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeCommentController.java @@ -0,0 +1,23 @@ +package com.umc.DongnaeFriend.domain.dongnae.controller; + +import com.umc.DongnaeFriend.domain.account.sharing.dto.ReqSharingCommentDto; +import com.umc.DongnaeFriend.domain.dongnae.dto.DongnaeCommentDto; +import com.umc.DongnaeFriend.domain.dongnae.service.DongnaeCommentService; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +@RequiredArgsConstructor +@RestController +@RequestMapping("/town-information/comments") +public class DongnaeCommentController { + private final DongnaeCommentService dongnaeCommentService; + + // [동네정보] 댓글 등록 + @PostMapping("/{townInformationId}") + public String postComment(@PathVariable("townInformationId") Long townInformationId, @RequestBody DongnaeCommentDto dongnaeCommentDto) { + dongnaeCommentService.newComment(townInformationId, dongnaeCommentDto); + return ""; + } + + +} diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/dto/DongnaeCommentDto.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/dto/DongnaeCommentDto.java new file mode 100644 index 0000000..8887994 --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/dto/DongnaeCommentDto.java @@ -0,0 +1,13 @@ +package com.umc.DongnaeFriend.domain.dongnae.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class DongnaeCommentDto { + Long parentCommentId; + String content; +} diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeCommentRepository.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeCommentRepository.java index c5368cf..c1401ef 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeCommentRepository.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeCommentRepository.java @@ -1,6 +1,8 @@ package com.umc.DongnaeFriend.domain.dongnae.respository; +import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeBoard; import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeComment; +import com.umc.DongnaeFriend.domain.user.entity.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -16,4 +18,9 @@ public interface DongnaeCommentRepository extends JpaRepository getCommentByUserIdAndBoard(@Param("userId") Long userId); + @Query("SELECT u FROM User u WHERE u.id = :user_id") + User findByUserId(@Param("user_id") Long user_id); + + @Query("SELECT db FROM DongnaeBoard db WHERE db.id = :dongnae_board_id") + DongnaeBoard findByDongnaeBoardId(@Param("dongnae_board_id") Long dongnae_board_id); } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeCommentService.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeCommentService.java new file mode 100644 index 0000000..ccd3e3a --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeCommentService.java @@ -0,0 +1,61 @@ +package com.umc.DongnaeFriend.domain.dongnae.service; + +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingBoard; +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingComment; +import com.umc.DongnaeFriend.domain.dongnae.dto.DongnaeCommentDto; +import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeBoard; +import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeComment; +import com.umc.DongnaeFriend.domain.dongnae.respository.DongnaeCommentRepository; +import com.umc.DongnaeFriend.domain.user.entity.User; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.Optional; + +@RequiredArgsConstructor +@Service +public class DongnaeCommentService { + private final DongnaeCommentRepository dongnaeCommentRepository; + + + public String newComment(Long townInformationId, DongnaeCommentDto dongnaeCommentDto) { + // !임시! 유저 가져오기 + User user = dongnaeCommentRepository.findByUserId(1L); + + // 게시판 가져오기 + DongnaeBoard dongnaeBoard = dongnaeCommentRepository.findByDongnaeBoardId(townInformationId); + + // 대댓글 등록 + if (!(dongnaeCommentDto.getParentCommentId() == null)){ + // 부모 댓글 가져오기 + Optional parentCommentOptional = dongnaeCommentRepository.findById(dongnaeCommentDto.getParentCommentId()); + DongnaeComment parentComment = parentCommentOptional.get(); + + // 댓글 빌드 + DongnaeComment comment = DongnaeComment.builder() + .parentComment(parentComment) + .content(dongnaeCommentDto.getContent()) + .isDeleted(false) + .dongnaeBoard(dongnaeBoard) + .user(user) + .build(); + + dongnaeCommentRepository.save(comment); + + return "대댓글 등록 성공"; + + } + + // 댓글 빌드 + DongnaeComment comment = DongnaeComment.builder() + .content(dongnaeCommentDto.getContent()) + .isDeleted(false) + .dongnaeBoard(dongnaeBoard) + .user(user) + .build(); + + dongnaeCommentRepository.save(comment); + + return "댓글 등록 성공"; + } +} From 7c637e0f30b59d42eacdab7489f8acf4c8a72867 Mon Sep 17 00:00:00 2001 From: sungone <20200999@sungshin.ac.kr> Date: Mon, 7 Aug 2023 00:26:29 +0900 Subject: [PATCH 2/4] =?UTF-8?q?:sparkles:=20Feat=20#66=20:=20[=EB=8F=99?= =?UTF-8?q?=EB=84=A4=EC=A0=95=EB=B3=B4]=20=EB=8C=93=EA=B8=80=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/DongnaeCommentController.java | 7 +++++++ .../domain/dongnae/entity/DongnaeComment.java | 5 +++++ .../dongnae/service/DongnaeCommentService.java | 15 +++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeCommentController.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeCommentController.java index af6d763..664b20d 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeCommentController.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeCommentController.java @@ -19,5 +19,12 @@ public String postComment(@PathVariable("townInformationId") Long townInformatio return ""; } + // [동네정보] 댓글 수정 + @PutMapping("/{commentId}") + public String putComment(@PathVariable("commentId") Long commentId, @RequestBody DongnaeCommentDto dongnaeCommentDto) { + dongnaeCommentService.modifyComment(commentId, dongnaeCommentDto); + return ""; + } + } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/entity/DongnaeComment.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/entity/DongnaeComment.java index 62ea0d8..a94bcf3 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/entity/DongnaeComment.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/entity/DongnaeComment.java @@ -6,6 +6,8 @@ import static lombok.AccessLevel.PROTECTED; import com.umc.DongnaeFriend.domain.BaseTimeEntity; +import com.umc.DongnaeFriend.domain.account.sharing.dto.ReqSharingCommentDto; +import com.umc.DongnaeFriend.domain.dongnae.dto.DongnaeCommentDto; import com.umc.DongnaeFriend.domain.user.entity.User; import java.util.List; import javax.persistence.*; @@ -44,4 +46,7 @@ public class DongnaeComment extends BaseTimeEntity { @Column(nullable = false) private String content; + public void modifyComment(DongnaeCommentDto dongnaeCommentDto) { + this.content = dongnaeCommentDto.getContent(); + } } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeCommentService.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeCommentService.java index ccd3e3a..86ad136 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeCommentService.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeCommentService.java @@ -1,5 +1,6 @@ package com.umc.DongnaeFriend.domain.dongnae.service; +import com.umc.DongnaeFriend.domain.account.sharing.dto.ReqSharingCommentDto; import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingBoard; import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingComment; import com.umc.DongnaeFriend.domain.dongnae.dto.DongnaeCommentDto; @@ -58,4 +59,18 @@ public String newComment(Long townInformationId, DongnaeCommentDto dongnaeCommen return "댓글 등록 성공"; } + + // [동네정보] 댓글 수정 + public String modifyComment(Long commentId, DongnaeCommentDto dongnaeCommentDto) { + // 댓글 찾기 + Optional dongnaeCommentOptional = dongnaeCommentRepository.findById(commentId); + DongnaeComment dongnaeComment = dongnaeCommentOptional.get(); + + dongnaeComment.modifyComment(dongnaeCommentDto); + + dongnaeCommentRepository.save(dongnaeComment); + + return "댓글 수정 성공"; + } + } From b7f3112d2155d4bd3de1c0c74b01f75b8af4869a Mon Sep 17 00:00:00 2001 From: sungone <20200999@sungshin.ac.kr> Date: Mon, 7 Aug 2023 00:28:17 +0900 Subject: [PATCH 3/4] =?UTF-8?q?:sparkles:=20Feat=20#66=20:=20[=EB=8F=99?= =?UTF-8?q?=EB=84=A4=EC=A0=95=EB=B3=B4]=20=EB=8C=93=EA=B8=80=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dongnae/controller/DongnaeCommentController.java | 7 ++++++- .../domain/dongnae/service/DongnaeCommentService.java | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeCommentController.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeCommentController.java index 664b20d..ce12431 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeCommentController.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeCommentController.java @@ -26,5 +26,10 @@ public String putComment(@PathVariable("commentId") Long commentId, @RequestBod return ""; } - + // [동네정보] 댓글 삭제 + @DeleteMapping("/{commentId}") + public String deleteComment(@PathVariable("commentId") Long commentId) { + dongnaeCommentService.deleteComment(commentId); + return ""; + } } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeCommentService.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeCommentService.java index 86ad136..d8f435b 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeCommentService.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeCommentService.java @@ -73,4 +73,13 @@ public String modifyComment(Long commentId, DongnaeCommentDto dongnaeCommentDto) return "댓글 수정 성공"; } + public String deleteComment(Long commentId) { + // 댓글 찾기 + Optional dongnaeCommentOptional = dongnaeCommentRepository.findById(commentId); + DongnaeComment dongnaeComment = dongnaeCommentOptional.get(); + + dongnaeCommentRepository.delete(dongnaeComment); + + return "댓글 삭제 성공"; + } } From c7845718883497460b50ee7a3cba025c76548c25 Mon Sep 17 00:00:00 2001 From: sungone <20200999@sungshin.ac.kr> Date: Mon, 7 Aug 2023 00:43:20 +0900 Subject: [PATCH 4/4] =?UTF-8?q?:sparkles:=20Feat=20#66=20:=20[=EB=8F=99?= =?UTF-8?q?=EB=84=A4=EC=A0=95=EB=B3=B4]=20=EB=8C=93=EA=B8=80=20=EC=A2=8B?= =?UTF-8?q?=EC=95=84=EC=9A=94=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/DongnaeCommentController.java | 8 +++++ .../DongnaeCommentLikeRepository.java | 16 +++++++++ .../service/DongnaeCommentService.java | 34 +++++++++++++++++-- 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeCommentLikeRepository.java diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeCommentController.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeCommentController.java index ce12431..2cd80a9 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeCommentController.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeCommentController.java @@ -32,4 +32,12 @@ public String deleteComment(@PathVariable("commentId") Long commentId) { dongnaeCommentService.deleteComment(commentId); return ""; } + + // [동네정보] 댓글 좋아요 + @PostMapping("/likes/{commentId}") + public String postCommentLike(@PathVariable("commentId") Long commentId) { + dongnaeCommentService.newLike(commentId); + return ""; + } + } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeCommentLikeRepository.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeCommentLikeRepository.java new file mode 100644 index 0000000..fc64cfb --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeCommentLikeRepository.java @@ -0,0 +1,16 @@ +package com.umc.DongnaeFriend.domain.dongnae.respository; + +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingBoard; +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingSympathy; +import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeComment; +import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeCommentLike; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +@Repository +public interface DongnaeCommentLikeRepository extends JpaRepository { + @Query(value = "SELECT dongnae_comment_like.* FROM dongnae_comment_like WHERE dongnae_comment_like.dongnae_comment_id = :comment_id", nativeQuery = true) + DongnaeCommentLike findByCommentId(@Param("comment_id") DongnaeComment comment_id); +} diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeCommentService.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeCommentService.java index d8f435b..4f912b0 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeCommentService.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeCommentService.java @@ -1,11 +1,11 @@ package com.umc.DongnaeFriend.domain.dongnae.service; -import com.umc.DongnaeFriend.domain.account.sharing.dto.ReqSharingCommentDto; -import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingBoard; -import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingComment; +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingSympathy; import com.umc.DongnaeFriend.domain.dongnae.dto.DongnaeCommentDto; import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeBoard; import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeComment; +import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeCommentLike; +import com.umc.DongnaeFriend.domain.dongnae.respository.DongnaeCommentLikeRepository; import com.umc.DongnaeFriend.domain.dongnae.respository.DongnaeCommentRepository; import com.umc.DongnaeFriend.domain.user.entity.User; import lombok.RequiredArgsConstructor; @@ -17,6 +17,7 @@ @Service public class DongnaeCommentService { private final DongnaeCommentRepository dongnaeCommentRepository; + private final DongnaeCommentLikeRepository dongnaeCommentLikeRepository; public String newComment(Long townInformationId, DongnaeCommentDto dongnaeCommentDto) { @@ -82,4 +83,31 @@ public String deleteComment(Long commentId) { return "댓글 삭제 성공"; } + + public String newLike(Long commentId) { + // !임시! 유저 가져오기 + User user = dongnaeCommentRepository.findByUserId(1L); + + // 댓글 가져오기 + Optional dongnaeCommentOptional = dongnaeCommentRepository.findById(commentId); + DongnaeComment dongnaeComment = dongnaeCommentOptional.get(); + + // 좋아요 유무 확인하기 + DongnaeCommentLike dongnaeCommentLikeExist = dongnaeCommentLikeRepository.findByCommentId(dongnaeComment); + + if (dongnaeCommentLikeExist == null) { + DongnaeCommentLike dongnaeCommentLike = DongnaeCommentLike.builder() + .user(user) + .dongnaeComment(dongnaeComment) + .build(); + + dongnaeCommentLikeRepository.save(dongnaeCommentLike); + + return "동네정보 댓글 좋아요 성공"; + } + + dongnaeCommentLikeRepository.delete(dongnaeCommentLikeExist); + + return "동네정보 댓글 좋아요 삭제 성공"; + } }