diff --git a/src/main/java/com/potatocake/everymoment/dto/response/FriendDiaryResponse.java b/src/main/java/com/potatocake/everymoment/dto/response/FriendDiaryResponse.java index 47ea8da..d533783 100644 --- a/src/main/java/com/potatocake/everymoment/dto/response/FriendDiaryResponse.java +++ b/src/main/java/com/potatocake/everymoment/dto/response/FriendDiaryResponse.java @@ -18,5 +18,6 @@ public class FriendDiaryResponse { private String emoji; private String content; private LikeCountResponse likeCount; + private boolean isLiked; private LocalDateTime createAt; } diff --git a/src/main/java/com/potatocake/everymoment/dto/response/MyDiaryResponse.java b/src/main/java/com/potatocake/everymoment/dto/response/MyDiaryResponse.java index a92bc41..c8c8110 100644 --- a/src/main/java/com/potatocake/everymoment/dto/response/MyDiaryResponse.java +++ b/src/main/java/com/potatocake/everymoment/dto/response/MyDiaryResponse.java @@ -15,5 +15,6 @@ public class MyDiaryResponse { private boolean isBookmark; private String emoji; private String content; + private boolean isLiked; private LocalDateTime createAt; } diff --git a/src/main/java/com/potatocake/everymoment/repository/LikeRepository.java b/src/main/java/com/potatocake/everymoment/repository/LikeRepository.java index dc383b1..367cd00 100644 --- a/src/main/java/com/potatocake/everymoment/repository/LikeRepository.java +++ b/src/main/java/com/potatocake/everymoment/repository/LikeRepository.java @@ -11,4 +11,6 @@ public interface LikeRepository extends JpaRepository { Long countByDiary(Diary diary); + boolean existsByMemberIdAndDiaryId(Long memberId, Long diaryId); + } diff --git a/src/main/java/com/potatocake/everymoment/service/DiaryService.java b/src/main/java/com/potatocake/everymoment/service/DiaryService.java index 12d070c..3378c30 100644 --- a/src/main/java/com/potatocake/everymoment/service/DiaryService.java +++ b/src/main/java/com/potatocake/everymoment/service/DiaryService.java @@ -22,6 +22,7 @@ import com.potatocake.everymoment.repository.DiaryCategoryRepository; import com.potatocake.everymoment.repository.DiaryRepository; import com.potatocake.everymoment.repository.FileRepository; +import com.potatocake.everymoment.repository.LikeRepository; import com.potatocake.everymoment.repository.MemberRepository; import java.time.LocalDate; import java.util.List; @@ -48,6 +49,7 @@ public class DiaryService { private final MemberRepository memberRepository; private final CategoryRepository categoryRepository; private final FileRepository fileRepository; + private final LikeRepository likeRepository; private final GeometryFactory geometryFactory; private final NotificationService notificationService; @@ -138,7 +140,7 @@ public MyDiariesResponse getMyDiaries(Long memberId, DiaryFilterRequest diaryFil Specification spec; - if(!diaryFilterRequest.hasFilter()){ + if (!diaryFilterRequest.hasFilter()) { LocalDate today = LocalDate.now(); spec = DiarySpecification.filterDiaries( @@ -153,9 +155,7 @@ public MyDiariesResponse getMyDiaries(Long memberId, DiaryFilterRequest diaryFil diaryPage = diaryRepository.findAll(spec, PageRequest.of(diaryFilterRequest.getKey(), diaryFilterRequest.getSize())); - } - - else{ + } else { spec = DiarySpecification.filterDiaries( diaryFilterRequest.getKeyword(), emojis, @@ -167,7 +167,8 @@ public MyDiariesResponse getMyDiaries(Long memberId, DiaryFilterRequest diaryFil .and((root, query, builder) -> builder.equal(root.get("member"), currentMember)); diaryPage = diaryRepository.findAll(spec, - PageRequest.of(diaryFilterRequest.getKey(), diaryFilterRequest.getSize(), Sort.by(Sort.Direction.DESC, "createAt"))); + PageRequest.of(diaryFilterRequest.getKey(), diaryFilterRequest.getSize(), + Sort.by(Sort.Direction.DESC, "createAt"))); } List diaryDTOs = diaryPage.getContent().stream() @@ -186,7 +187,7 @@ public MyDiariesResponse getMyDiaries(Long memberId, DiaryFilterRequest diaryFil @Transactional(readOnly = true) public MyDiaryResponse getMyDiary(Long memberId, Long diaryId) { Diary diary = getExistDiary(memberId, diaryId); - return convertToMyDiaryResponseDto(diary); + return convertToMyDiaryResponseDto(diary, memberId); } // 내 일기 위치 조회 @@ -282,7 +283,7 @@ private Diary getExistDiary(Long memberId, Long diaryId) { } //상세 조회시 일기DTO 변환 - private MyDiaryResponse convertToMyDiaryResponseDto(Diary savedDiary) { + private MyDiaryResponse convertToMyDiaryResponseDto(Diary savedDiary, Long memberId) { // 카테고리 찾음 List diaryCategories = diaryCategoryRepository.findByDiary(savedDiary); List categoryResponseList = diaryCategories.stream() @@ -292,6 +293,8 @@ private MyDiaryResponse convertToMyDiaryResponseDto(Diary savedDiary) { .build()) .collect(Collectors.toList()); + boolean isLiked = likeRepository.existsByMemberIdAndDiaryId(memberId, savedDiary.getId()); + return MyDiaryResponse.builder() .id(savedDiary.getId()) .categories(categoryResponseList) @@ -300,6 +303,7 @@ private MyDiaryResponse convertToMyDiaryResponseDto(Diary savedDiary) { .isBookmark(savedDiary.isBookmark()) .emoji(savedDiary.getEmoji()) .content(savedDiary.getContent()) + .isLiked(isLiked) .createAt(savedDiary.getCreateAt()) .build(); } diff --git a/src/main/java/com/potatocake/everymoment/service/FriendDiaryService.java b/src/main/java/com/potatocake/everymoment/service/FriendDiaryService.java index 0d324a9..14daba2 100644 --- a/src/main/java/com/potatocake/everymoment/service/FriendDiaryService.java +++ b/src/main/java/com/potatocake/everymoment/service/FriendDiaryService.java @@ -20,7 +20,6 @@ import com.potatocake.everymoment.repository.FriendRepository; import com.potatocake.everymoment.repository.LikeRepository; import com.potatocake.everymoment.repository.MemberRepository; -import java.time.LocalDate; import java.util.List; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; @@ -59,16 +58,17 @@ public FriendDiariesResponse getFriendDiaries(Long memberId, DiaryFilterRequest List emojis = diaryFilterRequest.getEmojis(); Specification spec = FriendDiarySpecification.filterDiaries( - diaryFilterRequest.getKeyword(), - emojis, - categories, - diaryFilterRequest.getDate(), - diaryFilterRequest.getFrom(), - diaryFilterRequest.getUntil()) - .and((root, query, builder) -> root.get("member").get("id").in(friendIdList)); + diaryFilterRequest.getKeyword(), + emojis, + categories, + diaryFilterRequest.getDate(), + diaryFilterRequest.getFrom(), + diaryFilterRequest.getUntil()) + .and((root, query, builder) -> root.get("member").get("id").in(friendIdList)); diaryPage = diaryRepository.findAll(spec, - PageRequest.of(diaryFilterRequest.getKey(), diaryFilterRequest.getSize(), Sort.by(Sort.Direction.DESC, "createAt"))); + PageRequest.of(diaryFilterRequest.getKey(), diaryFilterRequest.getSize(), + Sort.by(Sort.Direction.DESC, "createAt"))); List friendDiarySimpleResponseList = diaryPage.getContent().stream() .map(this::convertToFriendDiariesResponseDTO) @@ -87,7 +87,7 @@ public FriendDiaryResponse getFriendDiary(Long memberId, Long diaryId) { Diary diary = diaryRepository.findById(diaryId) .orElseThrow(() -> new GlobalException(ErrorCode.DIARY_NOT_FOUND)); - if(!diary.isPublic()){ + if (!diary.isPublic()) { throw new GlobalException(ErrorCode.DIARY_NOT_PUBLIC); } @@ -115,6 +115,7 @@ public FriendDiaryResponse getFriendDiary(Long memberId, Long diaryId) { //like 갯수 반환 Long likeCount = likeRepository.countByDiary(diary); + boolean isLiked = likeRepository.existsByMemberIdAndDiaryId(memberId, diary.getId()); LikeCountResponse count = LikeCountResponse.builder() .likeCount(likeCount) @@ -127,6 +128,7 @@ public FriendDiaryResponse getFriendDiary(Long memberId, Long diaryId) { .emoji(diary.getEmoji()) .content(diary.getContent()) .likeCount(count) + .isLiked(isLiked) .createAt(diary.getCreateAt()) .build(); diff --git a/src/main/java/com/potatocake/everymoment/service/LikeService.java b/src/main/java/com/potatocake/everymoment/service/LikeService.java index 6f9d90f..d2c5586 100644 --- a/src/main/java/com/potatocake/everymoment/service/LikeService.java +++ b/src/main/java/com/potatocake/everymoment/service/LikeService.java @@ -41,7 +41,7 @@ public void toggleLike(Long memberId, Long diaryId) { Diary diary = diaryRepository.findById(diaryId) .orElseThrow(() -> new GlobalException(ErrorCode.DIARY_NOT_FOUND)); - if (!diary.isPublic()) { + if (!diary.isPublic() && !diary.checkOwner(memberId)) { throw new GlobalException(ErrorCode.DIARY_NOT_PUBLIC); }