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 all commits
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 @@ -30,7 +30,7 @@ public class MyVeggieDiaryController {

private final MyVeggieDiaryService myVeggieDiaryService;

@PostMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public BaseResponseDto<Void> settingMyVeggieDiary(
@RequestPart(value = "image", required = false) MultipartFile multipartFile,
@RequestPart MyVeggieDiaryInsert myVeggieDiaryInsert
Expand All @@ -46,19 +46,15 @@ public BaseResponseDto<Void> settingMyVeggieDiary(


@GetMapping(value = "/{farmClubId}")
public BaseResponseDto<FarmClubDiary> findFarmClubDiary(
@PathVariable("farmClubId") Long farmClubId){

List<FarmClubDiary> farmClubDiaryList = myVeggieDiaryService.findDiaryAccordingToFarmClub(farmClubId);
public BaseResponseDto<FarmClubDiary> findFarmClubDiary(@PathVariable("farmClubId") Long farmClubId, @AuthenticationPrincipal CustomUser user){
List<FarmClubDiary> farmClubDiaryList = myVeggieDiaryService.findDiaryAccordingToFarmClub(farmClubId, user.getUserId());
return BaseResponseDto.of(SuccessCode.SUCCESS, farmClubDiaryList);
}



@GetMapping(value = "/{myVeggieId}/check")
public BaseResponseDto<CheckTodayDiaryResponse> checkTodayDiary(
@PathVariable("myVeggieId") Long myVeggieId
) {
public BaseResponseDto<CheckTodayDiaryResponse> checkTodayDiary(@PathVariable("myVeggieId") Long myVeggieId) {
return myVeggieDiaryService.checkTodayDiary(MyVeggie.builder().id(myVeggieId).build());
}

Expand All @@ -68,21 +64,19 @@ public BaseResponseDto<SelectDiaryOneResponse> selectDiaryOne(@PathVariable("myV
}

@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);

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

@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
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.modernfarmer.farmusspring.domain.myveggiegarden.dto;

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

@NoArgsConstructor
@Getter
public class SortedMyLikeDiary {

private Diary diary;
private boolean myLike;

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

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

import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.Diary;
import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.SortedMyLikeDiary;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -18,20 +18,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(SortedMyLikeDiary 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<SortedMyLikeDiary> 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
Expand Up @@ -20,8 +20,10 @@ public class FarmClubDiary {
private int commentCount;
private String nickname;
private String profileImage;
private boolean myLike;
private String state;

public static FarmClubDiary of(Diary diary, User user, String writeDateTime, int likeCount, int commentCount){
public static FarmClubDiary of(Diary diary, User user, String writeDateTime, int likeCount, int commentCount, boolean myLike, String status){
return new FarmClubDiary(
diary.getId(),
diary.getImage(),
Expand All @@ -30,7 +32,9 @@ public static FarmClubDiary of(Diary diary, User user, String writeDateTime, int
likeCount,
commentCount,
user.getNickname(),
user.getProfileImage()
user.getProfileImage(),
myLike,
status
);
}

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

import com.modernfarmer.farmusspring.domain.farmclub.entity.FarmClub;
import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.SortedMyLikeDiary;
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 All @@ -13,7 +13,6 @@
import org.springframework.stereotype.Repository;

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


@Repository
Expand All @@ -36,14 +35,26 @@ public interface DiaryRepository extends JpaRepository<Diary, Long> {
List<DiaryComment> findDiaryById(@Param("diaryId") Long diaryId);


@Query("SELECT d FROM diary AS d " +
@Query("SELECT new com.modernfarmer.farmusspring.domain.myveggiegarden.dto.SortedMyLikeDiary(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_comment AS dc ON dc.id = d.id "+
"LEFT JOIN diary_like AS dl ON dl.id = d.id " +
"WHERE d.farmClub.id = :farmClubId AND d.isOpen = true "
)
List<Diary> findDiaryByFarmClub(@Param("farmClubId") Long farmClubId);
List<SortedMyLikeDiary> findDiaryByFarmClub(@Param("farmClubId") Long farmClubId, @Param("userId") Long userId);

@Query("SELECT new com.modernfarmer.farmusspring.domain.myveggiegarden.dto.SortedMyLikeDiary(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<SortedMyLikeDiary> findDiariesByMyVeggie(@Param("myVeggie") MyVeggie myVeggie, @Param("userId") Long userId);

@Query("SELECT d FROM diary AS d WHERE d.myVeggie = :myVeggie AND FUNCTION('DATE', d.createdDate) = CURRENT_DATE")
List<Diary> findDiaryByToday(@Param("myVeggie") MyVeggie myVeggie);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.modernfarmer.farmusspring.domain.myveggiegarden.service;

import com.modernfarmer.farmusspring.domain.farmclub.entity.FarmClub;
import com.modernfarmer.farmusspring.domain.myveggiegarden.dto.SortedMyLikeDiary;
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 All @@ -21,7 +21,6 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Optional;

Expand All @@ -45,7 +44,6 @@ public BaseResponseDto<Void> settingMyVeggieDiary(
String state,
Long myVeggieId
) throws IOException {

String imageUrl = getImageUrl(multipartFile);
addMyyVeggieDiary(
content,
Expand All @@ -59,10 +57,7 @@ public BaseResponseDto<Void> settingMyVeggieDiary(


@Transactional
public BaseResponseDto<CheckTodayDiaryResponse> checkTodayDiary(
MyVeggie myVeggie
) {

public BaseResponseDto<CheckTodayDiaryResponse> checkTodayDiary(MyVeggie myVeggie) {
Diary diary = selectTodayDiary(myVeggie);
boolean state = verifyDiaryState(diary);
return BaseResponseDto.of(SuccessCode.SUCCESS,CheckTodayDiaryResponse.of(state));
Expand All @@ -71,40 +66,38 @@ public BaseResponseDto<CheckTodayDiaryResponse> checkTodayDiary(


@Transactional
public List<FarmClubDiary> findDiaryAccordingToFarmClub(Long farmClubId) {
List<Diary> diaryList = diaryRepository.findDiaryByFarmClub(farmClubId);
public List<FarmClubDiary> findDiaryAccordingToFarmClub(Long farmClubId, Long userId) {
List<SortedMyLikeDiary> diaryList = diaryRepository.findDiaryByFarmClub(farmClubId, userId);
List<FarmClubDiary> proccessData = proccessFarmClubData(diaryList);
return proccessData;
}

private List<FarmClubDiary> proccessFarmClubData(List<Diary> diaryList){
return diaryList.stream().map(diary -> {
User user = diary.getMyVeggie().getUser();
private List<FarmClubDiary> proccessFarmClubData(List<SortedMyLikeDiary> diaryAllList){
return diaryAllList.stream().map(allDiary -> {
User user = allDiary.getDiary().getMyVeggie().getUser();
return FarmClubDiary.of(
diary,
allDiary.getDiary(),
user,
DateManager.dotDateTime(diary.getCreatedDate()),
diary.getDiaryComments().size(),
diary.getDiaryLikes().size()
DateManager.dotDateTime(allDiary.getDiary().getCreatedDate()),
allDiary.getDiary().getDiaryComments().size(),
allDiary.getDiary().getDiaryLikes().size(),
allDiary.isMyLike(),
allDiary.getDiary().getState()
);}).toList();
}

@Transactional
public List<AllDairy> selectDiaryAll(MyVeggie myVeggie, Long userId) {
List<SortedMyLikeDiary> 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
public void pressLike(Long userId, Long diaryId) {
User userData = userService.selectUserById(userId);
Expand Down Expand Up @@ -179,14 +172,10 @@ public void deleteLike(User user, Diary diary){
}

public boolean verifyDiaryState(Diary diary){

if(diary == null){
return true;
}
if(diary == null){return true;}
return false;
}


public Diary selectTodayDiary(MyVeggie myVeggie){
return myVeggieRepository.findDiariesByMyVeggieAndToday(myVeggie);
}
Expand Down Expand Up @@ -218,7 +207,6 @@ public void validateDiaryComment(Optional<DiaryComment> diaryComment){
}
}


private void addMyyVeggieDiary(
String content,
boolean isOpen,
Expand Down
Loading