-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[Be/Feat] 분석 API 쿼리 개선
- Loading branch information
Showing
6 changed files
with
131 additions
and
5 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
BE/exceed/src/main/java/com/gaebaljip/exceed/adapter/out/jpa/meal/MealFoodConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.gaebaljip.exceed.adapter.out.jpa.meal; | ||
|
||
import static java.util.stream.Collectors.groupingBy; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import com.gaebaljip.exceed.application.domain.meal.Meal; | ||
import com.gaebaljip.exceed.application.domain.meal.MealEntity; | ||
import com.gaebaljip.exceed.application.domain.meal.MealFoodEntity; | ||
import com.gaebaljip.exceed.common.annotation.Timer; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class MealFoodConverter { | ||
|
||
private final ConsumedFoodConverter converter; | ||
|
||
@Timer | ||
public List<Meal> toMeals(List<MealFoodEntity> mealFoodEntities) { | ||
Map<MealEntity, List<MealFoodEntity>> mealEntityListMap = | ||
mealFoodEntities.stream().collect(groupingBy(MealFoodEntity::getMealEntity)); | ||
return mealEntityListMap.keySet().stream() | ||
.map(mealEntity -> toMeal(mealEntityListMap.get(mealEntity), mealEntity.getId())) | ||
.toList(); | ||
} | ||
|
||
private Meal toMeal(List<MealFoodEntity> mealFoodEntities, Long mealId) { | ||
return Meal.builder() | ||
.id(mealId) | ||
.mealDateTime(mealFoodEntities.get(0).getCreatedDate()) | ||
.consumedFoods(converter.toConsumedFoods(mealFoodEntities)) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...d/src/test/java/com/gaebaljip/exceed/adapter/out/jpa/meal/MealPersistenceAdapterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.gaebaljip.exceed.adapter.out.jpa.meal; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import com.gaebaljip.exceed.application.domain.meal.Meal; | ||
import com.gaebaljip.exceed.application.domain.nutritionist.MonthlyMeal; | ||
import com.gaebaljip.exceed.common.DatabaseTest; | ||
import com.gaebaljip.exceed.common.dto.DailyMealDTO; | ||
import com.gaebaljip.exceed.common.dto.MonthlyMealDTO; | ||
|
||
class MealPersistenceAdapterTest extends DatabaseTest { | ||
|
||
@Autowired private MealPersistenceAdapter mealPersistenceAdapter; | ||
|
||
@Test | ||
void when_queryForDaily_expected_success() { | ||
LocalDateTime dateTime = LocalDateTime.of(2024, 6, 10, 11, 11); | ||
List<Meal> meals = mealPersistenceAdapter.query(new DailyMealDTO(1L, dateTime)); | ||
assertAll( | ||
() -> assertEquals(2, meals.size()), | ||
() -> assertEquals(1, meals.get(0).getConsumedFoods().size()), | ||
() -> assertEquals(1, meals.get(1).getConsumedFoods().size())); | ||
} | ||
|
||
@Test | ||
void when_queryForMonth_expected_success() { | ||
LocalDateTime dateTime = LocalDateTime.of(2024, 6, 20, 23, 22); | ||
LocalDateTime plusDateTime = dateTime.plusDays(1); | ||
MonthlyMealDTO monthlyMealDTO = new MonthlyMealDTO(1L, dateTime); | ||
MonthlyMeal monthlyMeal = mealPersistenceAdapter.query(monthlyMealDTO); | ||
|
||
assertAll( | ||
() -> assertEquals(30, monthlyMeal.getMonthlyMeal().size()), | ||
() -> | ||
assertEquals( | ||
3, | ||
monthlyMeal.getMonthlyMeal().values().stream() | ||
.filter(dailyMeal -> dailyMeal.isEmptyMeals()) | ||
.toList() | ||
.size()), | ||
() -> | ||
assertEquals( | ||
2, | ||
monthlyMeal | ||
.getMonthlyMeal() | ||
.get(dateTime.toLocalDate()) | ||
.getMeals() | ||
.size()), | ||
() -> | ||
assertEquals( | ||
2, | ||
monthlyMeal | ||
.getMonthlyMeal() | ||
.get(plusDateTime.toLocalDate()) | ||
.getMeals() | ||
.size())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters