Skip to content

Commit

Permalink
[feat] 해당 일기별 댓글 조회 api 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryeolee committed May 10, 2024
1 parent caf70b8 commit 8bdfdf4
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.request.Like;
import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.*;
import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.Diary;
import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.DiaryComment;
import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.MyVeggie;
import com.modernfarmer.farmusspring.domain.myveggiegarden.service.MyVeggieDiaryService;
import com.modernfarmer.farmusspring.domain.user.entity.User;
import com.modernfarmer.farmusspring.global.response.BaseResponseDto;
import com.modernfarmer.farmusspring.global.response.SuccessCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.repository.query.Param;
import org.springframework.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
Expand Down Expand Up @@ -98,6 +100,19 @@ public BaseResponseDto<?> cancelLike(
myVeggieDiaryService.cancelLike(userObject, diaryObject);
return BaseResponseDto.of(SuccessCode.SUCCESS, null);
}

@GetMapping(value = "/{diaryId}/{farmClubId}/comment")
public BaseResponseDto<?> selectComment(
@AuthenticationPrincipal CustomUser user,
@PathVariable("diaryId") Long diaryId,
@PathVariable("farmClubId") Long farmClubId
) {
List<DiaryCommentContent> diaryCommentList = myVeggieDiaryService.selectComment(user.getUserId(), diaryId, farmClubId);

return BaseResponseDto.of(SuccessCode.SUCCESS, diaryCommentList);
}


@PostMapping(value = "/comment")
public BaseResponseDto<?> writeComment(
@AuthenticationPrincipal CustomUser user,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response;

import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.Diary;
import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.DiaryComment;
import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.MyVeggie;
import com.modernfarmer.farmusspring.domain.myveggiegarden.util.DateManager;
import com.modernfarmer.farmusspring.domain.user.entity.User;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Objects;

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Slf4j
public class DiaryCommentContent {

private Boolean myComment;
private String content;
private String date;
private String nickname;
private String profileImage;

public static DiaryCommentContent of(Boolean check, DiaryComment diaryComment, User user){
return new DiaryCommentContent(
check,
diaryComment.getComment(),
DateManager.parsingDotDateTime(diaryComment.getCreatedDate()),
user.getNickname(),
user.getProfileImage());
}


public static List<DiaryCommentContent> processData(List<DiaryComment> diaryCommentList, Long userId){
return diaryCommentList.stream()
.map(diaryComment -> {
User user = diaryComment.getUser();
boolean sortMyCommentResult = sortMyComment(user, userId);
return DiaryCommentContent.of(sortMyCommentResult, diaryComment, user );
})
.toList();
}

public static boolean sortMyComment(User user, Long userId){
log.info(String.valueOf(user.getId()));
log.info(String.valueOf(userId));
if(Objects.equals(user.getId(), userId))
return true;
return false;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.modernfarmer.farmusspring.domain.user.entity.User;
import com.modernfarmer.farmusspring.global.common.BaseEntity;
import com.modernfarmer.farmusspring.global.response.BaseResponseDto;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -48,4 +49,5 @@ public static DiaryComment createDiaryComment(String content, Diary diary, User
}



}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.modernfarmer.farmusspring.domain.myveggiegarden.repository;

import com.modernfarmer.farmusspring.domain.farmclub.entity.FarmClub;
import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.Diary;
import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.DiaryComment;
import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.DiaryLike;
import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.MyVeggie;
import com.modernfarmer.farmusspring.domain.user.entity.User;
Expand All @@ -10,6 +12,7 @@
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;


Expand All @@ -24,4 +27,14 @@ public interface DiaryRepository extends JpaRepository<Diary, Long> {
@Query("SELECT dl FROM diary_like AS dl WHERE dl.diary = :diary AND dl.user = :user")
DiaryLike findDiaryLikeByIdAndUser(@Param("user") User user, @Param("diary") Diary diary);


@Query("SELECT dc FROM diary_comment AS dc " +
"JOIN FETCH dc.diary AS d " +
"JOIN FETCH d.myVeggie AS mv " +
"JOIN FETCH mv.user " +
"JOIN FETCH d.farmClub " +
"WHERE d.id = :diaryId AND d.farmClub.id = :farmClubId AND d.isOpen = true")
List<DiaryComment> findDiary(@Param("diaryId") Long diaryId, @Param("farmClubId") Long farmClubId);


}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.modernfarmer.farmusspring.domain.myveggiegarden.service;

import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.AllDairy;
import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.CheckTodayDiaryResponse;
import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.MyVeggieDiaryCount;
import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.SelectDiaryOneResponse;
import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.*;
import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.Diary;
import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.DiaryComment;
import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.DiaryLike;
Expand Down Expand Up @@ -128,6 +125,21 @@ public BaseResponseDto<SelectDiaryOneResponse> selectDiaryOne(
}


@Transactional
public List<DiaryCommentContent> selectComment(
Long userId, Long diaryId, Long farmClubId
) {

List<DiaryComment> diaryCommentList = diaryRepository.findDiary(diaryId, farmClubId);
List<DiaryCommentContent> diaryCommentContent = DiaryCommentContent.processData(diaryCommentList, userId);
log.info(String.valueOf(diaryCommentContent));


return diaryCommentContent;
}



public void insertComment(String content, User user, Diary diary){
DiaryComment diaryComment = DiaryComment.createDiaryComment(content, diary, user);
diary.addDiaryComment(diaryComment);
Expand Down

0 comments on commit 8bdfdf4

Please sign in to comment.