Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] 채소의 일기 조회 시 조회 데이터 추가 (likeCount,commentCount, myLike ) #54

Merged
merged 5 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ public BaseResponseDto<SelectDiaryOneResponse> selectDiaryOne(
}

@GetMapping(value = "/{myVeggieId}/count")
public BaseResponseDto<MyVeggieDiaryCount> selectDiaryCount(
@PathVariable("myVeggieId") Long myVeggieId
) {
public BaseResponseDto<MyVeggieDiaryCount> selectDiaryCount(@PathVariable("myVeggieId") Long myVeggieId) {
Ryeolee marked this conversation as resolved.
Show resolved Hide resolved
MyVeggie myVeggie = MyVeggie.builder().id(myVeggieId).build();
MyVeggieDiaryCount result = myVeggieDiaryService.selectDiaryCount(myVeggie);

Expand All @@ -81,10 +79,11 @@ public BaseResponseDto<MyVeggieDiaryCount> selectDiaryCount(

@GetMapping(value = "/{myVeggieId}/all")
public BaseResponseDto<MyVeggieDiaryCount> selectDiaryAll(
@PathVariable("myVeggieId") Long myVeggieId
@PathVariable("myVeggieId") Long myVeggieId,
@AuthenticationPrincipal CustomUser user
) {
MyVeggie myVeggie = MyVeggie.builder().id(myVeggieId).build();
List<AllDairy> result = myVeggieDiaryService.selectDiaryAll(myVeggie);
List<AllDairy> result = myVeggieDiaryService.selectDiaryAll(myVeggie, user.getUserId());

return BaseResponseDto.of(SuccessCode.SUCCESS, result);
}
Expand Down
Ryeolee marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.modernfarmer.farmusspring.domain.myveggiegarden.dto;

import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.Diary;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;

@NoArgsConstructor
@Getter
public class DiaryAll {

private Diary diary;
private boolean myLike;

public DiaryAll(Diary diary, boolean myLike) {
this.diary = diary;
this.myLike = myLike;
}

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

import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.DiaryAll;
import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.Diary;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -18,20 +19,29 @@ public class AllDairy {
private String content;
private Boolean isOpen;
private String state;
private Long diaryId;
private int likeCount;
private int commentCount;
private boolean myLike;

public static AllDairy of(Diary diary, String date){
public static AllDairy of(DiaryAll diaryAll, String date){
return new AllDairy(
date,
diary.getImage(),
diary.getContent(),
diary.getIsOpen(),
diary.getState()
diaryAll.getDiary().getImage(),
diaryAll.getDiary().getContent(),
diaryAll.getDiary().getIsOpen(),
diaryAll.getDiary().getState(),
diaryAll.getDiary().getId(),
diaryAll.getDiary().getDiaryComments().size(),
diaryAll.getDiary().getDiaryLikes().size(),
diaryAll.isMyLike()

);
}

public static List<AllDairy> processData(List<Diary> diaryList){
return diaryList.stream()
.map(diary -> AllDairy.of(diary, formatDate(diary.getCreatedDate())))
public static List<AllDairy> processData(List<DiaryAll> diaryAllList){
return diaryAllList.stream()
.map(diaryAll -> AllDairy.of(diaryAll, formatDate(diaryAll.getDiary().getCreatedDate())))
.toList();
}

Expand Down
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.dto.DiaryAll;
import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.AllDairy;
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 @@ -46,4 +48,14 @@ public interface DiaryRepository extends JpaRepository<Diary, Long> {
)
List<Diary> findDiaryByFarmClub(@Param("farmClubId") Long farmClubId);

@Query("SELECT new com.modernfarmer.farmusspring.domain.myveggiegarden.dto.DiaryAll(d, " +
"CASE WHEN dl.user.id = :userId THEN true ELSE false END) " +
"FROM diary AS d " +
"JOIN FETCH d.myVeggie AS mv " +
"JOIN FETCH mv.user " +
"LEFT JOIN diary_like AS dl ON dl.diary.id = d.id AND dl.user.id = :userId " +
"WHERE d.myVeggie = :myVeggie " +
"ORDER BY d.createdDate DESC")
List<DiaryAll> findDiariesByMyVeggie(@Param("myVeggie") MyVeggie myVeggie, @Param("userId") Long userId);

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

import com.modernfarmer.farmusspring.domain.farmclub.entity.FarmClub;
import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.DiaryAll;
import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.response.*;
import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.Diary;
import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.DiaryComment;
Expand Down Expand Up @@ -89,20 +90,20 @@ private List<FarmClubDiary> proccessFarmClubData(List<Diary> diaryList){
);}).toList();
}

@Transactional
public List<AllDairy> selectDiaryAll(MyVeggie myVeggie, Long userId) {
List<DiaryAll> diaryList = diaryRepository.findDiariesByMyVeggie(myVeggie, userId);
return AllDairy.processData(diaryList);
}


@Transactional
public MyVeggieDiaryCount selectDiaryCount(MyVeggie myVeggie) {

List<Diary> diaryList = myVeggieRepository.findDiariesByMyVeggie(myVeggie);
return MyVeggieDiaryCount.processData(diaryList);
}

@Transactional
public List<AllDairy> selectDiaryAll(MyVeggie myVeggie) {

List<Diary> diaryList = selectDiaryByMyVeggie(myVeggie);
return AllDairy.processData(diaryList);
}


@Transactional
Expand Down