diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/controller/SharingCommentController.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/controller/SharingCommentController.java new file mode 100644 index 0000000..0e98d2a --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/controller/SharingCommentController.java @@ -0,0 +1,45 @@ +package com.umc.DongnaeFriend.domain.account.sharing.controller; + +import com.umc.DongnaeFriend.domain.account.sharing.dto.ReqSharingCommentDto; +import com.umc.DongnaeFriend.domain.account.sharing.dto.ResSharingCommentList; +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingComment; +import com.umc.DongnaeFriend.domain.account.sharing.service.SharingCommentService; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RequiredArgsConstructor +@RestController +@RequestMapping("/account-books/sharing/comments") +public class SharingCommentController { + private final SharingCommentService sharingCommentService; + + // [가계부 공유] 댓글 등록 + @PostMapping("/{accountBookId}") + public String postComment(@PathVariable("accountBookId") Long accountBookId, @RequestBody ReqSharingCommentDto reqSharingCommentDto) { + sharingCommentService.newComment(accountBookId, reqSharingCommentDto); + return ""; + } + + // [가계부 공유] 댓글 수정 + @PutMapping("/{commentId}") + public String putComment(@PathVariable("commentId") Long commentId, @RequestBody ReqSharingCommentDto reqSharingCommentDto) { + sharingCommentService.modifyComment(commentId, reqSharingCommentDto); + return ""; + } + + // [가계부 공유] 댓글 삭제 + @DeleteMapping("/{commentId}") + public String deleteComment(@PathVariable("commentId") Long commentId) { + sharingCommentService.deleteComment(commentId); + return ""; + } + + // [가계부 공유] 댓글 목록 조회 + @GetMapping("") + public ResSharingCommentList getList(@RequestParam Long accountBookId) { + ResSharingCommentList resSharingCommentList = sharingCommentService.getCommentList(accountBookId); + return resSharingCommentList; + } +} diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/controller/SharingCommentLikeController.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/controller/SharingCommentLikeController.java new file mode 100644 index 0000000..3cd7b64 --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/controller/SharingCommentLikeController.java @@ -0,0 +1,20 @@ +package com.umc.DongnaeFriend.domain.account.sharing.controller; + +import com.umc.DongnaeFriend.domain.account.sharing.dto.ReqSharingCommentDto; +import com.umc.DongnaeFriend.domain.account.sharing.service.SharingCommentLikeService; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +@RequiredArgsConstructor +@RestController +@RequestMapping("/account-books/sharing/likes") +public class SharingCommentLikeController { + private final SharingCommentLikeService sharingCommentLikeService; + + // [가계부 공유] 댓글 좋아요 + @PostMapping("/{commentId}") + public String postCommentLike(@PathVariable("commentId") Long commentId) { + sharingCommentLikeService.newLike(commentId); + return ""; + } +} diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/dto/ReqSharingCommentDto.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/dto/ReqSharingCommentDto.java new file mode 100644 index 0000000..e8a22b3 --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/dto/ReqSharingCommentDto.java @@ -0,0 +1,13 @@ +package com.umc.DongnaeFriend.domain.account.sharing.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class ReqSharingCommentDto { + Long parentCommentId; + String content; +} diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/dto/ResSharingCommentList.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/dto/ResSharingCommentList.java new file mode 100644 index 0000000..008f2eb --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/dto/ResSharingCommentList.java @@ -0,0 +1,20 @@ +package com.umc.DongnaeFriend.domain.account.sharing.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingComment; +import lombok.*; + +import java.util.List; +import java.util.Objects; + +@Getter +public class ResSharingCommentList { + int totalCount; + List commentList; + + @Builder + public ResSharingCommentList(int totalCount, List commentList) { + this.totalCount = totalCount; + this.commentList = commentList; + } +} diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/entity/SharingComment.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/entity/SharingComment.java index 4b56978..099fa81 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/entity/SharingComment.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/entity/SharingComment.java @@ -6,6 +6,7 @@ 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.user.entity.User; import java.util.List; import javax.persistence.*; @@ -43,4 +44,8 @@ public class SharingComment extends BaseTimeEntity { @Column(nullable = false) private String content; + + public void modifyComment(ReqSharingCommentDto reqSharingCommentDto) { + this.content = reqSharingCommentDto.getContent(); + } } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingCommentLikeRepository.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingCommentLikeRepository.java new file mode 100644 index 0000000..c6fabf2 --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingCommentLikeRepository.java @@ -0,0 +1,16 @@ +package com.umc.DongnaeFriend.domain.account.sharing.repository; + +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingComment; +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingCommentLike; +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; + +public interface SharingCommentLikeRepository extends JpaRepository { + @Query("SELECT u FROM User u WHERE u.id = :user_id") + User findByUserId(@Param("user_id") Long user_id); + + @Query("SELECT sc FROM SharingComment sc WHERE sc.id = :sharing_comment_id") + SharingComment findByCommentId(@Param("sharing_comment_id") Long sharing_comment_id); +} diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingCommentRepository.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingCommentRepository.java index 0eef074..06aa498 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingCommentRepository.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingCommentRepository.java @@ -1,15 +1,26 @@ package com.umc.DongnaeFriend.domain.account.sharing.repository; +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingBoard; import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingComment; +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; + import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Objects; @Repository public interface SharingCommentRepository extends JpaRepository { + @Query("SELECT u FROM User u WHERE u.id = :user_id") + User findByUserId(@Param("user_id") Long user_id); + @Query("SELECT sb FROM SharingBoard sb WHERE sb.id = :sharing_board_id") + SharingBoard findBySharingBoardId(@Param("sharing_board_id") Long sharing_board_id); + @Query("SELECT sc FROM SharingComment sc WHERE sc.sharingBoard = :sharingBoard") + List findAllByBoard(@Param("sharingBoard") SharingBoard sharingBoard); + public int countAllBySharingBoardId(Long sharing_board_id); int countAllByUserId(Long userId); @@ -17,4 +28,5 @@ public interface SharingCommentRepository extends JpaRepository getCommentByUserIdAndBoard(@Param("userId") Long userId); + } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/SharingCommentLikeService.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/SharingCommentLikeService.java new file mode 100644 index 0000000..62ceae1 --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/SharingCommentLikeService.java @@ -0,0 +1,31 @@ +package com.umc.DongnaeFriend.domain.account.sharing.service; + +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingComment; +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingCommentLike; +import com.umc.DongnaeFriend.domain.account.sharing.repository.SharingCommentLikeRepository; +import com.umc.DongnaeFriend.domain.user.entity.User; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@RequiredArgsConstructor +@Service +public class SharingCommentLikeService { + private final SharingCommentLikeRepository sharingCommentLikeRepository; + + public String newLike(Long commentId) { + // !임시! 유저 가져오기 + User user = sharingCommentLikeRepository.findByUserId(1L); + + // 댓글 가져오기 + SharingComment sharingComment = sharingCommentLikeRepository.findByCommentId(commentId); + + SharingCommentLike sharingCommentLike = SharingCommentLike.builder() + .user(user) + .sharingComment(sharingComment) + .build(); + + sharingCommentLikeRepository.save(sharingCommentLike); + + return "가계부 공유 댓글 좋아요 성공"; + } +} diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/SharingCommentService.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/SharingCommentService.java new file mode 100644 index 0000000..6c5f6bb --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/SharingCommentService.java @@ -0,0 +1,101 @@ +package com.umc.DongnaeFriend.domain.account.sharing.service; + +import com.umc.DongnaeFriend.domain.account.sharing.dto.ReqSharingCommentDto; +import com.umc.DongnaeFriend.domain.account.sharing.dto.ResSharingCommentList; +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.repository.SharingCommentRepository; +import com.umc.DongnaeFriend.domain.user.entity.User; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Objects; +import java.util.Optional; + +@RequiredArgsConstructor +@Service +public class SharingCommentService { + private final SharingCommentRepository sharingCommentRepository; + + public String newComment(Long accountBookId, ReqSharingCommentDto reqSharingCommentDto) { + // !임시! 유저 가져오기 + User user = sharingCommentRepository.findByUserId(1L); + + // 게시판 가져오기 + SharingBoard sharingBoard = sharingCommentRepository.findBySharingBoardId(accountBookId); + + // 대댓글 등록 + if (!(reqSharingCommentDto.getParentCommentId() == null)){ + // 부모 댓글 가져오기 + Optional parentCommentOptional = sharingCommentRepository.findById(reqSharingCommentDto.getParentCommentId()); + SharingComment parentComment = parentCommentOptional.get(); + + // 댓글 빌드 + SharingComment comment = SharingComment.builder() + .parentComment(parentComment) + .content(reqSharingCommentDto.getContent()) + .isDeleted(false) + .sharingBoard(sharingBoard) + .user(user) + .build(); + + sharingCommentRepository.save(comment); + + return "대댓글 등록 성공"; + + } + + // 댓글 등록 + SharingComment comment = SharingComment.builder() + .content(reqSharingCommentDto.getContent()) + .isDeleted(false) + .sharingBoard(sharingBoard) + .user(user) + .build(); + + sharingCommentRepository.save(comment); + + return "댓글 등록 성공"; + } + + // [가계부 공유] 댓글 수정 + public String modifyComment(Long commentId, ReqSharingCommentDto reqSharingCommentDto) { + // 댓글 찾기 + Optional sharingCommentOptional = sharingCommentRepository.findById(commentId); + SharingComment sharingComment = sharingCommentOptional.get(); + + sharingComment.modifyComment(reqSharingCommentDto); + + sharingCommentRepository.save(sharingComment); + + return "댓글 수정 성공"; + } + + // [가계부 공유] 댓글 삭제 + public String deleteComment(Long commentId) { + // 댓글 찾기 + Optional sharingCommentOptional = sharingCommentRepository.findById(commentId); + SharingComment sharingComment = sharingCommentOptional.get(); + + sharingCommentRepository.delete(sharingComment); + + return "댓글 삭제 성공"; + } + + // [가계부 공유] 댓글 목록 조회 + public ResSharingCommentList getCommentList(Long accountBookId) { + // 게시판 가져오기 + SharingBoard sharingBoard = sharingCommentRepository.findBySharingBoardId(accountBookId); + + List commentList = sharingCommentRepository.findAllByBoard(sharingBoard); + + ResSharingCommentList resSharingCommentList = ResSharingCommentList.builder() + .totalCount(commentList.size()) + .commentList(sharingCommentRepository.findAllByBoard(sharingBoard)) + .build(); + + return resSharingCommentList; + } + +}