diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/GetMyFarmClubResponseDto.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/GetMyFarmClubResponseDto.java index cca52a4f..c00dd246 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/GetMyFarmClubResponseDto.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/dto/res/GetMyFarmClubResponseDto.java @@ -1,6 +1,6 @@ package com.modernfarmer.farmusspring.domain.farmclub.dto.res; -import com.modernfarmer.farmusspring.domain.farmclub.vo.GetMissionPostListWithStepCountsAndImagesVo; +import com.modernfarmer.farmusspring.domain.farmclub.vo.GetMissionPostListVo; import com.modernfarmer.farmusspring.domain.farmclub.vo.GetMyFarmClubVo; import com.modernfarmer.farmusspring.domain.veggieinfo.vo.StepVo; import lombok.Builder; @@ -31,26 +31,24 @@ public static GetMyFarmClubResponseDto of(GetMyFarmClubVo farmClubInfo, List createSteps(List stepVoList, List missionList) { + public static List createSteps(List stepVoList, List missionList) { List steps = new ArrayList<>(); - for (int i = 0; i < stepVoList.size(); i++) { - Step step; - if (i >= missionList.size()) { - step = new Step( - new ArrayList<>(), - stepVoList.get(i).num(), - stepVoList.get(i).content(), - 0L - ); - } else { - step = new Step( - missionList.get(i).images(), - stepVoList.get(i).num(), - stepVoList.get(i).content(), - missionList.get(i).count() - ); + for (StepVo step: stepVoList) { + long count = 0L; + List images = new ArrayList<>(); + for (GetMissionPostListVo mission: missionList) { + if (step.num() + 1 == mission.stepNum()) { + images.add(mission.image()); + count++; + } } - steps.add(step); + + steps.add(new Step( + images, + step.num(), + step.content(), + count + )); } return steps; } diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/MissionPostRepositoryCustom.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/MissionPostRepositoryCustom.java index 460cc090..5883586e 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/MissionPostRepositoryCustom.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/MissionPostRepositoryCustom.java @@ -1,7 +1,7 @@ package com.modernfarmer.farmusspring.domain.farmclub.repository; -import com.modernfarmer.farmusspring.domain.farmclub.vo.GetMissionPostListWithStepCountsAndImagesVo; +import com.modernfarmer.farmusspring.domain.farmclub.vo.GetMissionPostListVo; import com.modernfarmer.farmusspring.domain.farmclub.vo.MissionPostCommentVo; import com.modernfarmer.farmusspring.domain.farmclub.vo.MissionPostVo; import com.modernfarmer.farmusspring.domain.history.vo.MissionPostHistoryVo; @@ -10,7 +10,7 @@ public interface MissionPostRepositoryCustom { - List getMissionPostStepNumAndImage(Long farmClubId); + List getMissionPostStepNumAndImage(Long farmClubId); List getMissionPostComment(Long missionPostId); List getMissionPostList(Long missionPostId); List getMissionPostHistory(Long missionPostId); diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/MissionPostRepositoryImpl.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/MissionPostRepositoryImpl.java index ca5829e1..23620752 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/MissionPostRepositoryImpl.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/repository/MissionPostRepositoryImpl.java @@ -4,11 +4,9 @@ import com.modernfarmer.farmusspring.domain.farmclub.vo.*; import com.modernfarmer.farmusspring.domain.history.vo.MissionPostHistoryVo; import com.modernfarmer.farmusspring.domain.history.vo.QMissionPostHistoryVo; -import com.querydsl.core.group.GroupBy; +import com.querydsl.core.types.Projections; import com.querydsl.jpa.JPAExpressions; import com.querydsl.jpa.impl.JPAQueryFactory; -import jakarta.persistence.EntityManager; -import jakarta.persistence.PersistenceContext; import lombok.RequiredArgsConstructor; import java.util.List; @@ -27,20 +25,18 @@ public class MissionPostRepositoryImpl implements MissionPostRepositoryCustom { private final JPAQueryFactory queryFactory; @Override - public List getMissionPostStepNumAndImage(Long farmClubId) { + public List getMissionPostStepNumAndImage(Long farmClubId) { return queryFactory + .select(Projections.constructor( + GetMissionPostListVo.class, + missionPost.stepNum, + missionPost.image)) .from(missionPost) .join(missionPost.userFarmClub, userFarmClub) .join(userFarmClub.farmClub, farmClub) .where(farmClub.id.eq(farmClubId)) - .transform( - GroupBy.groupBy(missionPost.stepNum) - .list(new QGetMissionPostListWithStepCountsAndImagesVo( - missionPost.stepNum.count(), - GroupBy.list(missionPost.image) - )) - ); + .fetch(); } @Override diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/service/FarmClubService.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/service/FarmClubService.java index 6d2246aa..b5604112 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/service/FarmClubService.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/service/FarmClubService.java @@ -8,7 +8,7 @@ import com.modernfarmer.farmusspring.domain.farmclub.repository.FarmClubRepository; import com.modernfarmer.farmusspring.domain.farmclub.repository.MissionPostRepository; import com.modernfarmer.farmusspring.domain.farmclub.repository.UserFarmClubRepository; -import com.modernfarmer.farmusspring.domain.farmclub.vo.GetMissionPostListWithStepCountsAndImagesVo; +import com.modernfarmer.farmusspring.domain.farmclub.vo.GetMissionPostListVo; import com.modernfarmer.farmusspring.domain.farmclub.vo.GetMyFarmClubVo; import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.MyVeggie; import com.modernfarmer.farmusspring.domain.myveggiegarden.helper.MyVeggieHelper; @@ -80,7 +80,7 @@ public RegisterFarmClubResponseDto registerFarmClub(Long farmClubId, Long myVegg MyVeggie myVeggie = myVeggieHelper.getMyVeggieEntity(myVeggieId); FarmClub farmClub = farmClubHelper.getFarmClubEntity(farmClubId); // 채소정보 id로 채소의 첫 스텝명 불러옴 - String stepName = veggieInfoHelper.getStepName(farmClub.getVeggieInfoId(), 0); + String stepName = veggieInfoHelper.getStepName(farmClub.getVeggieInfoId(), 1); UserFarmClub userFarmClub = createUserFarmClubEntity(userId, stepName, farmClub, myVeggie); farmClub.addUserFarmClub(userFarmClub); myVeggie.setUserFarmClub(userFarmClub); @@ -96,9 +96,11 @@ public GetMyFarmClubResponseDto getMyFarmClub(Long farmClubId, Long userId) { String veggieInfoId = farmClubInfo.veggieInfoId(); List stepList = veggieInfoHelper.getStepList(veggieInfoId); String randomTip = getRandomTip(stepList); - List missionList = + List missionList = missionPostRepository.getMissionPostStepNumAndImage(farmClubId); + log.info("missionList: {}", missionList); + return GetMyFarmClubResponseDto.of(farmClubInfo, GetMyFarmClubResponseDto.createSteps(stepList, missionList), randomTip); } diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/vo/GetMissionPostListVo.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/vo/GetMissionPostListVo.java new file mode 100644 index 00000000..fb81a2a6 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/vo/GetMissionPostListVo.java @@ -0,0 +1,12 @@ +package com.modernfarmer.farmusspring.domain.farmclub.vo; + +import com.querydsl.core.annotations.QueryProjection; + +public record GetMissionPostListVo( + Long stepNum, + String image +) { + @QueryProjection + public GetMissionPostListVo { + } +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/vo/GetMissionPostListWithStepCountsAndImagesVo.java b/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/vo/GetMissionPostListWithStepCountsAndImagesVo.java deleted file mode 100644 index ecac9988..00000000 --- a/src/main/java/com/modernfarmer/farmusspring/domain/farmclub/vo/GetMissionPostListWithStepCountsAndImagesVo.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.modernfarmer.farmusspring.domain.farmclub.vo; - -import com.querydsl.core.annotations.QueryProjection; - -import java.util.List; - -public record GetMissionPostListWithStepCountsAndImagesVo( - Long count, - List images -) { - @QueryProjection - public GetMissionPostListWithStepCountsAndImagesVo { - } -}