From 9f367d5e8cce42006532179e32074f0942bf0e83 Mon Sep 17 00:00:00 2001 From: sungone <20200999@sungshin.ac.kr> Date: Sun, 20 Aug 2023 09:54:58 +0900 Subject: [PATCH] =?UTF-8?q?:white=5Fcheck=5Fmark:=20Feat=20#71=20:=20?= =?UTF-8?q?=EC=9C=A0=EC=A0=80=20=EC=A0=95=EB=B3=B4=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/SharingCommentLikeService.java | 16 ++++++++++++++-- .../sharing/service/SharingCommentService.java | 15 +++++++++++++-- .../sharing/service/SharingSympathyService.java | 14 ++++++++++++-- .../dongnae/service/DongnaeCommentService.java | 17 +++++++++++++---- .../domain/scrap/service/ScrapService.java | 15 +++++++++++++-- 5 files changed, 65 insertions(+), 12 deletions(-) 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 index 167ba31..a34cb46 100644 --- 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 @@ -5,17 +5,23 @@ import com.umc.DongnaeFriend.domain.account.sharing.repository.SharingCommentLikeRepository; import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeCommentLike; import com.umc.DongnaeFriend.domain.user.entity.User; +import com.umc.DongnaeFriend.domain.user.repository.UserRepository; +import com.umc.DongnaeFriend.global.exception.CustomException; +import com.umc.DongnaeFriend.global.exception.ErrorCode; import lombok.RequiredArgsConstructor; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; @RequiredArgsConstructor @Service public class SharingCommentLikeService { private final SharingCommentLikeRepository sharingCommentLikeRepository; + private final UserRepository userRepository; + public String newLike(Long commentId) { - // !임시! 유저 가져오기 - User user = sharingCommentLikeRepository.findByUserId(1L); + + User user = findUser(); // 댓글 가져오기 SharingComment sharingComment = sharingCommentLikeRepository.findByCommentId(commentId); @@ -38,4 +44,10 @@ public String newLike(Long commentId) { return "가계부 공유 댓글 좋아요 삭제 성공"; } + + public User findUser() { + Object userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + return userRepository.findById((Long) userId) + .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); + } } 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 index 02aa070..2a90f56 100644 --- 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 @@ -5,7 +5,11 @@ 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 com.umc.DongnaeFriend.domain.user.repository.UserRepository; +import com.umc.DongnaeFriend.global.exception.CustomException; +import com.umc.DongnaeFriend.global.exception.ErrorCode; import lombok.RequiredArgsConstructor; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import java.util.List; @@ -15,10 +19,11 @@ @Service public class SharingCommentService { private final SharingCommentRepository sharingCommentRepository; + private final UserRepository userRepository; public String newComment(Long accountBookId, ReqSharingCommentDto reqSharingCommentDto) { - // !임시! 유저 가져오기 - User user = sharingCommentRepository.findByUserId(1L); + + User user = findUser(); // 게시판 가져오기 SharingBoard sharingBoard = sharingCommentRepository.findBySharingBoardId(accountBookId); @@ -91,4 +96,10 @@ public ReqSharingCommentDto.CommentListResponse getList(Long accountBookId) { return ReqSharingCommentDto.CommentListResponse.of(list); } + + public User findUser() { + Object userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + return userRepository.findById((Long) userId) + .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); + } } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/SharingSympathyService.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/SharingSympathyService.java index 3b78d48..7484a8e 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/SharingSympathyService.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/SharingSympathyService.java @@ -4,17 +4,22 @@ import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingSympathy; import com.umc.DongnaeFriend.domain.account.sharing.repository.SharingSympathyRepository; import com.umc.DongnaeFriend.domain.user.entity.User; +import com.umc.DongnaeFriend.domain.user.repository.UserRepository; +import com.umc.DongnaeFriend.global.exception.CustomException; +import com.umc.DongnaeFriend.global.exception.ErrorCode; import lombok.RequiredArgsConstructor; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; @RequiredArgsConstructor @Service public class SharingSympathyService { private final SharingSympathyRepository sharingSympathyRepository; + private final UserRepository userRepository; + public String newSympathy(Long accountBookId){ - // !임시! 유저 가져오기 - User user = sharingSympathyRepository.findByUserId(1L); + User user = findUser(); // 게시판 가져오기 SharingBoard sharingBoard = sharingSympathyRepository.findBySharingBoardId(accountBookId); @@ -40,4 +45,9 @@ public String newSympathy(Long accountBookId){ return "[가계부 공유] 공감 삭제 성공"; } + public User findUser() { + Object userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + return userRepository.findById((Long) userId) + .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); + } } 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 7de8f05..264dea4 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 @@ -8,7 +8,11 @@ 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 com.umc.DongnaeFriend.domain.user.repository.UserRepository; +import com.umc.DongnaeFriend.global.exception.CustomException; +import com.umc.DongnaeFriend.global.exception.ErrorCode; import lombok.RequiredArgsConstructor; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import java.util.List; @@ -19,11 +23,11 @@ public class DongnaeCommentService { private final DongnaeCommentRepository dongnaeCommentRepository; private final DongnaeCommentLikeRepository dongnaeCommentLikeRepository; + private final UserRepository userRepository; public String newComment(Long townInformationId, DongnaeCommentDto dongnaeCommentDto) { - // !임시! 유저 가져오기 - User user = dongnaeCommentRepository.findByUserId(1L); + User user = findUser(); // 게시판 가져오기 DongnaeBoard dongnaeBoard = dongnaeCommentRepository.findByDongnaeBoardId(townInformationId); @@ -86,8 +90,7 @@ public String deleteComment(Long commentId) { } public String newLike(Long commentId) { - // !임시! 유저 가져오기 - User user = dongnaeCommentRepository.findByUserId(1L); + User user = findUser(); // 댓글 가져오기 Optional dongnaeCommentOptional = dongnaeCommentRepository.findById(commentId); @@ -121,4 +124,10 @@ public DongnaeCommentDto.CommentListResponse getList(Long id) { return DongnaeCommentDto.CommentListResponse.of(list); } + + public User findUser() { + Object userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + return userRepository.findById((Long) userId) + .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); + } } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/scrap/service/ScrapService.java b/src/main/java/com/umc/DongnaeFriend/domain/scrap/service/ScrapService.java index e9a342e..79c0d68 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/scrap/service/ScrapService.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/scrap/service/ScrapService.java @@ -6,17 +6,22 @@ import com.umc.DongnaeFriend.domain.scrap.entity.Scrap; import com.umc.DongnaeFriend.domain.scrap.repository.ScrapRepository; import com.umc.DongnaeFriend.domain.user.entity.User; +import com.umc.DongnaeFriend.domain.user.repository.UserRepository; +import com.umc.DongnaeFriend.global.exception.CustomException; +import com.umc.DongnaeFriend.global.exception.ErrorCode; import lombok.RequiredArgsConstructor; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; @RequiredArgsConstructor @Service public class ScrapService { private final ScrapRepository scrapRepository; + private final UserRepository userRepository; public String newScrap(ReqScrapDto reqScrapDto) { - // !임시! 유저 가져오기 - User user = scrapRepository.findByUserId(1L); + + User user = findUser(); // 가계부 공유 if (!(reqScrapDto.getAccountBookId() == null)) { @@ -66,4 +71,10 @@ else if (!(reqScrapDto.getTownInformationId() == null)) { return "스크랩 성공"; } + + public User findUser() { + Object userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + return userRepository.findById((Long) userId) + .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); + } }