Skip to content

Commit

Permalink
Merge pull request #75 from CAUSOLDOUTMEN/fix/74-addedtime
Browse files Browse the repository at this point in the history
Fix: 테이블의 칼럼 명과 직접 매핑
  • Loading branch information
win-luck authored Nov 12, 2023
2 parents 0c60013 + 25d7bdd commit 3159469
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/diareat/diareat/food/domain/Food.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class Food {

private LocalDate date;

@Column(name = "added_time") //테이블과 매핑
private LocalDateTime addedTime; //클라이언트에서 추가하도록 요청 보낸 timestamp

private BaseNutrition baseNutrition;
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/diareat/diareat/food/service/FoodService.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public Long saveFood(CreateFoodDto createFoodDto) {
@Transactional(readOnly = true)
public List<ResponseFoodDto> getFoodListByDate(Long userId, LocalDate date){
validateUser(userId);
Sort sort = Sort.by(Sort.Direction.DESC, "addedTime");
Sort sort = Sort.by(Sort.Direction.DESC, "added_time");
List<Food> foodList = foodRepository.findAllByUserIdAndDate(userId, date, sort);
return foodList.stream()
.map(food -> ResponseFoodDto.of(food.getId(), food.getUser().getId(), food.getName(), food.getBaseNutrition(), food.isFavorite())).collect(Collectors.toList());
Expand Down Expand Up @@ -119,7 +119,7 @@ public void deleteFavoriteFood(Long favoriteFoodId, Long userId) {
// 유저의 특정 날짜에 먹은 음식들의 영양성분별 총합 조회 (섭취영양소/기준영양소 및 비율까지 계산해서 반환, dto 구체적 협의 필요)
public ResponseNutritionSumByDateDto getNutritionSumByDate(Long userId, LocalDate date) {
validateUser(userId);
Sort sort = Sort.by(Sort.Direction.DESC, "addedTime");
Sort sort = Sort.by(Sort.Direction.DESC, "added_time");
List<Food> foodList = foodRepository.findAllByUserIdAndDate(userId, date, sort);
return calculateNutritionSumAndRatio(userId, foodList, date, 1);
}
Expand All @@ -129,7 +129,7 @@ public ResponseNutritionSumByDateDto getNutritionSumByDate(Long userId, LocalDat
public ResponseNutritionSumByDateDto getNutritionSumByWeek(Long userId) {
validateUser(userId);
LocalDate endDate = LocalDate.now();
Sort sort = Sort.by(Sort.Direction.DESC, "addedTime");
Sort sort = Sort.by(Sort.Direction.DESC, "added_time");
List<Food> foodList = foodRepository.findAllByUserIdAndDateBetween(userId, endDate.minusWeeks(1), endDate, sort);

return calculateNutritionSumAndRatio(userId, foodList, endDate, 7);
Expand All @@ -140,7 +140,7 @@ public ResponseNutritionSumByDateDto getNutritionSumByWeek(Long userId) {
public ResponseNutritionSumByDateDto getNutritionSumByMonth(Long userId) {
validateUser(userId);
LocalDate endDate = LocalDate.now();
Sort sort = Sort.by(Sort.Direction.DESC, "addedTime");
Sort sort = Sort.by(Sort.Direction.DESC, "added_time");
List<Food> foodList = foodRepository.findAllByUserIdAndDateBetween(userId, endDate.minusMonths(1), endDate, sort);

return calculateNutritionSumAndRatio(userId, foodList, endDate, 30);
Expand All @@ -151,7 +151,7 @@ public ResponseNutritionSumByDateDto getNutritionSumByMonth(Long userId) {
public ResponseFoodRankDto getBestFoodByWeek(Long userId) {
validateUser(userId);
LocalDate endDate = LocalDate.now();
Sort sort = Sort.by(Sort.Direction.DESC, "addedTime");
Sort sort = Sort.by(Sort.Direction.DESC, "added_time");
List<Food> foodList = foodRepository.findAllByUserIdAndDateBetween(userId, endDate.minusWeeks(1), endDate, sort);

List<Food> top3Foods = foodList.stream()
Expand All @@ -174,7 +174,7 @@ public ResponseFoodRankDto getBestFoodByWeek(Long userId) {
public ResponseFoodRankDto getWorstFoodByWeek(Long userId) {
validateUser(userId);
LocalDate endDate = LocalDate.now();
Sort sort = Sort.by(Sort.Direction.DESC, "addedTime");
Sort sort = Sort.by(Sort.Direction.DESC, "added_time");
List<Food> foodList = foodRepository.findAllByUserIdAndDateBetween(userId, endDate.minusWeeks(1), endDate, sort);

List<Food> worst3Foods = foodList.stream()
Expand Down Expand Up @@ -402,7 +402,7 @@ private void validateFavoriteFood(Long favoriteFoodId, Long userId) {
// 1주일동안 먹은 음식들의 영양성분 총합을 요일을 Key로 한 Map을 통해 반환
private HashMap<LocalDate, List<BaseNutrition>> getNutritionSumByDateMap(Long userId, LocalDate startDate, LocalDate endDate) {
HashMap<LocalDate, List<BaseNutrition>> maps = new HashMap<>();
Sort sort = Sort.by(Sort.Direction.DESC, "addedTime");
Sort sort = Sort.by(Sort.Direction.DESC, "added_time");
List<Food> foodList = foodRepository.findAllByUserIdAndDateBetween(userId, startDate, endDate, sort);
for (Food food : foodList) {
if (maps.containsKey(food.getDate())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ void testGetAnalysisOfUser(){

List<Food> foodListOfWeek = List.of(food1,food1_1, food2, food3);
List<Food> foodListOfMonth = List.of(food1, food1_1,food2, food3, food4, food5);
Sort sort = Sort.by(Sort.Direction.DESC, "addedTime");
Sort sort = Sort.by(Sort.Direction.DESC, "added_time");



Expand Down

0 comments on commit 3159469

Please sign in to comment.