From cf1ce93556258812ee22b21ce21e9c0b69ce4a34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=95=88=EC=A0=95=ED=9B=84?= Date: Mon, 7 Oct 2024 16:56:31 +0900 Subject: [PATCH 1/7] =?UTF-8?q?feat:=20ingredient=20=EC=A0=95=EB=B3=B4=20r?= =?UTF-8?q?eport=20=EC=A0=80=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/IngredientFindUseCase.java | 12 +++++ .../presentation/IngredientController.java | 45 ++++++++++++------- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/api-module/src/main/java/com/foodgo/apimodule/ingredient/application/IngredientFindUseCase.java b/api-module/src/main/java/com/foodgo/apimodule/ingredient/application/IngredientFindUseCase.java index feebb61..fdae240 100644 --- a/api-module/src/main/java/com/foodgo/apimodule/ingredient/application/IngredientFindUseCase.java +++ b/api-module/src/main/java/com/foodgo/apimodule/ingredient/application/IngredientFindUseCase.java @@ -1,7 +1,10 @@ package com.foodgo.apimodule.ingredient.application; +import com.foodgo.apimodule.report.mapper.ReportMapper; import com.foodgo.coremodule.ingredient.dto.request.IngredientGetRequest; import com.foodgo.coremodule.ingredient.dto.response.IngredientGetResponse; +import com.foodgo.coremodule.report.domain.Report; +import com.foodgo.coremodule.report.service.ReportQueryService; import com.foodgo.coremodule.user.domain.User; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Value; @@ -19,6 +22,8 @@ public class IngredientFindUseCase { @Value("${spring.openapi.key.ingredient}") private String apiKey; + private final ReportQueryService reportQueryService; + public IngredientGetResponse.Row getIngredient(User user, IngredientGetRequest request) throws URISyntaxException { String apiUrl = "http://openapi.foodsafetykorea.go.kr/api/" + apiKey + "/I2790/json/1/100/DESC_KOR=" + request.descKor(); @@ -39,6 +44,13 @@ public IngredientGetResponse.Row getIngredient(User user, IngredientGetRequest r .filter(row -> groupNameMatches(row, request.groupName())) .toList(); + // 리포트 저장 후 결과 반환 + if (!filteredRows.isEmpty()) { + IngredientGetResponse.Row selectedRow = filteredRows.get(0); + Report report = ReportMapper.mapToReport(user, selectedRow); + reportQueryService.saveReport(report); // 리포트 저장 + } + return filteredRows.isEmpty() ? null : filteredRows.get(0); } diff --git a/api-module/src/main/java/com/foodgo/apimodule/ingredient/presentation/IngredientController.java b/api-module/src/main/java/com/foodgo/apimodule/ingredient/presentation/IngredientController.java index e1ce4a1..3895a19 100644 --- a/api-module/src/main/java/com/foodgo/apimodule/ingredient/presentation/IngredientController.java +++ b/api-module/src/main/java/com/foodgo/apimodule/ingredient/presentation/IngredientController.java @@ -1,36 +1,49 @@ package com.foodgo.apimodule.ingredient.presentation; -import java.net.URISyntaxException; - import com.foodgo.apimodule.ingredient.application.IngredientFindUseCase; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - import com.foodgo.commonmodule.common.ApplicationResponse; import com.foodgo.coremodule.ingredient.dto.request.IngredientGetRequest; import com.foodgo.coremodule.ingredient.dto.response.IngredientGetResponse; import com.foodgo.coremodule.security.annotation.UserResolver; import com.foodgo.coremodule.user.domain.User; - +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.net.URISyntaxException; @Slf4j @RequiredArgsConstructor @RestController @RequestMapping("/api/v1/ingredients") +@Tag(name = "ingredinet", description = "식재료 관련 API") public class IngredientController { - private final IngredientFindUseCase ingredientFindUseCase; + private final IngredientFindUseCase ingredientFindUseCase; - @PostMapping("") - public ApplicationResponse getIngredientInfo( - @UserResolver User user, - @RequestBody @Valid IngredientGetRequest request - ) throws URISyntaxException { - return ApplicationResponse.onSuccess(ingredientFindUseCase.getIngredient(user, request)); - } + @PostMapping("") + @ApiResponses( + value = { + @ApiResponse( + responseCode = "200", + description = "식재료 정보 확인 성공", + useReturnTypeSchema = true + ) + } + ) + @Operation(summary = "식재료 정보 확인 API", description = "식재료 정보 확인 + 리포트 DB 저장 API 입니다.") + public ApplicationResponse getIngredientInfo( + @UserResolver User user, + @RequestBody @Valid IngredientGetRequest request + ) throws URISyntaxException { + return ApplicationResponse.onSuccess(ingredientFindUseCase.getIngredient(user, request)); + } } From 6bc16054a1b215201a08c8f1f97430b8c001a56f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=95=88=EC=A0=95=ED=9B=84?= Date: Mon, 7 Oct 2024 16:56:53 +0900 Subject: [PATCH 2/7] =?UTF-8?q?feat:=20core-module=20report=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20logic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coremodule/report/ReportRepository.java | 7 ++ .../coremodule/report/domain/Report.java | 14 ++++ .../report/dto/ReportComparisonDTO.java | 13 ++++ .../report/service/ReportQueryService.java | 72 +++++++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 core-module/src/main/java/com/foodgo/coremodule/report/dto/ReportComparisonDTO.java diff --git a/core-module/src/main/java/com/foodgo/coremodule/report/ReportRepository.java b/core-module/src/main/java/com/foodgo/coremodule/report/ReportRepository.java index 85718fc..f4eac53 100644 --- a/core-module/src/main/java/com/foodgo/coremodule/report/ReportRepository.java +++ b/core-module/src/main/java/com/foodgo/coremodule/report/ReportRepository.java @@ -2,11 +2,18 @@ import com.foodgo.coremodule.report.domain.Report; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import java.time.LocalDate; import java.time.LocalDateTime; import java.util.List; public interface ReportRepository extends JpaRepository { List findAllByUserIdAndCreatedAtBetween(Long userId, LocalDateTime start, LocalDateTime end); + + // 이번 주 총 레포트 조회 + @Query("SELECT r FROM Report r WHERE r.user.id = :userId AND r.createdAt >= :startOfWeek AND r.createdAt < :endOfWeek") + List findReportsByUserAndWeek(Long userId, LocalDate startOfWeek, LocalDate endOfWeek); + } diff --git a/core-module/src/main/java/com/foodgo/coremodule/report/domain/Report.java b/core-module/src/main/java/com/foodgo/coremodule/report/domain/Report.java index df73749..05e4067 100644 --- a/core-module/src/main/java/com/foodgo/coremodule/report/domain/Report.java +++ b/core-module/src/main/java/com/foodgo/coremodule/report/domain/Report.java @@ -37,4 +37,18 @@ public class Report extends BaseEntity { @Column(name = "report_fat", nullable = false) private Integer fat; + @Column(name = "report_sugar", nullable = false) + private Integer sugar; // 당류 + + @Column(name = "report_sodium", nullable = false) + private Integer sodium; // 나트륨 + + @Column(name = "report_cholesterol", nullable = false) + private Integer cholesterol; // 콜레스테롤 + + @Column(name = "report_saturated_fat", nullable = false) + private Integer saturatedFat; // 포화지방산 + + @Column(name = "report_trans_fat", nullable = false) + private Integer transFat; // 트랜스지방 } diff --git a/core-module/src/main/java/com/foodgo/coremodule/report/dto/ReportComparisonDTO.java b/core-module/src/main/java/com/foodgo/coremodule/report/dto/ReportComparisonDTO.java new file mode 100644 index 0000000..ae70c50 --- /dev/null +++ b/core-module/src/main/java/com/foodgo/coremodule/report/dto/ReportComparisonDTO.java @@ -0,0 +1,13 @@ +package com.foodgo.coremodule.report.dto; + +public record ReportComparisonDTO( + int lastWeekTotal, + int thisWeekTotal, + int lastWeekCarbs, + int thisWeekCarbs, + int lastWeekProteins, + int thisWeekProteins, + int lastWeekFats, + int thisWeekFats +) { +} diff --git a/core-module/src/main/java/com/foodgo/coremodule/report/service/ReportQueryService.java b/core-module/src/main/java/com/foodgo/coremodule/report/service/ReportQueryService.java index 73639a5..ce713b3 100644 --- a/core-module/src/main/java/com/foodgo/coremodule/report/service/ReportQueryService.java +++ b/core-module/src/main/java/com/foodgo/coremodule/report/service/ReportQueryService.java @@ -2,10 +2,13 @@ import com.foodgo.coremodule.report.ReportRepository; import com.foodgo.coremodule.report.domain.Report; +import com.foodgo.coremodule.report.dto.ReportComparisonDTO; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.temporal.TemporalAdjusters; import java.util.List; @Service @@ -17,4 +20,73 @@ public class ReportQueryService { public List findReportByDate(Long userId, LocalDateTime start, LocalDateTime end) { return reportRepository.findAllByUserIdAndCreatedAtBetween(userId, start, end); } + + public void saveReport(Report report) { + reportRepository.save(report); + } + + // 이번 주 시작과 끝 날짜 구하기 + private LocalDate getStartOfWeek(LocalDate date) { + return date.with(TemporalAdjusters.previousOrSame(java.time.DayOfWeek.MONDAY)); + } + + private LocalDate getEndOfWeek(LocalDate date) { + return date.with(TemporalAdjusters.nextOrSame(java.time.DayOfWeek.SUNDAY)); + } + + // 이번 주와 저번 주의 칼로리 리포트 비교 + public ReportComparisonDTO getWeeklyReportComparison(Long userId) { + LocalDate today = LocalDate.now(); + + // 이번 주와 저번 주의 날짜 범위 계산 + LocalDate startOfThisWeek = getStartOfWeek(today); + LocalDate endOfThisWeek = getEndOfWeek(today); + LocalDate startOfLastWeek = startOfThisWeek.minusWeeks(1); + LocalDate endOfLastWeek = endOfThisWeek.minusWeeks(1); + + // 이번 주와 저번 주 레포트 가져오기 + List thisWeekReports = reportRepository.findReportsByUserAndWeek(userId, startOfThisWeek, endOfThisWeek); + List lastWeekReports = reportRepository.findReportsByUserAndWeek(userId, startOfLastWeek, endOfLastWeek); + + // 칼로리 및 영양소 총합 계산 + int thisWeekTotal = calculateTotalCalories(thisWeekReports); + int lastWeekTotal = calculateTotalCalories(lastWeekReports); + + int thisWeekCarbs = calculateTotalCarbs(thisWeekReports); + int lastWeekCarbs = calculateTotalCarbs(lastWeekReports); + + int thisWeekProteins = calculateTotalProteins(thisWeekReports); + int lastWeekProteins = calculateTotalProteins(lastWeekReports); + + int thisWeekFats = calculateTotalFats(thisWeekReports); + int lastWeekFats = calculateTotalFats(lastWeekReports); + + // 결과 반환 + return new ReportComparisonDTO( + lastWeekTotal, thisWeekTotal, + lastWeekCarbs, thisWeekCarbs, + lastWeekProteins, thisWeekProteins, + lastWeekFats, thisWeekFats + ); + } + + // 칼로리 합계 계산 + private int calculateTotalCalories(List reports) { + return reports.stream().mapToInt(Report::getTotal).sum(); + } + + // 탄수화물 합계 계산 + private int calculateTotalCarbs(List reports) { + return reports.stream().mapToInt(Report::getCarb).sum(); + } + + // 단백질 합계 계산 + private int calculateTotalProteins(List reports) { + return reports.stream().mapToInt(Report::getProtein).sum(); + } + + // 지방 합계 계산 + private int calculateTotalFats(List reports) { + return reports.stream().mapToInt(Report::getFat).sum(); + } } From 73166ad8be4721f593d8ab46e9bf65767229f16e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=95=88=EC=A0=95=ED=9B=84?= Date: Mon, 7 Oct 2024 16:57:15 +0900 Subject: [PATCH 3/7] =?UTF-8?q?feat:=20api-module=20report=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20logic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../report/application/ReportFindUseCase.java | 19 ++++++++ .../report/dto/ReportStatistics.java | 8 ++++ .../apimodule/report/mapper/ReportMapper.java | 23 ++++++++++ .../report/presentation/ReportController.java | 45 +++++++++++++++++++ 4 files changed, 95 insertions(+) create mode 100644 api-module/src/main/java/com/foodgo/apimodule/report/application/ReportFindUseCase.java create mode 100644 api-module/src/main/java/com/foodgo/apimodule/report/dto/ReportStatistics.java create mode 100644 api-module/src/main/java/com/foodgo/apimodule/report/mapper/ReportMapper.java create mode 100644 api-module/src/main/java/com/foodgo/apimodule/report/presentation/ReportController.java diff --git a/api-module/src/main/java/com/foodgo/apimodule/report/application/ReportFindUseCase.java b/api-module/src/main/java/com/foodgo/apimodule/report/application/ReportFindUseCase.java new file mode 100644 index 0000000..80dd446 --- /dev/null +++ b/api-module/src/main/java/com/foodgo/apimodule/report/application/ReportFindUseCase.java @@ -0,0 +1,19 @@ +package com.foodgo.apimodule.report.application; + +import com.foodgo.coremodule.report.dto.ReportComparisonDTO; +import com.foodgo.coremodule.report.service.ReportQueryService; +import com.foodgo.coremodule.user.domain.User; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class ReportFindUseCase { + + private final ReportQueryService reportQueryService; + + // 통게 dto list 만들기 + public ReportComparisonDTO getStatistics(User user) { + return reportQueryService.getWeeklyReportComparison(user.getId()); + } +} diff --git a/api-module/src/main/java/com/foodgo/apimodule/report/dto/ReportStatistics.java b/api-module/src/main/java/com/foodgo/apimodule/report/dto/ReportStatistics.java new file mode 100644 index 0000000..1c34cd4 --- /dev/null +++ b/api-module/src/main/java/com/foodgo/apimodule/report/dto/ReportStatistics.java @@ -0,0 +1,8 @@ +package com.foodgo.apimodule.report.dto; + +public record ReportStatistics( + String name, + Double lastweek, + Double thisweek +) { +} diff --git a/api-module/src/main/java/com/foodgo/apimodule/report/mapper/ReportMapper.java b/api-module/src/main/java/com/foodgo/apimodule/report/mapper/ReportMapper.java new file mode 100644 index 0000000..fcdead5 --- /dev/null +++ b/api-module/src/main/java/com/foodgo/apimodule/report/mapper/ReportMapper.java @@ -0,0 +1,23 @@ +package com.foodgo.apimodule.report.mapper; + +import com.foodgo.coremodule.ingredient.dto.response.IngredientGetResponse; +import com.foodgo.coremodule.report.domain.Report; +import com.foodgo.coremodule.user.domain.User; + +public class ReportMapper { + + public static Report mapToReport(User user, IngredientGetResponse.Row row) { + return Report.builder() + .user(user) + .total(Integer.parseInt(row.getNutrCont1())) // 총 칼로리 + .carb(Integer.parseInt(row.getNutrCont2())) // 탄수화물 + .protein(Integer.parseInt(row.getNutrCont3())) // 단백질 + .fat(Integer.parseInt(row.getNutrCont4())) // 지방 + .sugar(Integer.parseInt(row.getNutrCont5())) // 당류 + .sodium(Integer.parseInt(row.getNutrCont6())) // 나트륨 + .cholesterol(Integer.parseInt(row.getNutrCont7())) // 콜레스테롤 + .saturatedFat(Integer.parseInt(row.getNutrCont8())) // 포화지방산 + .transFat(Integer.parseInt(row.getNutrCont9())) // 트랜스지방 + .build(); + } +} diff --git a/api-module/src/main/java/com/foodgo/apimodule/report/presentation/ReportController.java b/api-module/src/main/java/com/foodgo/apimodule/report/presentation/ReportController.java new file mode 100644 index 0000000..2fab0b1 --- /dev/null +++ b/api-module/src/main/java/com/foodgo/apimodule/report/presentation/ReportController.java @@ -0,0 +1,45 @@ +package com.foodgo.apimodule.report.presentation; + +import com.foodgo.apimodule.report.application.ReportFindUseCase; +import com.foodgo.commonmodule.common.ApplicationResponse; +import com.foodgo.coremodule.report.dto.ReportComparisonDTO; +import com.foodgo.coremodule.security.annotation.UserResolver; +import com.foodgo.coremodule.user.domain.User; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Slf4j +@RequiredArgsConstructor +@RestController +@RequestMapping("/api/v1/report") +@Tag(name = "report", description = "리포트 관련 API") +public class ReportController { + + private final ReportFindUseCase reportFindUseCase; + + @GetMapping() + @ApiResponses( + value = { + @ApiResponse( + responseCode = "200", + description = "리포트 통계 확인 성공", + useReturnTypeSchema = true + ) + } + ) + @Operation(summary = "리포트 통계 확인 API", description = "리포트 통계 확인 API 입니다.") + public ApplicationResponse findReportStatistics( + @UserResolver User user) { + + ReportComparisonDTO comparisonDTO = reportFindUseCase.getStatistics(user); + return ApplicationResponse.onSuccess(comparisonDTO); + } + +} From 1aa8fa6a100317c9579906c3877632979a65ec89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=95=88=EC=A0=95=ED=9B=84?= Date: Mon, 7 Oct 2024 17:01:07 +0900 Subject: [PATCH 4/7] =?UTF-8?q?fix:=20=EA=B0=92=EC=9D=B4=20=EC=97=86?= =?UTF-8?q?=EC=9D=84=20=EB=95=8C=EC=9D=98=200=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../report/service/ReportQueryService.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/core-module/src/main/java/com/foodgo/coremodule/report/service/ReportQueryService.java b/core-module/src/main/java/com/foodgo/coremodule/report/service/ReportQueryService.java index ce713b3..4ddf7aa 100644 --- a/core-module/src/main/java/com/foodgo/coremodule/report/service/ReportQueryService.java +++ b/core-module/src/main/java/com/foodgo/coremodule/report/service/ReportQueryService.java @@ -48,18 +48,18 @@ public ReportComparisonDTO getWeeklyReportComparison(Long userId) { List thisWeekReports = reportRepository.findReportsByUserAndWeek(userId, startOfThisWeek, endOfThisWeek); List lastWeekReports = reportRepository.findReportsByUserAndWeek(userId, startOfLastWeek, endOfLastWeek); - // 칼로리 및 영양소 총합 계산 - int thisWeekTotal = calculateTotalCalories(thisWeekReports); - int lastWeekTotal = calculateTotalCalories(lastWeekReports); + // 데이터가 있을 때만 합계를 계산하고, 없으면 0으로 처리 + int thisWeekTotal = thisWeekReports.isEmpty() ? 0 : calculateTotalCalories(thisWeekReports); + int lastWeekTotal = lastWeekReports.isEmpty() ? 0 : calculateTotalCalories(lastWeekReports); - int thisWeekCarbs = calculateTotalCarbs(thisWeekReports); - int lastWeekCarbs = calculateTotalCarbs(lastWeekReports); + int thisWeekCarbs = thisWeekReports.isEmpty() ? 0 : calculateTotalCarbs(thisWeekReports); + int lastWeekCarbs = lastWeekReports.isEmpty() ? 0 : calculateTotalCarbs(lastWeekReports); - int thisWeekProteins = calculateTotalProteins(thisWeekReports); - int lastWeekProteins = calculateTotalProteins(lastWeekReports); + int thisWeekProteins = thisWeekReports.isEmpty() ? 0 : calculateTotalProteins(thisWeekReports); + int lastWeekProteins = lastWeekReports.isEmpty() ? 0 : calculateTotalProteins(lastWeekReports); - int thisWeekFats = calculateTotalFats(thisWeekReports); - int lastWeekFats = calculateTotalFats(lastWeekReports); + int thisWeekFats = thisWeekReports.isEmpty() ? 0 : calculateTotalFats(thisWeekReports); + int lastWeekFats = lastWeekReports.isEmpty() ? 0 : calculateTotalFats(lastWeekReports); // 결과 반환 return new ReportComparisonDTO( From 7ac3da822878c51ed743d5e1de63d6719871d781 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=95=88=EC=A0=95=ED=9B=84?= Date: Tue, 8 Oct 2024 15:18:34 +0900 Subject: [PATCH 5/7] =?UTF-8?q?feat:=20report=20type=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/ChallengeFindUseCase.java | 10 +++--- .../presentation/IngredientController.java | 2 +- .../apimodule/report/mapper/ReportMapper.java | 36 ++++++++++++++----- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/api-module/src/main/java/com/foodgo/apimodule/community/application/ChallengeFindUseCase.java b/api-module/src/main/java/com/foodgo/apimodule/community/application/ChallengeFindUseCase.java index 9b810b9..5560e46 100644 --- a/api-module/src/main/java/com/foodgo/apimodule/community/application/ChallengeFindUseCase.java +++ b/api-module/src/main/java/com/foodgo/apimodule/community/application/ChallengeFindUseCase.java @@ -47,20 +47,20 @@ public ChallengeList findChallengeList(User user, Long challengeId) { } private double calculateRate(List reports, Challenge challenge) { - int totalSum = 0; + double totalSum = 0; switch (challenge.getType()) { case CALORIE: - totalSum = reports.stream().mapToInt(Report::getTotal).sum(); + totalSum = reports.stream().mapToDouble(Report::getTotal).sum(); break; case CARB: - totalSum = reports.stream().mapToInt(Report::getCarb).sum(); + totalSum = reports.stream().mapToDouble(Report::getCarb).sum(); break; case PROTEIN: - totalSum = reports.stream().mapToInt(Report::getProtein).sum(); + totalSum = reports.stream().mapToDouble(Report::getProtein).sum(); break; case FAT: - totalSum = reports.stream().mapToInt(Report::getFat).sum(); + totalSum = reports.stream().mapToDouble(Report::getFat).sum(); break; case FREQUENCY: totalSum = reports.size(); diff --git a/api-module/src/main/java/com/foodgo/apimodule/ingredient/presentation/IngredientController.java b/api-module/src/main/java/com/foodgo/apimodule/ingredient/presentation/IngredientController.java index 3895a19..bda044a 100644 --- a/api-module/src/main/java/com/foodgo/apimodule/ingredient/presentation/IngredientController.java +++ b/api-module/src/main/java/com/foodgo/apimodule/ingredient/presentation/IngredientController.java @@ -24,7 +24,7 @@ @RequiredArgsConstructor @RestController @RequestMapping("/api/v1/ingredients") -@Tag(name = "ingredinet", description = "식재료 관련 API") +@Tag(name = "ingredient", description = "식재료 관련 API") public class IngredientController { private final IngredientFindUseCase ingredientFindUseCase; diff --git a/api-module/src/main/java/com/foodgo/apimodule/report/mapper/ReportMapper.java b/api-module/src/main/java/com/foodgo/apimodule/report/mapper/ReportMapper.java index fcdead5..0a1c673 100644 --- a/api-module/src/main/java/com/foodgo/apimodule/report/mapper/ReportMapper.java +++ b/api-module/src/main/java/com/foodgo/apimodule/report/mapper/ReportMapper.java @@ -1,23 +1,41 @@ package com.foodgo.apimodule.report.mapper; import com.foodgo.coremodule.ingredient.dto.response.IngredientGetResponse; +import com.foodgo.coremodule.report.domain.MealType; import com.foodgo.coremodule.report.domain.Report; import com.foodgo.coremodule.user.domain.User; +import java.time.LocalTime; + public class ReportMapper { public static Report mapToReport(User user, IngredientGetResponse.Row row) { + MealType mealType = determineMealType(); // MealType을 결정하는 메서드 호출 + return Report.builder() .user(user) - .total(Integer.parseInt(row.getNutrCont1())) // 총 칼로리 - .carb(Integer.parseInt(row.getNutrCont2())) // 탄수화물 - .protein(Integer.parseInt(row.getNutrCont3())) // 단백질 - .fat(Integer.parseInt(row.getNutrCont4())) // 지방 - .sugar(Integer.parseInt(row.getNutrCont5())) // 당류 - .sodium(Integer.parseInt(row.getNutrCont6())) // 나트륨 - .cholesterol(Integer.parseInt(row.getNutrCont7())) // 콜레스테롤 - .saturatedFat(Integer.parseInt(row.getNutrCont8())) // 포화지방산 - .transFat(Integer.parseInt(row.getNutrCont9())) // 트랜스지방 + .type(mealType) // MealType 저장 + .total(Double.parseDouble(row.getNutrCont1())) // 총 칼로리 + .carb(Double.parseDouble(row.getNutrCont2())) // 탄수화물 + .protein(Double.parseDouble(row.getNutrCont3())) // 단백질 + .fat(Double.parseDouble(row.getNutrCont4())) // 지방 + .sugar(Double.parseDouble(row.getNutrCont5())) // 당류 + .sodium(Double.parseDouble(row.getNutrCont6())) // 나트륨 + .cholesterol(Double.parseDouble(row.getNutrCont7())) // 콜레스테롤 + .saturatedFat(Double.parseDouble(row.getNutrCont8())) // 포화지방산 + .transFat(Double.parseDouble(row.getNutrCont9())) // 트랜스지방 .build(); } + + private static MealType determineMealType() { + LocalTime now = LocalTime.now(); + + if (now.isBefore(LocalTime.NOON)) { + return MealType.BREAKFAST; // 정오 이전이면 아침 + } else if (now.isBefore(LocalTime.of(15, 0))) { + return MealType.LUNCH; // 오후 3시 이전이면 점심 + } else { + return MealType.DINNER; // 그 이후는 저녁 + } + } } From 828acfd7b906959ef21352c69a009bdf85dd359a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=95=88=EC=A0=95=ED=9B=84?= Date: Tue, 8 Oct 2024 15:18:54 +0900 Subject: [PATCH 6/7] =?UTF-8?q?feat:=20core-module=20type=20=EB=B0=8F=20fi?= =?UTF-8?q?eld=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coremodule/report/ReportRepository.java | 3 +- .../coremodule/report/domain/Report.java | 18 ++-- .../report/dto/ReportComparisonDTO.java | 26 +++-- .../report/service/ReportQueryService.java | 102 +++++++++++++----- 4 files changed, 104 insertions(+), 45 deletions(-) diff --git a/core-module/src/main/java/com/foodgo/coremodule/report/ReportRepository.java b/core-module/src/main/java/com/foodgo/coremodule/report/ReportRepository.java index f4eac53..028716b 100644 --- a/core-module/src/main/java/com/foodgo/coremodule/report/ReportRepository.java +++ b/core-module/src/main/java/com/foodgo/coremodule/report/ReportRepository.java @@ -4,7 +4,6 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; -import java.time.LocalDate; import java.time.LocalDateTime; import java.util.List; @@ -14,6 +13,6 @@ public interface ReportRepository extends JpaRepository { // 이번 주 총 레포트 조회 @Query("SELECT r FROM Report r WHERE r.user.id = :userId AND r.createdAt >= :startOfWeek AND r.createdAt < :endOfWeek") - List findReportsByUserAndWeek(Long userId, LocalDate startOfWeek, LocalDate endOfWeek); + List findReportsByUserAndCreatedAtBetween(Long userId, LocalDateTime startOfWeek, LocalDateTime endOfWeek); } diff --git a/core-module/src/main/java/com/foodgo/coremodule/report/domain/Report.java b/core-module/src/main/java/com/foodgo/coremodule/report/domain/Report.java index 05e4067..afac597 100644 --- a/core-module/src/main/java/com/foodgo/coremodule/report/domain/Report.java +++ b/core-module/src/main/java/com/foodgo/coremodule/report/domain/Report.java @@ -26,29 +26,29 @@ public class Report extends BaseEntity { private MealType type; @Column(name = "report_total", nullable = false) - private Integer total; + private Double total; @Column(name = "report_carb", nullable = false) - private Integer carb; + private Double carb; @Column(name = "report_protein", nullable = false) - private Integer protein; + private Double protein; @Column(name = "report_fat", nullable = false) - private Integer fat; + private Double fat; @Column(name = "report_sugar", nullable = false) - private Integer sugar; // 당류 + private Double sugar; // 당류 @Column(name = "report_sodium", nullable = false) - private Integer sodium; // 나트륨 + private Double sodium; // 나트륨 @Column(name = "report_cholesterol", nullable = false) - private Integer cholesterol; // 콜레스테롤 + private Double cholesterol; // 콜레스테롤 @Column(name = "report_saturated_fat", nullable = false) - private Integer saturatedFat; // 포화지방산 + private Double saturatedFat; // 포화지방산 @Column(name = "report_trans_fat", nullable = false) - private Integer transFat; // 트랜스지방 + private Double transFat; // 트랜스지방 } diff --git a/core-module/src/main/java/com/foodgo/coremodule/report/dto/ReportComparisonDTO.java b/core-module/src/main/java/com/foodgo/coremodule/report/dto/ReportComparisonDTO.java index ae70c50..433066b 100644 --- a/core-module/src/main/java/com/foodgo/coremodule/report/dto/ReportComparisonDTO.java +++ b/core-module/src/main/java/com/foodgo/coremodule/report/dto/ReportComparisonDTO.java @@ -1,13 +1,23 @@ package com.foodgo.coremodule.report.dto; public record ReportComparisonDTO( - int lastWeekTotal, - int thisWeekTotal, - int lastWeekCarbs, - int thisWeekCarbs, - int lastWeekProteins, - int thisWeekProteins, - int lastWeekFats, - int thisWeekFats + Double lastWeekTotal, + Double thisWeekTotal, + Double lastWeekCarbs, + Double thisWeekCarbs, + Double lastWeekProteins, + Double thisWeekProteins, + Double lastWeekFats, + Double thisWeekFats, + Double lastWeekSugar, + Double thisWeekSugar, + Double lastWeekSodium, + Double thisWeekSodium, + Double lastWeekCholesterol, + Double thisWeekCholesterol, + Double lastWeekSaturatedFat, + Double thisWeekSaturatedFat, + Double lastWeekTransFat, + Double thisWeekTransFat ) { } diff --git a/core-module/src/main/java/com/foodgo/coremodule/report/service/ReportQueryService.java b/core-module/src/main/java/com/foodgo/coremodule/report/service/ReportQueryService.java index 4ddf7aa..e9c8c39 100644 --- a/core-module/src/main/java/com/foodgo/coremodule/report/service/ReportQueryService.java +++ b/core-module/src/main/java/com/foodgo/coremodule/report/service/ReportQueryService.java @@ -34,7 +34,6 @@ private LocalDate getEndOfWeek(LocalDate date) { return date.with(TemporalAdjusters.nextOrSame(java.time.DayOfWeek.SUNDAY)); } - // 이번 주와 저번 주의 칼로리 리포트 비교 public ReportComparisonDTO getWeeklyReportComparison(Long userId) { LocalDate today = LocalDate.now(); @@ -44,49 +43,100 @@ public ReportComparisonDTO getWeeklyReportComparison(Long userId) { LocalDate startOfLastWeek = startOfThisWeek.minusWeeks(1); LocalDate endOfLastWeek = endOfThisWeek.minusWeeks(1); - // 이번 주와 저번 주 레포트 가져오기 - List thisWeekReports = reportRepository.findReportsByUserAndWeek(userId, startOfThisWeek, endOfThisWeek); - List lastWeekReports = reportRepository.findReportsByUserAndWeek(userId, startOfLastWeek, endOfLastWeek); + // 이번 주와 저번 주의 칼로리 합산 + double thisWeekTotal = calculateTotalForWeek(userId, startOfThisWeek, endOfThisWeek); + double lastWeekTotal = calculateTotalForWeek(userId, startOfLastWeek, endOfLastWeek); - // 데이터가 있을 때만 합계를 계산하고, 없으면 0으로 처리 - int thisWeekTotal = thisWeekReports.isEmpty() ? 0 : calculateTotalCalories(thisWeekReports); - int lastWeekTotal = lastWeekReports.isEmpty() ? 0 : calculateTotalCalories(lastWeekReports); + // 영양소별 합산 (탄수화물, 단백질, 지방, 당류, 나트륨 등) + double thisWeekCarbs = calculateCarbsForWeek(userId, startOfThisWeek, endOfThisWeek); + double lastWeekCarbs = calculateCarbsForWeek(userId, startOfLastWeek, endOfLastWeek); - int thisWeekCarbs = thisWeekReports.isEmpty() ? 0 : calculateTotalCarbs(thisWeekReports); - int lastWeekCarbs = lastWeekReports.isEmpty() ? 0 : calculateTotalCarbs(lastWeekReports); + double thisWeekProteins = calculateProteinsForWeek(userId, startOfThisWeek, endOfThisWeek); + double lastWeekProteins = calculateProteinsForWeek(userId, startOfLastWeek, endOfLastWeek); - int thisWeekProteins = thisWeekReports.isEmpty() ? 0 : calculateTotalProteins(thisWeekReports); - int lastWeekProteins = lastWeekReports.isEmpty() ? 0 : calculateTotalProteins(lastWeekReports); + double thisWeekFats = calculateFatsForWeek(userId, startOfThisWeek, endOfThisWeek); + double lastWeekFats = calculateFatsForWeek(userId, startOfLastWeek, endOfLastWeek); - int thisWeekFats = thisWeekReports.isEmpty() ? 0 : calculateTotalFats(thisWeekReports); - int lastWeekFats = lastWeekReports.isEmpty() ? 0 : calculateTotalFats(lastWeekReports); + double thisWeekSugar = calculateSugarForWeek(userId, startOfThisWeek, endOfThisWeek); + double lastWeekSugar = calculateSugarForWeek(userId, startOfLastWeek, endOfLastWeek); + + double thisWeekSodium = calculateSodiumForWeek(userId, startOfThisWeek, endOfThisWeek); + double lastWeekSodium = calculateSodiumForWeek(userId, startOfLastWeek, endOfLastWeek); + + double thisWeekCholesterol = calculateCholesterolForWeek(userId, startOfThisWeek, endOfThisWeek); + double lastWeekCholesterol = calculateCholesterolForWeek(userId, startOfLastWeek, endOfLastWeek); + + double thisWeekSaturatedFat = calculateSaturatedFatForWeek(userId, startOfThisWeek, endOfThisWeek); + double lastWeekSaturatedFat = calculateSaturatedFatForWeek(userId, startOfLastWeek, endOfLastWeek); + + double thisWeekTransFat = calculateTransFatForWeek(userId, startOfThisWeek, endOfThisWeek); + double lastWeekTransFat = calculateTransFatForWeek(userId, startOfLastWeek, endOfLastWeek); // 결과 반환 return new ReportComparisonDTO( lastWeekTotal, thisWeekTotal, lastWeekCarbs, thisWeekCarbs, lastWeekProteins, thisWeekProteins, - lastWeekFats, thisWeekFats + lastWeekFats, thisWeekFats, + lastWeekSugar, thisWeekSugar, + lastWeekSodium, thisWeekSodium, + lastWeekCholesterol, thisWeekCholesterol, + lastWeekSaturatedFat, thisWeekSaturatedFat, + lastWeekTransFat, thisWeekTransFat ); } - // 칼로리 합계 계산 - private int calculateTotalCalories(List reports) { - return reports.stream().mapToInt(Report::getTotal).sum(); + // 주간 칼로리 합계 계산 + private double calculateTotalForWeek(Long userId, LocalDate start, LocalDate end) { + List reports = reportRepository.findReportsByUserAndCreatedAtBetween(userId, start.atStartOfDay(), end.atTime(23, 59, 59)); + return reports.stream().mapToDouble(Report::getTotal).sum(); + } + + // 주간 탄수화물 합계 계산 + private double calculateCarbsForWeek(Long userId, LocalDate start, LocalDate end) { + List reports = reportRepository.findReportsByUserAndCreatedAtBetween(userId, start.atStartOfDay(), end.atTime(23, 59, 59)); + return reports.stream().mapToDouble(Report::getCarb).sum(); + } + + // 주간 단백질 합계 계산 + private double calculateProteinsForWeek(Long userId, LocalDate start, LocalDate end) { + List reports = reportRepository.findReportsByUserAndCreatedAtBetween(userId, start.atStartOfDay(), end.atTime(23, 59, 59)); + return reports.stream().mapToDouble(Report::getProtein).sum(); + } + + // 주간 지방 합계 계산 + private double calculateFatsForWeek(Long userId, LocalDate start, LocalDate end) { + List reports = reportRepository.findReportsByUserAndCreatedAtBetween(userId, start.atStartOfDay(), end.atTime(23, 59, 59)); + return reports.stream().mapToDouble(Report::getFat).sum(); + } + + // 주간 당류 합계 계산 + private double calculateSugarForWeek(Long userId, LocalDate start, LocalDate end) { + List reports = reportRepository.findReportsByUserAndCreatedAtBetween(userId, start.atStartOfDay(), end.atTime(23, 59, 59)); + return reports.stream().mapToDouble(Report::getSugar).sum(); + } + + // 주간 나트륨 합계 계산 + private double calculateSodiumForWeek(Long userId, LocalDate start, LocalDate end) { + List reports = reportRepository.findReportsByUserAndCreatedAtBetween(userId, start.atStartOfDay(), end.atTime(23, 59, 59)); + return reports.stream().mapToDouble(Report::getSodium).sum(); } - // 탄수화물 합계 계산 - private int calculateTotalCarbs(List reports) { - return reports.stream().mapToInt(Report::getCarb).sum(); + // 주간 콜레스테롤 합계 계산 + private double calculateCholesterolForWeek(Long userId, LocalDate start, LocalDate end) { + List reports = reportRepository.findReportsByUserAndCreatedAtBetween(userId, start.atStartOfDay(), end.atTime(23, 59, 59)); + return reports.stream().mapToDouble(Report::getCholesterol).sum(); } - // 단백질 합계 계산 - private int calculateTotalProteins(List reports) { - return reports.stream().mapToInt(Report::getProtein).sum(); + // 주간 포화지방산 합계 계산 + private double calculateSaturatedFatForWeek(Long userId, LocalDate start, LocalDate end) { + List reports = reportRepository.findReportsByUserAndCreatedAtBetween(userId, start.atStartOfDay(), end.atTime(23, 59, 59)); + return reports.stream().mapToDouble(Report::getSaturatedFat).sum(); } - // 지방 합계 계산 - private int calculateTotalFats(List reports) { - return reports.stream().mapToInt(Report::getFat).sum(); + // 주간 트랜스지방 합계 계산 + private double calculateTransFatForWeek(Long userId, LocalDate start, LocalDate end) { + List reports = reportRepository.findReportsByUserAndCreatedAtBetween(userId, start.atStartOfDay(), end.atTime(23, 59, 59)); + return reports.stream().mapToDouble(Report::getTransFat).sum(); } } From 06758e8c3f3b151ddae5a278655dfd85a1439c13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=95=88=EC=A0=95=ED=9B=84?= Date: Tue, 8 Oct 2024 15:19:38 +0900 Subject: [PATCH 7/7] test: for feature deploy --- .github/workflows/cd.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 784f53f..331ec4d 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -2,7 +2,7 @@ name: Backend CD # actions 이름 on: push: - branches: [ fix/#28 ] + branches: [ feature/#29 ] jobs: deploy: