Skip to content

Commit

Permalink
feat: 좋아요 상태 응답 필드에 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
peeerr committed Nov 6, 2024
1 parent 740538a commit d6d2513
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ public class FriendDiaryResponse {
private String emoji;
private String content;
private LikeCountResponse likeCount;
private boolean isLiked;
private LocalDateTime createAt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public class MyDiaryResponse {
private boolean isBookmark;
private String emoji;
private String content;
private boolean isLiked;
private LocalDateTime createAt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface LikeRepository extends JpaRepository<Like, Long> {

Long countByDiary(Diary diary);

boolean existsByMemberIdAndDiaryId(Long memberId, Long diaryId);

}
18 changes: 11 additions & 7 deletions src/main/java/com/potatocake/everymoment/service/DiaryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -138,7 +140,7 @@ public MyDiariesResponse getMyDiaries(Long memberId, DiaryFilterRequest diaryFil

Specification<Diary> spec;

if(!diaryFilterRequest.hasFilter()){
if (!diaryFilterRequest.hasFilter()) {
LocalDate today = LocalDate.now();

spec = DiarySpecification.filterDiaries(
Expand All @@ -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,
Expand All @@ -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<MyDiarySimpleResponse> diaryDTOs = diaryPage.getContent().stream()
Expand All @@ -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);
}

// 내 일기 위치 조회
Expand Down Expand Up @@ -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<DiaryCategory> diaryCategories = diaryCategoryRepository.findByDiary(savedDiary);
List<CategoryResponse> categoryResponseList = diaryCategories.stream()
Expand All @@ -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)
Expand All @@ -300,6 +303,7 @@ private MyDiaryResponse convertToMyDiaryResponseDto(Diary savedDiary) {
.isBookmark(savedDiary.isBookmark())
.emoji(savedDiary.getEmoji())
.content(savedDiary.getContent())
.isLiked(isLiked)
.createAt(savedDiary.getCreateAt())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,16 +58,17 @@ public FriendDiariesResponse getFriendDiaries(Long memberId, DiaryFilterRequest
List<String> emojis = diaryFilterRequest.getEmojis();

Specification<Diary> 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<FriendDiarySimpleResponse> friendDiarySimpleResponseList = diaryPage.getContent().stream()
.map(this::convertToFriendDiariesResponseDTO)
Expand All @@ -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);
}

Expand Down Expand Up @@ -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)
Expand All @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit d6d2513

Please sign in to comment.