Skip to content

Commit

Permalink
feat: 가장 최근 식단에서 부족한 영양소 top3 조회 api 구현 완료
Browse files Browse the repository at this point in the history
  • Loading branch information
versatile0010 committed Oct 7, 2023
1 parent 3bf0354 commit 07c7a4b
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.kusitmshackthon.domain.healthlog.standard;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class StandardHealthLog {
// 15세 여아 표준 영양 정보
Float protein = 45F; // 단백질(g)
Float calcium = 800F; // 칼슘(mg)
Float sodium = 1.5F; // 나트륨(mg)
Float fe = 17F; // 철(mg)
Float zinc = 9F; // 아연(mg)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import com.example.kusitmshackthon.domain.member.dto.response.MainPageResponse;
import com.example.kusitmshackthon.domain.member.service.MemberService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
Expand All @@ -18,6 +19,12 @@
public class MemberController {
private final MemberService memberService;

@Operation(summary = "가장 최근에 섭취한 식단에서, 부족한 영양소 top 3 조회", description =
"""
name 은 영양소 이름이고, amount 는 섭취량, diff: 는 권장 섭취량 - amount 입니다!
부족한 순으로 정렬해서 나갑니다..
""")
@GetMapping("/{userId}")
public ResponseEntity<MainPageResponse> getMainPage(@PathVariable(name = "userId") Long userId) {
MainPageResponse response = memberService.getMainPage(userId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.kusitmshackthon.domain.member.dto.response;


import com.example.kusitmshackthon.domain.diet.dto.response.PostDietResponse;
import lombok.*;

import java.util.List;
Expand All @@ -19,8 +20,12 @@ public class MainPageResponse {
@ToString
public static class NutrientInfo {
private String name;
private Integer plus;
private Integer minus;
private Float amount;
private Float diff;

public static MainPageResponse.NutrientInfo of (String name, Float amount, Float diff){
return new MainPageResponse.NutrientInfo(name, amount, diff);
}
}

public static MainPageResponse of(List<NutrientInfo> nutrientInfoList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.example.kusitmshackthon.domain.healthlog.entity.HealthLog;
import com.example.kusitmshackthon.domain.healthlog.repository.HealthLogRepository;
import com.example.kusitmshackthon.domain.healthlog.standard.StandardHealthLog;
import com.example.kusitmshackthon.domain.member.dto.response.MainPageResponse;
import com.example.kusitmshackthon.domain.member.dto.response.MemberAuthResponseDto;
import com.example.kusitmshackthon.domain.member.entity.Member;
Expand All @@ -18,7 +19,9 @@

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.example.kusitmshackthon.domain.healthlog.entity.QHealthLog.healthLog;

Expand All @@ -34,7 +37,6 @@ public class MemberService {
private EntityManager entityManager;

public MainPageResponse getMainPage(Long userId) {
List<MainPageResponse.NutrientInfo> nutrientInfoList = new ArrayList<>();
Member member = memberRepository.findById(userId)
.orElseThrow(MemberNotFoundException::new);
LocalDate nowDate = LocalDate.now();
Expand All @@ -54,8 +56,54 @@ public MainPageResponse getMainPage(Long userId) {
.orderBy(healthLog.intakeAt.desc())
.limit(1)
.fetchOne();
// 해당 회원의 베스트 영양 정보 get 하기
StandardHealthLog standardHealthLog = new StandardHealthLog();

Map<String, Float> diffHashMap = new HashMap<>();
Map<String, Float> orignHashMap = new HashMap<>();

orignHashMap.put("protein", recentlyHealthLog.getProtein());
orignHashMap.put("calcium", recentlyHealthLog.getCalcium());
orignHashMap.put("sodium", recentlyHealthLog.getSodium());
orignHashMap.put("fe", recentlyHealthLog.getFe());
orignHashMap.put("zinc", recentlyHealthLog.getZinc());

if (recentlyHealthLog.getProtein() != 0) {
diffHashMap.put("protein", recentlyHealthLog.getProtein() - standardHealthLog.getProtein());
}
if (recentlyHealthLog.getCalcium() != 0) {
diffHashMap.put("calcium", recentlyHealthLog.getCalcium() - standardHealthLog.getCalcium());
}
if (recentlyHealthLog.getSodium() != 0) {
diffHashMap.put("sodium", recentlyHealthLog.getSodium() - standardHealthLog.getSodium());
}
if (recentlyHealthLog.getFe() != 0) {
diffHashMap.put("fe", recentlyHealthLog.getFe() - standardHealthLog.getFe());
}
if (recentlyHealthLog.getZinc() != 0) {
diffHashMap.put("zinc", recentlyHealthLog.getZinc() - standardHealthLog.getZinc());
}

System.out.println("recentlyHealthLog = " + recentlyHealthLog);

List<Map.Entry<String, Float>> collect = diffHashMap.entrySet().stream()
.sorted(Map.Entry.comparingByValue())
.toList();

List<MainPageResponse.NutrientInfo> nutrientInfoList = new ArrayList<>();
int cnt = 0;
for (Map.Entry<String, Float> entry : collect) {
String key = entry.getKey();
Float diff = entry.getValue();
if (Math.abs(diff) <= 200) {
cnt++;
nutrientInfoList.add(MainPageResponse.NutrientInfo.of(
key, orignHashMap.get(key), diff
));
if (cnt == 3) {
break;
}
}
}

return MainPageResponse.of(nutrientInfoList);
}
Expand Down

0 comments on commit 07c7a4b

Please sign in to comment.