diff --git a/moodoodle-api/src/main/java/zzangdol/report/business/ReportMapper.java b/moodoodle-api/src/main/java/zzangdol/report/business/ReportMapper.java index b236f05..4f1718a 100644 --- a/moodoodle-api/src/main/java/zzangdol/report/business/ReportMapper.java +++ b/moodoodle-api/src/main/java/zzangdol/report/business/ReportMapper.java @@ -1,10 +1,10 @@ package zzangdol.report.business; +import java.time.DayOfWeek; import java.time.LocalDate; import java.time.temporal.WeekFields; import java.util.HashMap; import java.util.List; -import java.util.Locale; import java.util.Map; import java.util.stream.Collectors; import zzangdol.report.domain.Asset; @@ -48,14 +48,21 @@ private static List toReportEmotionResponseList(List optionalAsset = - (negativePercentage > positivePercentage) ? assetQueryRepository.findRandomAsset() : Optional.empty(); - Report report = buildReport(user, positivePercentage, negativePercentage, optionalAsset.orElse(null)); + Asset asset; + if (positivePercentage > negativePercentage) { + asset = assetRepository.findById(Constants.POSITIVE_ASSET_ID) + .orElse(null); + } else { + asset = assetRepository.findRandomAssetExcludingId(Constants.POSITIVE_ASSET_ID) + .orElse(null); + } + + Report report = buildReport(user, asset, positivePercentage, negativePercentage, lastMonday, thisSunday); emotionCounts.forEach( (emotion, count) -> report.addReportEmotion(buildReportEmotion(report, totalEmotions, emotion))); @@ -118,9 +125,16 @@ public Report createReportByDate(User user, LocalDate startDate, LocalDate endDa int positivePercentage = (int) Math.round((double) positiveCount.get() / totalEmotions * 100); int negativePercentage = (int) Math.round((double) negativeCount.get() / totalEmotions * 100); - Optional optionalAsset = - (negativePercentage > positivePercentage) ? assetQueryRepository.findRandomAsset() : Optional.empty(); - Report report = buildReport(user, positivePercentage, negativePercentage, optionalAsset.orElse(null)); + Asset asset; + if (positivePercentage > negativePercentage) { + asset = assetRepository.findById(Constants.POSITIVE_ASSET_ID) + .orElse(null); + } else { + asset = assetRepository.findRandomAssetExcludingId(Constants.POSITIVE_ASSET_ID) + .orElse(null); + } + + Report report = buildReport(user, asset, positivePercentage, negativePercentage, startDate, endDate); emotionCounts.entrySet().stream() .sorted(Map.Entry.comparingByValue().reversed()) @@ -154,12 +168,15 @@ private ReportEmotion buildReportEmotion(Report report, int percentage, Emotion .build(); } - private Report buildReport(User user, double positivePercentage, double negativePercentage, Asset asset) { + private Report buildReport(User user, Asset asset, double positivePercentage, double negativePercentage, + LocalDate startDate, LocalDate endDate) { return Report.builder() .user(user) + .asset(asset) .positivePercentage(positivePercentage) .negativePercentage(negativePercentage) - .asset(asset) + .startDate(startDate) + .endDate(endDate) .build(); } } diff --git a/moodoodle-common/src/main/java/zzangdol/constant/Constants.java b/moodoodle-common/src/main/java/zzangdol/constant/Constants.java index aa92ba2..58794d1 100644 --- a/moodoodle-common/src/main/java/zzangdol/constant/Constants.java +++ b/moodoodle-common/src/main/java/zzangdol/constant/Constants.java @@ -4,5 +4,6 @@ public class Constants { public static final String DEFAULT_IMAGE_URL = "https://moodoodle-dev.s3.ap-northeast-2.amazonaws.com/default_category_image.png"; public static final String DEFAULT_CATEGORY_NAME = "모든 스크랩"; + public static final Long POSITIVE_ASSET_ID = 6L; } \ No newline at end of file diff --git a/moodoodle-domain/src/main/java/zzangdol/report/dao/AssetRepository.java b/moodoodle-domain/src/main/java/zzangdol/report/dao/AssetRepository.java index 9685ce6..0bf2e34 100644 --- a/moodoodle-domain/src/main/java/zzangdol/report/dao/AssetRepository.java +++ b/moodoodle-domain/src/main/java/zzangdol/report/dao/AssetRepository.java @@ -1,7 +1,8 @@ package zzangdol.report.dao; import org.springframework.data.jpa.repository.JpaRepository; +import zzangdol.report.dao.querydsl.AssetQueryRepository; import zzangdol.report.domain.Asset; -public interface AssetRepository extends JpaRepository { +public interface AssetRepository extends JpaRepository, AssetQueryRepository { } diff --git a/moodoodle-domain/src/main/java/zzangdol/report/dao/querydsl/AssetQueryRepository.java b/moodoodle-domain/src/main/java/zzangdol/report/dao/querydsl/AssetQueryRepository.java index b85f0a5..6ba22bf 100644 --- a/moodoodle-domain/src/main/java/zzangdol/report/dao/querydsl/AssetQueryRepository.java +++ b/moodoodle-domain/src/main/java/zzangdol/report/dao/querydsl/AssetQueryRepository.java @@ -5,6 +5,6 @@ public interface AssetQueryRepository { - Optional findRandomAsset(); + Optional findRandomAssetExcludingId(Long excludedId); } diff --git a/moodoodle-domain/src/main/java/zzangdol/report/dao/querydsl/AssetQueryRepositoryImpl.java b/moodoodle-domain/src/main/java/zzangdol/report/dao/querydsl/AssetQueryRepositoryImpl.java index 9757511..866362f 100644 --- a/moodoodle-domain/src/main/java/zzangdol/report/dao/querydsl/AssetQueryRepositoryImpl.java +++ b/moodoodle-domain/src/main/java/zzangdol/report/dao/querydsl/AssetQueryRepositoryImpl.java @@ -3,6 +3,7 @@ import static zzangdol.report.domain.QAsset.asset; import com.querydsl.jpa.impl.JPAQueryFactory; +import java.util.List; import java.util.Optional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Repository; @@ -16,16 +17,14 @@ public class AssetQueryRepositoryImpl implements AssetQueryRepository { private final JPAQueryFactory queryFactory; @Override - public Optional findRandomAsset() { - int count = queryFactory.selectFrom(asset).fetch().size(); - - if (count > 0) { - int index = (int) (Math.random() * count); - Asset selectedAsset = queryFactory.selectFrom(asset) - .offset(index) - .limit(1) - .fetchOne(); - return Optional.ofNullable(selectedAsset); + public Optional findRandomAssetExcludingId(Long excludedId) { + List assets = queryFactory.selectFrom(asset) + .where(asset.id.ne(excludedId)) + .fetch(); + + if (assets.size() > 0) { + int index = (int) (Math.random() * assets.size()); + return Optional.of(assets.get(index)); } return Optional.empty(); diff --git a/moodoodle-domain/src/main/java/zzangdol/report/domain/Report.java b/moodoodle-domain/src/main/java/zzangdol/report/domain/Report.java index cd2f836..e9a6ce0 100644 --- a/moodoodle-domain/src/main/java/zzangdol/report/domain/Report.java +++ b/moodoodle-domain/src/main/java/zzangdol/report/domain/Report.java @@ -8,6 +8,7 @@ import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToMany; +import java.time.LocalDate; import java.util.ArrayList; import java.util.List; import lombok.AccessLevel; @@ -40,12 +41,19 @@ public class Report extends BaseTimeEntity { private double positivePercentage; private double negativePercentage; + private LocalDate startDate; + private LocalDate endDate; + @Builder - public Report(User user, double positivePercentage, double negativePercentage, Asset asset) { + public Report(User user, Asset asset, + double positivePercentage, double negativePercentage, + LocalDate startDate, LocalDate endDate) { this.user = user; + this.asset = asset; this.positivePercentage = positivePercentage; this.negativePercentage = negativePercentage; - this.asset = asset; + this.startDate = startDate; + this.endDate = endDate; } public void addReportEmotion(ReportEmotion reportEmotion) {