Skip to content

Commit

Permalink
Merge pull request #456 from JNU-econovation/be
Browse files Browse the repository at this point in the history
운영환경 반영
  • Loading branch information
hwangdaesun authored Aug 9, 2024
2 parents cfb6e57 + c5bcc37 commit f48bc37
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public interface MealFoodRepository extends JpaRepository<MealFoodEntity, Long>
@Query("select mf from MealFoodEntity mf where mf.mealEntity.memberEntity = :memberEntity")
List<MealFoodEntity> findByMemberEntity(MemberEntity memberEntity);

@Query("select mft from MealFoodEntity mft join fetch mft.foodEntity where mft.id in :ids")
@Query(
"select mft from MealFoodEntity mft join fetch mft.foodEntity where mft.mealEntity.id in :ids")
List<MealFoodEntity> findMFTByIdInQuery(List<Long> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ public List<HistoryEntity> findByMemberEntity(MemberEntity memberEntity) {
return historyRepository.findByMemberEntity(memberEntity);
}

/**
* 특정 월의 회원 기록을 찾는다. HISTORY_TB에서 특정 월의 기록이 있을 경우, HISTORY_TB에서 특정 월의 기록을 찾는다.(historyEntities).
* 그리고, HISTORY_TB에서 특정 월의 가장 최신 기록(recentHistory)과 비교하여 가장 최신에 있는 기록을 찾는다. 만약 없을 경우 가장 최신 기록인
* MEMBER_TB의 정보를 사용한다. / HISTORY_TB에서 특정 월의 기록이 없을 경우 가장 최신 기록인 MEMBER_TB의 정보를 사용한다.
*
* @param memberId
* @param dateTime
* @return
*/
@Override
@Timer
public Map<LocalDate, Member> findMembersByMonth(Long memberId, LocalDateTime dateTime) {
Expand Down Expand Up @@ -59,11 +68,11 @@ public Map<LocalDate, Member> findMembersByMonth(Long memberId, LocalDateTime da
members.put(
historyEntity.getCreatedDate().toLocalDate(),
memberConverter.toModel(historyEntity)));
HistoryEntity pastHistoryEntity = historyEntities.get(historyEntities.size() - 1);
Optional<HistoryEntity> recentFutureMember =
HistoryEntity recentHistory = historyEntities.get(historyEntities.size() - 1);
Optional<HistoryEntity> recentFutureHistory =
historyRepository.findRecentFutureMember(
memberId, pastHistoryEntity.getCreatedDate());
if (recentFutureMember.isEmpty()) {
memberId, recentHistory.getCreatedDate());
if (recentFutureHistory.isEmpty()) {
MemberEntity memberEntity =
memberRepository
.findById(memberId)
Expand All @@ -72,7 +81,7 @@ public Map<LocalDate, Member> findMembersByMonth(Long memberId, LocalDateTime da
} else {
members.put(
endDateTime.toLocalDate(),
memberConverter.toModel(recentFutureMember.get()));
memberConverter.toModel(recentFutureHistory.get()));
}
}
return members;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.gaebaljip.exceed.application.domain.member.Member;
import com.gaebaljip.exceed.common.annotation.Timer;
import com.gaebaljip.exceed.common.exception.member.MemberNotFoundException;
import com.gaebaljip.exceed.common.exception.nutritionist.MinimumMemberRequiredException;

/**
Expand Down Expand Up @@ -42,16 +43,22 @@ public Map<LocalDate, Boolean> isCalorieAchievementByDate() {
return calorieAchievements;
}

/**
* day와 비교해 가장 가까운 미래를 찾는다. 단, members의 size가 1인 경우는 day가 속한 달보다 미래이다.
*
* @param day
* @param members
* @return
*/
private Member getMemberByDate(LocalDate day, Map<LocalDate, Member> members) {
if (members.size() == 1) {
return members.entrySet().stream().findFirst().map(Map.Entry::getValue).get();
}
return members.entrySet().stream()
.filter(entry -> entry.getKey().isAfter(day) || entry.getKey().equals(day))
.min(Map.Entry.comparingByKey())
.map(Map.Entry::getValue)
.orElse(members.get(getLastDate(day)));
}

private LocalDate getLastDate(LocalDate date) {
return date.withDayOfMonth(date.lengthOfMonth());
.orElseThrow(() -> MemberNotFoundException.EXECPTION);
}

private void validateAtLeastOneMember(Map<LocalDate, Member> members) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.gaebaljip.exceed.application.domain.nutritionist;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.HashMap;
import java.util.Map;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import com.gaebaljip.exceed.application.domain.member.Member;
import com.gaebaljip.exceed.common.factory.MemberFixtureFactory;
import com.gaebaljip.exceed.common.factory.MonthlyMealFixtureFactory;

class MonthlyAnalyzerTest {
@Test
@DisplayName(
"MonthlyAnalyzer의 members 필드가 1개일 경우"
+ "membersMap의 key의 날짜가 now보다 40일전, 즉 무조건 한달 뒤를 보장")
void when_isCalorieAchievementByDate_expected_success() {
LocalDateTime date = LocalDateTime.now();
LocalDateTime startDateTime = date.with(TemporalAdjusters.firstDayOfMonth());
LocalDateTime endOfDateTime = date.with(TemporalAdjusters.lastDayOfMonth());
MonthlyMeal monthlyMeal = MonthlyMealFixtureFactory.create(startDateTime, endOfDateTime);
Member member = MemberFixtureFactory.create(1);
Map<LocalDate, Member> membersMap = new HashMap<>();
membersMap.put(startDateTime.toLocalDate().minusDays(40), member);
MonthlyAnalyzer monthlyAnalyzer = new MonthlyAnalyzer(monthlyMeal, membersMap);
assertDoesNotThrow(() -> monthlyAnalyzer.isCalorieAchievementByDate());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.gaebaljip.exceed.common.factory;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.gaebaljip.exceed.application.domain.meal.DailyMeal;
import com.gaebaljip.exceed.application.domain.meal.Meal;
import com.gaebaljip.exceed.application.domain.nutritionist.MonthlyMeal;

public class MonthlyMealFixtureFactory {
public static MonthlyMeal create(LocalDateTime startDateTime, LocalDateTime endDateTime) {
List<Meal> meals =
MealsFixtureFactory.create(
startDateTime.toLocalDate(), endDateTime.toLocalDate(), 40);
Map<LocalDate, DailyMeal> dailyMealMap =
meals.stream()
.collect(
Collectors.groupingBy(
meal -> meal.getMealDateTime().toLocalDate(),
Collectors.collectingAndThen(
Collectors.toList(), DailyMeal::new)));

List<Meal> emptyMeal = new ArrayList<>();
startDateTime
.toLocalDate()
.datesUntil(endDateTime.toLocalDate())
.forEach(day -> dailyMealMap.putIfAbsent(day, new DailyMeal(emptyMeal)));

MonthlyMeal monthlyMeal = new MonthlyMeal(dailyMealMap);
return monthlyMeal;
}
}

0 comments on commit f48bc37

Please sign in to comment.