From bfbfedbecdf917050fc87f1c6603a80e2ffd4444 Mon Sep 17 00:00:00 2001 From: MJJ Date: Mon, 12 Aug 2024 13:13:40 +0900 Subject: [PATCH 01/35] =?UTF-8?q?[refactor]=20=EB=98=90=EB=9E=98=EB=93=A4?= =?UTF-8?q?=EC=9D=B4=20=EA=B0=80=EC=9E=A5=20=EB=A7=8E=EC=9D=B4=20=EC=86=8C?= =?UTF-8?q?=EB=B9=84=ED=95=9C=20=EC=B9=B4=ED=85=8C=EA=B3=A0=EB=A6=AC=20?= =?UTF-8?q?=EC=A0=84=EC=B2=B4=20=EC=A1=B0=ED=9A=8C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - page 제거 - top 제거 (리밋 x) --- .../controller/ConsumptionGoalApi.java | 16 +++------------- .../controller/ConsumptionGoalController.java | 17 +++-------------- 2 files changed, 6 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalApi.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalApi.java index 25249f26..3b27bbed 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalApi.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalApi.java @@ -56,22 +56,12 @@ ResponseEntity getTopGoalCategoriesPage(Long userId, ResponseEntity updateOrElseGenerateConsumptionGoal(Long userId, ConsumptionGoalListRequestDto consumptionGoalListRequestDto); - @Operation(summary = "또래들이 가장 많이한 소비 카테고리 조회 Top4 API", description = "특정 사용자의 소비 카테고리별 소비 금액을 조회하는 API 입니다.") - @ApiResponses(value = {@ApiResponse(responseCode = "COMMON200", description = "OK, 성공")}) - @Parameters({@Parameter(name = "top", description = "가장 큰 소비 카테고리의 개수를 지정합니다."), - @Parameter(name = "userId", description = "로그인 한 유저 아이디"), - @Parameter(name = "peerAgeStart", description = "또래나이 시작 범위"), - @Parameter(name = "peerAgeEnd", description = "또래나이 끝 범위"), - @Parameter(name = "peerGender", description = "또래 성별")}) - ResponseEntity getConsumptionGoalList(int top, Long userId, int peerAgeStart, int peerAgeEnd, - String peerGender); - - @Operation(summary = "또래들이 가장 많이한 소비 카테고리 조회 API", description = "특정 사용자의 소비 카테고리별 소비 금액을 전체 조회하는 API 입니다.") + @Operation(summary = "또래들이 가장 많이한 소비 카테고리와 평균 금액 및 내 소비금액 차이 조회 API", description = "특정 사용자의 또래 소비 카테고리별 평균 소비 금액을 조회하는 API 입니다.") @ApiResponses(value = {@ApiResponse(responseCode = "COMMON200", description = "OK, 성공")}) @Parameters({@Parameter(name = "userId", description = "로그인 한 유저 아이디"), @Parameter(name = "peerAgeStart", description = "또래나이 시작 범위"), @Parameter(name = "peerAgeEnd", description = "또래나이 끝 범위"), @Parameter(name = "peerGender", description = "또래 성별")}) - ResponseEntity getConsumptionGoalPage(Long userId, int peerAgeStart, int peerAgeEnd, String peerGender, - Pageable pageable); + ResponseEntity getConsumptionGoalList(Long userId, int peerAgeStart, int peerAgeEnd, + String peerGender); } \ No newline at end of file diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java index c435c94e..30cb85b3 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java @@ -90,24 +90,13 @@ public ResponseEntity updateOrElseGenerateConsum .body(consumptionGoalService.updateConsumptionGoals(userId, consumptionGoalListRequestDto)); } - @GetMapping("/top-categories/top-consumption/{top}") - public ResponseEntity getConsumptionGoalList(@PathVariable(name = "top") int top, - @RequestParam(name = "userId") Long userId, + @GetMapping("/top-categories/top-consumption") + public ResponseEntity getConsumptionGoalList(@RequestParam(name = "userId") Long userId, @RequestParam(name = "peerAgeStart", defaultValue = "0") int peerAgeStart, @RequestParam(name = "peerAgeEnd", defaultValue = "0") int peerAgeEnd, @RequestParam(name = "peerGender", defaultValue = "none") String peerGender) { - List response = consumptionGoalService.getTopConsumptionsLimit(top, userId, + List response = consumptionGoalService.getTopConsumptionsLimit(userId, peerAgeStart, peerAgeEnd, peerGender); return ResponseEntity.ok(response); } - - @GetMapping("/top-categories/top-consumption") - public ResponseEntity getConsumptionGoalPage(@RequestParam(name = "userId") Long userId, - @RequestParam(name = "peerAgeStart", defaultValue = "0") int peerAgeStart, - @RequestParam(name = "peerAgeEnd", defaultValue = "0") int peerAgeEnd, - @RequestParam(name = "peerGender", defaultValue = "none") String peerGender, Pageable pageable) { - Page response = consumptionGoalService.getTopConsumptions(userId, - peerAgeStart, peerAgeEnd, peerGender, pageable); - return ResponseEntity.ok(response.getContent()); - } } \ No newline at end of file From 1a0083c1616b69639e5636829054e095bfc11e7c Mon Sep 17 00:00:00 2001 From: MJJ Date: Mon, 12 Aug 2024 13:14:28 +0900 Subject: [PATCH 02/35] =?UTF-8?q?[refactor]=20=EB=98=90=EB=9E=98=EB=93=A4?= =?UTF-8?q?=EC=9D=B4=20=EA=B0=80=EC=9E=A5=20=EB=A7=8E=EC=9D=B4=20=EC=86=8C?= =?UTF-8?q?=EB=B9=84=ED=95=9C=20=EC=B9=B4=ED=85=8C=EA=B3=A0=EB=A6=AC=20pag?= =?UTF-8?q?e=20=EC=A1=B0=ED=9A=8C=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/ConsumptionGoalService.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java index 041ce876..7799bba4 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java @@ -21,17 +21,17 @@ public interface ConsumptionGoalService { List getTopGoalCategoriesLimit(int top, Long userId, int peerAgeStart, int peerAgeEnd, - String peerGender); + String peerGender); Page getTopGoalCategories(Long userId, int peerAgeStart, int peerAgeEnd, - String peerGender, Pageable pageable); + String peerGender, Pageable pageable); ConsumptionGoalResponseListDto findUserConsumptionGoalList(Long userId, LocalDate date); PeerInfoResponseDTO getPeerInfo(Long userId, int peerAgeStart, int peerAgeEnd, String peerGender); ConsumptionGoalResponseListDto updateConsumptionGoals(Long userId, - ConsumptionGoalListRequestDto consumptionGoalListRequestDto); + ConsumptionGoalListRequestDto consumptionGoalListRequestDto); ConsumptionAnalysisResponseDTO getTopCategoryAndConsumptionAmount(Long userId); @@ -41,8 +41,6 @@ ConsumptionGoalResponseListDto updateConsumptionGoals(Long userId, void decreaseConsumeAmount(Long userId, Long categoryId, Long amount, LocalDate expenseDate); - List getTopConsumption(int top, Long userId, int peerAgeS, int peerAgeE, String peerG); - - Page getTopConsumptions(Long userId, int peerAgeS, int peerAgeE, - String peerG, Pageable pageable); + List getTopConsumptionsLimit(Long userId, int peerAgeS, int peerAgeE, + String peerG); } From 6d35836aa459a7294ceb1500f55a68fa76bf5f2c Mon Sep 17 00:00:00 2001 From: MJJ Date: Mon, 12 Aug 2024 13:15:58 +0900 Subject: [PATCH 03/35] =?UTF-8?q?[refactor]=20=EB=98=90=EB=9E=98=EB=93=A4?= =?UTF-8?q?=EC=9D=B4=20=EA=B0=80=EC=9E=A5=20=EB=A7=8E=EC=9D=B4=20=EC=86=8C?= =?UTF-8?q?=EB=B9=84=ED=95=9C=20=EC=B9=B4=ED=85=8C=EA=B3=A0=EB=A6=AC=20?= =?UTF-8?q?=EB=B0=8F=20=ED=8F=89=EA=B7=A0=20=EC=86=8C=EB=B9=84=EA=B8=88?= =?UTF-8?q?=EC=95=A1=EA=B3=BC=20=EB=82=B4=20=EC=86=8C=EB=B9=84=EA=B8=88?= =?UTF-8?q?=EC=95=A1=20=EC=B0=A8=20DTO=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/consumptiongoal/dto/TopConsumptionResponseDTO.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/TopConsumptionResponseDTO.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/TopConsumptionResponseDTO.java index e6041e2b..cfd0b44b 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/TopConsumptionResponseDTO.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/TopConsumptionResponseDTO.java @@ -13,5 +13,7 @@ public class TopConsumptionResponseDTO { private String categoryName; - private Long consumeAmount; + private Long avgConsumeAmount; + + private Long consumeAmountDifference; } From 143ad837441b6e1130ce7c38be6462f1a6963875 Mon Sep 17 00:00:00 2001 From: MJJ Date: Mon, 12 Aug 2024 13:34:59 +0900 Subject: [PATCH 04/35] =?UTF-8?q?[refactor]=20=EB=98=90=EB=9E=98=EB=93=A4?= =?UTF-8?q?=EC=9D=B4=20=EA=B0=80=EC=9E=A5=20=EB=A7=8E=EC=9D=B4=20=EC=86=8C?= =?UTF-8?q?=EB=B9=84=ED=95=9C=20=EC=B9=B4=ED=85=8C=EA=B3=A0=EB=A6=AC=20?= =?UTF-8?q?=EB=B0=8F=20=ED=8F=89=EA=B7=A0=20=EC=86=8C=EB=B9=84=20=EA=B8=88?= =?UTF-8?q?=EC=95=A1=20=EC=A1=B0=ED=9A=8C=20repository=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/ConsumptionGoalRepository.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java index 0550465e..bbb9fccb 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java @@ -12,6 +12,7 @@ import org.springframework.stereotype.Repository; import com.bbteam.budgetbuddies.domain.category.entity.Category; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.CategoryAvgConsumptionDTO; import com.bbteam.budgetbuddies.domain.consumptiongoal.entity.ConsumptionGoal; import com.bbteam.budgetbuddies.domain.user.entity.User; import com.bbteam.budgetbuddies.enums.Gender; @@ -58,4 +59,15 @@ Page findTopConsumptionAndConsumeAmount(@Param("peerAgeStart") @Param("peerAgeEnd") int peerAgeEnd, @Param("peerGender") Gender peerGender, @Param("currentMonth") LocalDate currentMonth, Pageable pageable); -} \ No newline at end of file + @Query( + "SELECT new com.bbteam.budgetbuddies.domain.consumptiongoal.dto.CategoryAvgConsumptionDTO(cg.category.id, AVG(cg.consumeAmount))" + + "FROM ConsumptionGoal cg " + "WHERE cg.category.isDefault = true " + + "AND cg.user.age BETWEEN :peerAgeStart AND :peerAgeEnd " + "AND cg.user.gender = :peerGender " + + "AND cg.goalMonth >= :currentMonth " + "GROUP BY cg.category.id " + "ORDER BY AVG(cg.consumeAmount) DESC") + List findAvgConsumptionByCategory( + @Param("peerAgeStart") int peerAgeStart, + @Param("peerAgeEnd") int peerAgeEnd, + @Param("peerGender") Gender peerGender, + @Param("currentMonth") LocalDate currentMonth); + +} From 219ee16c600fee76c96663423ae4a2a7e1e33a42 Mon Sep 17 00:00:00 2001 From: MJJ Date: Mon, 12 Aug 2024 13:37:32 +0900 Subject: [PATCH 05/35] =?UTF-8?q?[refactor]=20=ED=95=84=EC=9A=94=20?= =?UTF-8?q?=EC=97=86=EB=8A=94=20repository=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - page 조회 제거 - 소비 금액 순 ConsumptionGoal 조회 제거 --- .../repository/ConsumptionGoalRepository.java | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java index bbb9fccb..6ad22365 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java @@ -45,20 +45,6 @@ Optional findConsumptionGoalByUserAndCategoryAndGoalMonth(User Optional findTopConsumptionByCategoryIdAndCurrentWeek(@Param("categoryId") Long categoryId, @Param("startOfWeek") LocalDate startOfWeek, @Param("endOfWeek") LocalDate endOfWeek); - @Query("SELECT cg FROM ConsumptionGoal cg " + "WHERE cg.category.isDefault = true " - + "AND cg.user.age BETWEEN :peerAgeStart AND :peerAgeEnd " + "AND cg.user.gender = :peerGender " - + "AND cg.goalMonth >= :currentMonth " + "ORDER BY cg.consumeAmount DESC limit :top") - List findTopConsumptionAndConsumeAmountLimit(@Param("top") int top, - @Param("peerAgeStart") int peerAgeStart, @Param("peerAgeEnd") int peerAgeEnd, - @Param("peerGender") Gender peerGender, @Param("currentMonth") LocalDate currentMonth); - - @Query("SELECT cg FROM ConsumptionGoal cg " + "WHERE cg.category.isDefault = true " - + "AND cg.user.age BETWEEN :peerAgeStart AND :peerAgeEnd " + "AND cg.user.gender = :peerGender " - + "AND cg.goalMonth >= :currentMonth " + "ORDER BY cg.consumeAmount DESC") - Page findTopConsumptionAndConsumeAmount(@Param("peerAgeStart") int peerAgeStart, - @Param("peerAgeEnd") int peerAgeEnd, @Param("peerGender") Gender peerGender, - @Param("currentMonth") LocalDate currentMonth, Pageable pageable); - @Query( "SELECT new com.bbteam.budgetbuddies.domain.consumptiongoal.dto.CategoryAvgConsumptionDTO(cg.category.id, AVG(cg.consumeAmount))" + "FROM ConsumptionGoal cg " + "WHERE cg.category.isDefault = true " From bfcabcdc3520793edf631dafdbb9eb3969b88574 Mon Sep 17 00:00:00 2001 From: MJJ Date: Mon, 12 Aug 2024 14:14:15 +0900 Subject: [PATCH 06/35] =?UTF-8?q?[refactor]=20=EB=98=90=EB=9E=98=EB=93=A4?= =?UTF-8?q?=EC=9D=B4=20=EA=B0=80=EC=9E=A5=20=EB=A7=8E=EC=9D=B4=20=EA=B3=84?= =?UTF-8?q?=ED=9A=8D=ED=95=9C=20=EC=B9=B4=ED=85=8C=EA=B3=A0=EB=A6=AC=20pag?= =?UTF-8?q?e=20=EC=A1=B0=ED=9A=8C=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ConsumptionGoalApi.java | 10 ---------- .../controller/ConsumptionGoalController.java | 19 +++---------------- 2 files changed, 3 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalApi.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalApi.java index 3b27bbed..1fb16b2d 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalApi.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalApi.java @@ -2,7 +2,6 @@ import java.time.LocalDate; -import org.springframework.data.domain.Pageable; import org.springframework.http.ResponseEntity; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalListRequestDto; @@ -26,15 +25,6 @@ public interface ConsumptionGoalApi { ResponseEntity getTopGoalCategoriesList(int top, Long userId, int peerAgeStart, int peerAgeEnd, String peerGender); - @Operation(summary = "또래들이 가장 큰 계획을 세운 카테고리 조회 API", description = "특정 사용자의 소비 목표 카테고리별 소비 목표 금액을 전체 조회하는 API 입니다.") - @ApiResponses(value = {@ApiResponse(responseCode = "COMMON200", description = "OK, 성공")}) - @Parameters({@Parameter(name = "userId", description = "로그인 한 유저 아이디"), - @Parameter(name = "peerAgeStart", description = "또래나이 시작 범위"), - @Parameter(name = "peerAgeEnd", description = "또래나이 끝 범위"), - @Parameter(name = "peerGender", description = "또래 성별")}) - ResponseEntity getTopGoalCategoriesPage(Long userId, - int peerAgeStart, int peerAgeEnd, String peerGender, Pageable pageable); - @Operation(summary = "또래가 가장 큰 계획을 세운 카테고리와 이번 주 사용한 금액 조회 API", description = "로그인 한 유저의 또래 중 가장 큰 소비 목표 금액을 가진 카테고리와 이번 주 사용한 금액을 조회하는 API 입니다") @ApiResponses(value = {@ApiResponse(responseCode = "COMMON200", description = "OK, 성공")}) @Parameters({@Parameter(name = "userId", description = "로그인 한 유저 아이디")}) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java index 30cb85b3..9e3a69fd 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java @@ -3,8 +3,6 @@ import java.time.LocalDate; import java.util.List; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; @@ -40,21 +38,10 @@ public ResponseEntity getTopGoalCategoriesList(@PathVariable(name = "top") in @RequestParam(name = "peerAgeEnd", defaultValue = "0") int peerAgeEnd, @RequestParam(name = "peerGender", defaultValue = "none") String peerGender) { List topCategoriesList = consumptionGoalService.getTopGoalCategoriesLimit(top, - userId, - peerAgeStart, peerAgeEnd, peerGender); + userId, peerAgeStart, peerAgeEnd, peerGender); return ResponseEntity.ok(topCategoriesList); } - @GetMapping("/top-categories/top-goal") - public ResponseEntity getTopGoalCategoriesPage(@RequestParam(name = "userId") Long userId, - @RequestParam(name = "peerAgeStart", defaultValue = "0") int peerAgeStart, - @RequestParam(name = "peerAgeEnd", defaultValue = "0") int peerAgeEnd, - @RequestParam(name = "peerGender", defaultValue = "none") String peerGender, Pageable pageable) { - Page topCategoriesPage = consumptionGoalService.getTopGoalCategories(userId, - peerAgeStart, peerAgeEnd, peerGender, pageable); - return ResponseEntity.ok(topCategoriesPage.getContent()); - } - @Override @GetMapping("/top-category/top-goal") public ResponseEntity getTopGoalCategory(@RequestParam(name = "userId") Long userId) { @@ -95,8 +82,8 @@ public ResponseEntity getConsumptionGoalList(@RequestParam(name = "userId") L @RequestParam(name = "peerAgeStart", defaultValue = "0") int peerAgeStart, @RequestParam(name = "peerAgeEnd", defaultValue = "0") int peerAgeEnd, @RequestParam(name = "peerGender", defaultValue = "none") String peerGender) { - List response = consumptionGoalService.getTopConsumptionsLimit(userId, - peerAgeStart, peerAgeEnd, peerGender); + List response = consumptionGoalService.getTopConsumptionsLimit(userId, peerAgeStart, + peerAgeEnd, peerGender); return ResponseEntity.ok(response); } } \ No newline at end of file From f50517fc6a60782172d4b6cd300358f2b025720e Mon Sep 17 00:00:00 2001 From: MJJ Date: Mon, 12 Aug 2024 14:16:41 +0900 Subject: [PATCH 07/35] =?UTF-8?q?[refactor]=20=EB=98=90=EB=9E=98=EB=93=A4?= =?UTF-8?q?=EC=9D=B4=20=EA=B0=80=EC=9E=A5=20=EB=A7=8E=EC=9D=B4=20=EA=B3=84?= =?UTF-8?q?=ED=9A=8D=ED=95=9C=20=EC=B9=B4=ED=85=8C=EA=B3=A0=EB=A6=AC=20pag?= =?UTF-8?q?e=20=EC=A1=B0=ED=9A=8C=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../consumptiongoal/service/ConsumptionGoalService.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java index 7799bba4..09333813 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java @@ -3,8 +3,6 @@ import java.time.LocalDate; import java.util.List; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionAnalysisResponseDTO; @@ -23,9 +21,6 @@ public interface ConsumptionGoalService { List getTopGoalCategoriesLimit(int top, Long userId, int peerAgeStart, int peerAgeEnd, String peerGender); - Page getTopGoalCategories(Long userId, int peerAgeStart, int peerAgeEnd, - String peerGender, Pageable pageable); - ConsumptionGoalResponseListDto findUserConsumptionGoalList(Long userId, LocalDate date); PeerInfoResponseDTO getPeerInfo(Long userId, int peerAgeStart, int peerAgeEnd, String peerGender); From 9fb32c0c03f3450dde265b7226f76a20a85a6d0c Mon Sep 17 00:00:00 2001 From: MJJ Date: Mon, 12 Aug 2024 14:18:59 +0900 Subject: [PATCH 08/35] =?UTF-8?q?[refactor]=20=EB=98=90=EB=9E=98=EC=9D=98?= =?UTF-8?q?=20=EC=86=8C=EB=B9=84=20=EC=B9=B4=ED=85=8C=EA=B3=A0=EB=A6=AC=20?= =?UTF-8?q?=EC=A0=84=EC=B2=B4=EC=A1=B0=ED=9A=8C=20DTO=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 소비 카테고리와 평균 소비 금액 DTO 추가 - 소비 카테고리와 내 소비 금액 DTO 추가 --- .../dto/CategoryAvgConsumptionDTO.java | 19 +++++++++++++++++++ .../consumptiongoal/dto/MyConsumptionDTO.java | 14 ++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/CategoryAvgConsumptionDTO.java create mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/MyConsumptionDTO.java diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/CategoryAvgConsumptionDTO.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/CategoryAvgConsumptionDTO.java new file mode 100644 index 00000000..ba07fa01 --- /dev/null +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/CategoryAvgConsumptionDTO.java @@ -0,0 +1,19 @@ +package com.bbteam.budgetbuddies.domain.consumptiongoal.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@AllArgsConstructor +@NoArgsConstructor +public class CategoryAvgConsumptionDTO { + + private Long categoryId; + private Long averageConsumeAmount; // Change to Long + + public CategoryAvgConsumptionDTO(Long categoryId, Double averageConsumeAmount) { + this.categoryId = categoryId; + this.averageConsumeAmount = (averageConsumeAmount != null) ? averageConsumeAmount.longValue() : 0L; + } +} \ No newline at end of file diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/MyConsumptionDTO.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/MyConsumptionDTO.java new file mode 100644 index 00000000..b0edad4b --- /dev/null +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/MyConsumptionDTO.java @@ -0,0 +1,14 @@ +package com.bbteam.budgetbuddies.domain.consumptiongoal.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@AllArgsConstructor +@NoArgsConstructor +public class MyConsumptionDTO { + + private Long categoryId; + private Long ConsumeAmount; // Change to Long +} \ No newline at end of file From 0238ace3110ea88fa435aa81ad7a5bd84af98401 Mon Sep 17 00:00:00 2001 From: MJJ Date: Mon, 12 Aug 2024 14:30:14 +0900 Subject: [PATCH 09/35] =?UTF-8?q?[refactor]=20=ED=8A=B9=EC=A0=95=20userId?= =?UTF-8?q?=EC=9D=98=20=EC=B9=B4=ED=85=8C=EA=B3=A0=EB=A6=AC=20=EB=B3=84=20?= =?UTF-8?q?=EC=B4=9D=20=EC=86=8C=EB=B9=84=20=EA=B8=88=EC=95=A1=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20repository=20=EA=B5=AC=ED=98=84,=20page=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/ConsumptionGoalRepository.java | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java index 6ad22365..ebc77e16 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java @@ -4,8 +4,6 @@ import java.util.List; import java.util.Optional; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -13,6 +11,7 @@ import com.bbteam.budgetbuddies.domain.category.entity.Category; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.CategoryAvgConsumptionDTO; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.MyConsumptionDTO; import com.bbteam.budgetbuddies.domain.consumptiongoal.entity.ConsumptionGoal; import com.bbteam.budgetbuddies.domain.user.entity.User; import com.bbteam.budgetbuddies.enums.Gender; @@ -27,13 +26,6 @@ List findTopCategoriesAndGoalAmountLimit(@Param("top") int top, @Param("peerAgeStart") int peerAgeStart, @Param("peerAgeEnd") int peerAgeEnd, @Param("peerGender") Gender peerGender, @Param("currentMonth") LocalDate currentMonth); - @Query("SELECT cg FROM ConsumptionGoal cg " + "WHERE cg.category.isDefault = true " - + "AND cg.user.age BETWEEN :peerAgeStart AND :peerAgeEnd " + "AND cg.user.gender = :peerGender " - + "AND cg.goalMonth >= :currentMonth " + "ORDER BY cg.goalAmount DESC") - Page findTopCategoriesAndGoalAmount(@Param("peerAgeStart") int peerAgeStart, - @Param("peerAgeEnd") int peerAgeEnd, @Param("peerGender") Gender peerGender, - @Param("currentMonth") LocalDate currentMonth, Pageable pageable); - @Query(value = "SELECT cg FROM ConsumptionGoal AS cg WHERE cg.user.id = :userId AND cg.goalMonth = :goalMonth") List findConsumptionGoalByUserIdAndGoalMonth(Long userId, LocalDate goalMonth); @@ -56,4 +48,9 @@ List findAvgConsumptionByCategory( @Param("peerGender") Gender peerGender, @Param("currentMonth") LocalDate currentMonth); + @Query("SELECT new com.bbteam.budgetbuddies.domain.consumptiongoal.dto.MyConsumptionDTO(" + + "cg.category.id, SUM(cg.consumeAmount)) " + "FROM ConsumptionGoal cg " + + "WHERE cg.category.isDefault = true " + "AND cg.user.id = :userId " + + "GROUP BY cg.category.id " + "ORDER BY cg.category.id") + List findAllConsumptionAmountByUserId(@Param("userId") Long userId); } From a52c4cb774b2953ef2f571b882e3538fd15a9351 Mon Sep 17 00:00:00 2001 From: MJJ Date: Mon, 12 Aug 2024 14:47:48 +0900 Subject: [PATCH 10/35] =?UTF-8?q?[refactor]=20=EC=86=8C=EB=B9=84=20?= =?UTF-8?q?=EB=A0=88=ED=8F=AC=ED=8A=B8=20=EC=A0=84=EC=B2=B4=EB=B3=B4?= =?UTF-8?q?=EA=B8=B0=20service=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/ConsumptionGoalServiceImpl.java | 221 ++++++++++-------- 1 file changed, 130 insertions(+), 91 deletions(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java index f7caa290..a5ac4c28 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java @@ -8,10 +8,9 @@ import java.util.Map; import java.util.NoSuchElementException; import java.util.Optional; +import java.util.function.Function; import java.util.stream.Collectors; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -21,12 +20,13 @@ import com.bbteam.budgetbuddies.domain.consumptiongoal.converter.ConsumptionGoalConverter; import com.bbteam.budgetbuddies.domain.consumptiongoal.converter.PeerInfoConverter; import com.bbteam.budgetbuddies.domain.consumptiongoal.converter.TopCategoryConverter; -import com.bbteam.budgetbuddies.domain.consumptiongoal.converter.TopConsumptionConverter; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.CategoryAvgConsumptionDTO; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionAnalysisResponseDTO; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalListRequestDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalRequestDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseListDto; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.MyConsumptionDTO; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.PeerInfoResponseDTO; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopConsumptionResponseDTO; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDTO; @@ -63,27 +63,15 @@ public class ConsumptionGoalServiceImpl implements ConsumptionGoalService { @Override @Transactional(readOnly = true) public List getTopGoalCategoriesLimit(int top, Long userId, int peerAgeS, int peerAgeE, - String peerG) { + String peerG) { checkPeerInfo(userId, peerAgeS, peerAgeE, peerG); List topGoals = consumptionGoalRepository.findTopCategoriesAndGoalAmountLimit(top, - peerAgeStart, peerAgeEnd, peerGender, currentMonth); + peerAgeStart, peerAgeEnd, peerGender, currentMonth); return topGoals.stream().map(TopCategoryConverter::fromEntity).collect(Collectors.toList()); } - @Override - @Transactional(readOnly = true) - public Page getTopGoalCategories(Long userId, int peerAgeS, int peerAgeE, String peerG, - Pageable pageable) { - - checkPeerInfo(userId, peerAgeS, peerAgeE, peerG); - - Page topGoals = consumptionGoalRepository.findTopCategoriesAndGoalAmount(peerAgeStart, - peerAgeEnd, peerGender, currentMonth, pageable); - return topGoals.map(TopCategoryConverter::fromEntity); - } - @Override @Transactional(readOnly = true) public PeerInfoResponseDTO getPeerInfo(Long userId, int peerAgeS, int peerAgeE, String peerG) { @@ -100,12 +88,12 @@ public ConsumptionAnalysisResponseDTO getTopCategoryAndConsumptionAmount(Long us checkPeerInfo(userId, 0, 0, "none"); ConsumptionGoal topConsumptionGoal = consumptionGoalRepository.findTopCategoriesAndGoalAmountLimit(1, - peerAgeStart, peerAgeEnd, peerGender, currentMonth).get(0); + peerAgeStart, peerAgeEnd, peerGender, currentMonth).get(0); ConsumptionGoal currentWeekConsumptionAmount = consumptionGoalRepository.findTopConsumptionByCategoryIdAndCurrentWeek( - topConsumptionGoal.getCategory().getId(), startOfWeek, endOfWeek) - .orElseThrow(() -> new IllegalArgumentException( - "카테고리 ID " + topConsumptionGoal.getCategory().getId() + "에 대한 현재 주 소비 데이터가 없습니다.")); + topConsumptionGoal.getCategory().getId(), startOfWeek, endOfWeek) + .orElseThrow(() -> new IllegalArgumentException( + "카테고리 ID " + topConsumptionGoal.getCategory().getId() + "에 대한 현재 주 소비 데이터가 없습니다.")); Long totalConsumptionAmountForCurrentWeek = currentWeekConsumptionAmount.getConsumeAmount(); @@ -113,25 +101,47 @@ public ConsumptionAnalysisResponseDTO getTopCategoryAndConsumptionAmount(Long us } @Override - public List getTopConsumptionsLimit(int top, Long userId, int peerAgeS, int peerAgeE, - String peerG) { - - checkPeerInfo(userId, peerAgeS, peerAgeE, peerG); - - List topConsumptions = consumptionGoalRepository.findTopConsumptionAndConsumeAmountLimit(top, - peerAgeStart, peerAgeEnd, peerGender, currentMonth); - return topConsumptions.stream().map(TopConsumptionConverter::fromEntity).collect(Collectors.toList()); - } - - @Override - public Page getTopConsumptions(Long userId, int peerAgeS, int peerAgeE, String peerG, - Pageable pageable) { + @Transactional(readOnly = true) + public List getTopConsumptionsLimit(Long userId, int peerAgeS, int peerAgeE, + String peerG) { checkPeerInfo(userId, peerAgeS, peerAgeE, peerG); - Page topConsumptions = consumptionGoalRepository.findTopConsumptionAndConsumeAmount( - peerAgeStart, peerAgeEnd, peerGender, currentMonth, pageable); - return topConsumptions.map(TopConsumptionConverter::fromEntity); + List categoryAvgList = getAvgGoalAmount(); + + List myConsumptionAmountList = getMyConsumptionAmount(userId); + + List defaultCategories = categoryRepository.findAllByIsDefaultTrue(); + + return defaultCategories.stream() + .map(category -> { + MyConsumptionDTO myConsumptionAmountDTO = myConsumptionAmountList.stream() + .filter(dto -> dto.getCategoryId().equals(category.getId())) + .findFirst() + .orElse(new MyConsumptionDTO(category.getId(), 0L)); // 없을 경우 0L 설정 + + CategoryAvgConsumptionDTO avgDTO = categoryAvgList.stream() + .filter(dto -> dto.getCategoryId().equals(category.getId())) + .findFirst() + .orElse(new CategoryAvgConsumptionDTO(category.getId(), 0L)); + + Long avgConsumeAmount = avgDTO.getAverageConsumeAmount(); + Long myConsumeAmount = myConsumptionAmountDTO.getConsumeAmount(); + Long consumeAmountDifference; + + if (avgConsumeAmount == 0L) { + consumeAmountDifference = -myConsumeAmount; + } else { + consumeAmountDifference = myConsumeAmount - avgConsumeAmount; + } + + return TopConsumptionResponseDTO.builder() + .categoryName(category.getName()) + .avgConsumeAmount(avgConsumeAmount) + .consumeAmountDifference(consumeAmountDifference) + .build(); + }) + .collect(Collectors.toList()); } private User findUserById(Long userId) { @@ -179,31 +189,71 @@ private void setAgeGroupByUser(int userAge) { } } + private List getAvgGoalAmount() { + + List defaultCategories = categoryRepository.findAllByIsDefaultTrue(); + List categoryAvgList = new ArrayList<>(); + + List categoryAverageGoalDTOs = consumptionGoalRepository.findAvgConsumptionByCategory( + peerAgeStart, peerAgeEnd, peerGender, currentMonth); + + Map categoryAvgMap = categoryAverageGoalDTOs.stream() + .collect(Collectors.toMap(CategoryAvgConsumptionDTO::getCategoryId, Function.identity())); + + for (Category category : defaultCategories) { + CategoryAvgConsumptionDTO avgDTO = categoryAvgMap.getOrDefault(category.getId(), + new CategoryAvgConsumptionDTO(category.getId(), 0.0)); + + categoryAvgList.add(avgDTO); + } + return categoryAvgList; + } + + private List getMyConsumptionAmount(Long userId) { + + List defaultCategories = categoryRepository.findAllByIsDefaultTrue(); + List myConsumptionAmountList = new ArrayList<>(); + + List myConsumptionAmountDTOs = consumptionGoalRepository.findAllConsumptionAmountByUserId( + userId); + + Map myConsumptionMap = myConsumptionAmountDTOs.stream() + .collect(Collectors.toMap(MyConsumptionDTO::getCategoryId, Function.identity())); + + for (Category category : defaultCategories) { + MyConsumptionDTO myConsumptionDTO = myConsumptionMap.getOrDefault(category.getId(), + new MyConsumptionDTO(category.getId(), 0L)); + + myConsumptionAmountList.add(myConsumptionDTO); + } + return myConsumptionAmountList; + } + @Override @Transactional public ConsumptionGoalResponseListDto updateConsumptionGoals(Long userId, - ConsumptionGoalListRequestDto consumptionGoalListRequestDto) { + ConsumptionGoalListRequestDto consumptionGoalListRequestDto) { LocalDate thisMonth = LocalDate.now().withDayOfMonth(1); User user = userRepository.findById(userId).orElseThrow(() -> new IllegalArgumentException("Not found user")); List updatedConsumptionGoal = consumptionGoalListRequestDto.getConsumptionGoalList() - .stream() - .map(c -> updateConsumptionGoalWithRequestDto(user, c, thisMonth)) - .toList(); + .stream() + .map(c -> updateConsumptionGoalWithRequestDto(user, c, thisMonth)) + .toList(); List response = consumptionGoalRepository.saveAll(updatedConsumptionGoal) - .stream() - .map(consumptionGoalConverter::toConsumptionGoalResponseDto) - .toList(); + .stream() + .map(consumptionGoalConverter::toConsumptionGoalResponseDto) + .toList(); return consumptionGoalConverter.toConsumptionGoalResponseListDto(response, thisMonth); } private ConsumptionGoal updateConsumptionGoalWithRequestDto(User user, - ConsumptionGoalRequestDto consumptionGoalRequestDto, LocalDate goalMonth) { + ConsumptionGoalRequestDto consumptionGoalRequestDto, LocalDate goalMonth) { Category category = categoryRepository.findById(consumptionGoalRequestDto.getCategoryId()) - .orElseThrow(() -> new IllegalArgumentException("Not found Category")); + .orElseThrow(() -> new IllegalArgumentException("Not found Category")); ConsumptionGoal consumptionGoal = findOrElseGenerateConsumptionGoal(user, category, goalMonth); consumptionGoal.updateGoalAmount(consumptionGoalRequestDto.getGoalAmount()); @@ -213,17 +263,17 @@ private ConsumptionGoal updateConsumptionGoalWithRequestDto(User user, private ConsumptionGoal findOrElseGenerateConsumptionGoal(User user, Category category, LocalDate goalMonth) { return consumptionGoalRepository.findConsumptionGoalByUserAndCategoryAndGoalMonth(user, category, goalMonth) - .orElseGet(() -> generateNewConsumptionGoal(user, category, goalMonth)); + .orElseGet(() -> generateNewConsumptionGoal(user, category, goalMonth)); } private ConsumptionGoal generateNewConsumptionGoal(User user, Category category, LocalDate goalMonth) { return ConsumptionGoal.builder() - .goalMonth(goalMonth) - .user(user) - .category(category) - .consumeAmount(0L) - .goalAmount(0L) - .build(); + .goalMonth(goalMonth) + .user(user) + .category(category) + .consumeAmount(0L) + .goalAmount(0L) + .build(); } @Override @@ -240,25 +290,25 @@ public ConsumptionGoalResponseListDto findUserConsumptionGoalList(Long userId, L private Map initializeGoalMap(Long userId) { return categoryRepository.findUserCategoryByUserId(userId) - .stream() - .collect(Collectors.toMap(Category::getId, consumptionGoalConverter::toConsumptionGoalResponseDto)); + .stream() + .collect(Collectors.toMap(Category::getId, consumptionGoalConverter::toConsumptionGoalResponseDto)); } private void updateGoalMapWithPreviousMonth(Long userId, LocalDate goalMonth, - Map goalMap) { + Map goalMap) { updateGoalMap(userId, goalMonth.minusMonths(1), goalMap); } private void updateGoalMapWithCurrentMonth(Long userId, LocalDate goalMonth, - Map goalMap) { + Map goalMap) { updateGoalMap(userId, goalMonth, goalMap); } private void updateGoalMap(Long userId, LocalDate month, Map goalMap) { consumptionGoalRepository.findConsumptionGoalByUserIdAndGoalMonth(userId, month) - .stream() - .map(consumptionGoalConverter::toConsumptionGoalResponseDto) - .forEach(goal -> goalMap.put(goal.getCategoryId(), goal)); + .stream() + .map(consumptionGoalConverter::toConsumptionGoalResponseDto) + .forEach(goal -> goalMap.put(goal.getCategoryId(), goal)); } @Override @@ -270,8 +320,8 @@ public void recalculateConsumptionAmount(Expense expense, ExpenseUpdateRequestDt private void restorePreviousGoalConsumptionAmount(Expense expense, User user) { ConsumptionGoal previousConsumptionGoal = consumptionGoalRepository.findConsumptionGoalByUserAndCategoryAndGoalMonth( - user, expense.getCategory(), expense.getExpenseDate().toLocalDate().withDayOfMonth(1)) - .orElseThrow(() -> new IllegalArgumentException("Not found consumptionGoal")); + user, expense.getCategory(), expense.getExpenseDate().toLocalDate().withDayOfMonth(1)) + .orElseThrow(() -> new IllegalArgumentException("Not found consumptionGoal")); previousConsumptionGoal.restoreConsumeAmount(expense.getAmount()); consumptionGoalRepository.save(previousConsumptionGoal); @@ -279,12 +329,12 @@ private void restorePreviousGoalConsumptionAmount(Expense expense, User user) { private void calculatePresentGoalConsumptionAmount(ExpenseUpdateRequestDto request, User user) { Category categoryToReplace = categoryRepository.findById(request.getCategoryId()) - .orElseThrow(() -> new IllegalArgumentException("Not found category")); + .orElseThrow(() -> new IllegalArgumentException("Not found category")); ConsumptionGoal consumptionGoal = consumptionGoalRepository.findConsumptionGoalByUserAndCategoryAndGoalMonth( - user, categoryToReplace, request.getExpenseDate().toLocalDate().withDayOfMonth(1)) - .orElseGet(() -> this.generateGoalByPreviousOrElseNew(user, categoryToReplace, - request.getExpenseDate().toLocalDate().withDayOfMonth(1))); + user, categoryToReplace, request.getExpenseDate().toLocalDate().withDayOfMonth(1)) + .orElseGet(() -> this.generateGoalByPreviousOrElseNew(user, categoryToReplace, + request.getExpenseDate().toLocalDate().withDayOfMonth(1))); consumptionGoal.updateConsumeAmount(request.getAmount()); consumptionGoalRepository.save(consumptionGoal); @@ -294,18 +344,18 @@ private ConsumptionGoal generateGoalByPreviousOrElseNew(User user, Category cate LocalDate previousMonth = goalMonth.minusMonths(1); return consumptionGoalRepository.findConsumptionGoalByUserAndCategoryAndGoalMonth(user, category, previousMonth) - .map(this::generateGoalByPrevious) - .orElseGet(() -> generateNewConsumptionGoal(user, category, goalMonth)); + .map(this::generateGoalByPrevious) + .orElseGet(() -> generateNewConsumptionGoal(user, category, goalMonth)); } private ConsumptionGoal generateGoalByPrevious(ConsumptionGoal consumptionGoal) { return ConsumptionGoal.builder() - .goalMonth(consumptionGoal.getGoalMonth().plusMonths(1)) - .user(consumptionGoal.getUser()) - .category(consumptionGoal.getCategory()) - .consumeAmount(0L) - .goalAmount(consumptionGoal.getGoalAmount()) - .build(); + .goalMonth(consumptionGoal.getGoalMonth().plusMonths(1)) + .user(consumptionGoal.getUser()) + .category(consumptionGoal.getCategory()) + .consumeAmount(0L) + .goalAmount(consumptionGoal.getGoalAmount()) + .build(); } @Override @@ -313,39 +363,28 @@ public void updateConsumeAmount(Long userId, Long categoryId, Long amount) { User user = userRepository.findById(userId).orElseThrow(() -> new IllegalArgumentException("Not found user")); Category category = categoryRepository.findById(categoryId) - .orElseThrow(() -> new IllegalArgumentException("Not found Category")); + .orElseThrow(() -> new IllegalArgumentException("Not found Category")); LocalDate thisMonth = LocalDate.now().withDayOfMonth(1); ConsumptionGoal consumptionGoal = consumptionGoalRepository.findConsumptionGoalByUserAndCategoryAndGoalMonth( - user, category, thisMonth).orElseGet(() -> generateNewConsumptionGoal(user, category, thisMonth)); + user, category, thisMonth).orElseGet(() -> generateNewConsumptionGoal(user, category, thisMonth)); consumptionGoal.updateConsumeAmount(amount); consumptionGoalRepository.save(consumptionGoal); } - @Override - public List getTopConsumption(int top, Long userId, int peerAgeS, int peerAgeE, - String peerG) { - - checkPeerInfo(userId, peerAgeS, peerAgeE, peerG); - - List topConsumptions = consumptionGoalRepository.findTopConsumptionAndConsumeAmount(top, - peerAgeStart, peerAgeEnd, peerGender); - return topConsumptions.stream().map(TopConsumptionConverter::fromEntity).collect(Collectors.toList()); - } - @Override public void decreaseConsumeAmount(Long userId, Long categoryId, Long amount, LocalDate expenseDate) { User user = userRepository.findById(userId) - .orElseThrow(() -> new IllegalArgumentException("Not found user")); + .orElseThrow(() -> new IllegalArgumentException("Not found user")); Category category = categoryRepository.findById(categoryId) - .orElseThrow(() -> new IllegalArgumentException("Not found Category")); + .orElseThrow(() -> new IllegalArgumentException("Not found Category")); LocalDate goalMonth = expenseDate.withDayOfMonth(1); ConsumptionGoal consumptionGoal = consumptionGoalRepository - .findConsumptionGoalByUserAndCategoryAndGoalMonth(user, category, goalMonth) - .orElseThrow(() -> new IllegalArgumentException("Not found ConsumptionGoal")); + .findConsumptionGoalByUserAndCategoryAndGoalMonth(user, category, goalMonth) + .orElseThrow(() -> new IllegalArgumentException("Not found ConsumptionGoal")); consumptionGoal.decreaseConsumeAmount(amount); consumptionGoalRepository.save(consumptionGoal); From 6d86dd06b52a6d583fa7bc19f5d20f7ea0b1cb7a Mon Sep 17 00:00:00 2001 From: MJJ Date: Mon, 12 Aug 2024 14:51:27 +0900 Subject: [PATCH 11/35] =?UTF-8?q?[refactor]=20=EC=86=8C=EB=B9=84=20?= =?UTF-8?q?=EB=A0=88=ED=8F=AC=ED=8A=B8=20=EC=A0=84=EC=B2=B4=EB=B3=B4?= =?UTF-8?q?=EA=B8=B0=20=EB=A9=94=EC=84=9C=EB=93=9C=EB=AA=85=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../consumptiongoal/controller/ConsumptionGoalApi.java | 2 +- .../controller/ConsumptionGoalController.java | 5 +++-- .../consumptiongoal/service/ConsumptionGoalService.java | 2 +- .../consumptiongoal/service/ConsumptionGoalServiceImpl.java | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalApi.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalApi.java index 1fb16b2d..d9041f19 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalApi.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalApi.java @@ -52,6 +52,6 @@ ResponseEntity updateOrElseGenerateConsumptionGo @Parameter(name = "peerAgeStart", description = "또래나이 시작 범위"), @Parameter(name = "peerAgeEnd", description = "또래나이 끝 범위"), @Parameter(name = "peerGender", description = "또래 성별")}) - ResponseEntity getConsumptionGoalList(Long userId, int peerAgeStart, int peerAgeEnd, + ResponseEntity getAllConsumptionCategories(Long userId, int peerAgeStart, int peerAgeEnd, String peerGender); } \ No newline at end of file diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java index 9e3a69fd..85722d89 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java @@ -78,11 +78,12 @@ public ResponseEntity updateOrElseGenerateConsum } @GetMapping("/top-categories/top-consumption") - public ResponseEntity getConsumptionGoalList(@RequestParam(name = "userId") Long userId, + public ResponseEntity getAllConsumptionCategories(@RequestParam(name = "userId") Long userId, @RequestParam(name = "peerAgeStart", defaultValue = "0") int peerAgeStart, @RequestParam(name = "peerAgeEnd", defaultValue = "0") int peerAgeEnd, @RequestParam(name = "peerGender", defaultValue = "none") String peerGender) { - List response = consumptionGoalService.getTopConsumptionsLimit(userId, peerAgeStart, + List response = consumptionGoalService.getAllConsumptionCategories(userId, + peerAgeStart, peerAgeEnd, peerGender); return ResponseEntity.ok(response); } diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java index 09333813..952edc35 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java @@ -36,6 +36,6 @@ ConsumptionGoalResponseListDto updateConsumptionGoals(Long userId, void decreaseConsumeAmount(Long userId, Long categoryId, Long amount, LocalDate expenseDate); - List getTopConsumptionsLimit(Long userId, int peerAgeS, int peerAgeE, + List getAllConsumptionCategories(Long userId, int peerAgeS, int peerAgeE, String peerG); } diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java index a5ac4c28..c27b94ee 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java @@ -102,7 +102,7 @@ public ConsumptionAnalysisResponseDTO getTopCategoryAndConsumptionAmount(Long us @Override @Transactional(readOnly = true) - public List getTopConsumptionsLimit(Long userId, int peerAgeS, int peerAgeE, + public List getAllConsumptionCategories(Long userId, int peerAgeS, int peerAgeE, String peerG) { checkPeerInfo(userId, peerAgeS, peerAgeE, peerG); From fbca62f132ad79345d4fec7d729801905de6ac90 Mon Sep 17 00:00:00 2001 From: MJJ Date: Mon, 12 Aug 2024 15:21:43 +0900 Subject: [PATCH 12/35] =?UTF-8?q?[refactor]=20=EB=98=90=EB=9E=98=EB=93=A4?= =?UTF-8?q?=EC=9D=B4=20=EA=B3=84=ED=9A=8D=ED=95=9C=20=EC=B9=B4=ED=85=8C?= =?UTF-8?q?=EA=B3=A0=EB=A6=AC=EC=99=80=20=EB=AA=A9=ED=91=9C=20=EA=B8=88?= =?UTF-8?q?=EC=95=A1=20=EC=A0=84=EC=B2=B4=EB=B3=B4=EA=B8=B0=20Controller?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ConsumptionGoalApi.java | 10 +++++++--- .../controller/ConsumptionGoalController.java | 13 ++++++++----- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalApi.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalApi.java index d9041f19..a2a6fbab 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalApi.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalApi.java @@ -25,10 +25,14 @@ public interface ConsumptionGoalApi { ResponseEntity getTopGoalCategoriesList(int top, Long userId, int peerAgeStart, int peerAgeEnd, String peerGender); - @Operation(summary = "또래가 가장 큰 계획을 세운 카테고리와 이번 주 사용한 금액 조회 API", description = "로그인 한 유저의 또래 중 가장 큰 소비 목표 금액을 가진 카테고리와 이번 주 사용한 금액을 조회하는 API 입니다") + @Operation(summary = "또래들이 가장 많이 계획한 카테고리와 평균 금액 및 내 목표금액 차이 조회 API", description = "특정 사용자의 또래 소비 카테고리별 평균 목표 금액을 조회하는 API 입니다.") @ApiResponses(value = {@ApiResponse(responseCode = "COMMON200", description = "OK, 성공")}) - @Parameters({@Parameter(name = "userId", description = "로그인 한 유저 아이디")}) - ResponseEntity getTopGoalCategory(Long userId); + @Parameters({@Parameter(name = "userId", description = "로그인 한 유저 아이디"), + @Parameter(name = "peerAgeStart", description = "또래나이 시작 범위"), + @Parameter(name = "peerAgeEnd", description = "또래나이 끝 범위"), + @Parameter(name = "peerGender", description = "또래 성별")}) + ResponseEntity getAllConsumptionGoalCategories(Long userId, int peerAgeStart, int peerAgeEnd, + String peerGender); @Operation(summary = "또래나이와 성별 조회 API", description = "또래나이와 성별을 조회하는 API 입니다.") @ApiResponses(value = {@ApiResponse(responseCode = "COMMON200", description = "OK, 성공")}) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java index 85722d89..592a5491 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java @@ -13,7 +13,6 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionAnalysisResponseDTO; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalListRequestDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseListDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.PeerInfoResponseDTO; @@ -42,10 +41,14 @@ public ResponseEntity getTopGoalCategoriesList(@PathVariable(name = "top") in return ResponseEntity.ok(topCategoriesList); } - @Override - @GetMapping("/top-category/top-goal") - public ResponseEntity getTopGoalCategory(@RequestParam(name = "userId") Long userId) { - ConsumptionAnalysisResponseDTO response = consumptionGoalService.getTopCategoryAndConsumptionAmount(userId); + @GetMapping("/top-categories/top-goal") + public ResponseEntity getAllConsumptionGoalCategories(@RequestParam(name = "userId") Long userId, + @RequestParam(name = "peerAgeStart", defaultValue = "0") int peerAgeStart, + @RequestParam(name = "peerAgeEnd", defaultValue = "0") int peerAgeEnd, + @RequestParam(name = "peerGender", defaultValue = "none") String peerGender) { + List response = consumptionGoalService.getAllConsumptionGoalCategories(userId, + peerAgeStart, + peerAgeEnd, peerGender); return ResponseEntity.ok(response); } From 9ac6e062e2c829bbb5909bb9b9f2f543b18ccf7d Mon Sep 17 00:00:00 2001 From: MJJ Date: Mon, 12 Aug 2024 15:27:55 +0900 Subject: [PATCH 13/35] =?UTF-8?q?[refactor]=20=EC=86=8C=EB=B9=84=20?= =?UTF-8?q?=EB=A0=88=ED=8F=AC=ED=8A=B8=20=EC=A0=84=EC=B2=B4=EB=B3=B4?= =?UTF-8?q?=EA=B8=B0=20DTO=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit dto 재사용을 위해 수정 - 평균 소비 금액 -> 평균 금액 - 내 소비 금액 -> 내 금액 --- .../dto/AvgConsumptionGoalDTO.java | 19 +++++++++++++++++++ .../dto/CategoryAvgConsumptionDTO.java | 19 ------------------- ...tionDTO.java => MyConsumptionGoalDTO.java} | 4 ++-- 3 files changed, 21 insertions(+), 21 deletions(-) create mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/AvgConsumptionGoalDTO.java delete mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/CategoryAvgConsumptionDTO.java rename src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/{MyConsumptionDTO.java => MyConsumptionGoalDTO.java} (74%) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/AvgConsumptionGoalDTO.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/AvgConsumptionGoalDTO.java new file mode 100644 index 00000000..249ace6f --- /dev/null +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/AvgConsumptionGoalDTO.java @@ -0,0 +1,19 @@ +package com.bbteam.budgetbuddies.domain.consumptiongoal.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@AllArgsConstructor +@NoArgsConstructor +public class AvgConsumptionGoalDTO { + + private Long categoryId; + private Long averageAmount; + + public AvgConsumptionGoalDTO(Long categoryId, Double averageAmount) { + this.categoryId = categoryId; + this.averageAmount = (averageAmount != null) ? averageAmount.longValue() : 0L; + } +} \ No newline at end of file diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/CategoryAvgConsumptionDTO.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/CategoryAvgConsumptionDTO.java deleted file mode 100644 index ba07fa01..00000000 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/CategoryAvgConsumptionDTO.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.bbteam.budgetbuddies.domain.consumptiongoal.dto; - -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@Getter -@AllArgsConstructor -@NoArgsConstructor -public class CategoryAvgConsumptionDTO { - - private Long categoryId; - private Long averageConsumeAmount; // Change to Long - - public CategoryAvgConsumptionDTO(Long categoryId, Double averageConsumeAmount) { - this.categoryId = categoryId; - this.averageConsumeAmount = (averageConsumeAmount != null) ? averageConsumeAmount.longValue() : 0L; - } -} \ No newline at end of file diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/MyConsumptionDTO.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/MyConsumptionGoalDTO.java similarity index 74% rename from src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/MyConsumptionDTO.java rename to src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/MyConsumptionGoalDTO.java index b0edad4b..10cc56d3 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/MyConsumptionDTO.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/MyConsumptionGoalDTO.java @@ -7,8 +7,8 @@ @Getter @AllArgsConstructor @NoArgsConstructor -public class MyConsumptionDTO { +public class MyConsumptionGoalDTO { private Long categoryId; - private Long ConsumeAmount; // Change to Long + private Long MyAmount; } \ No newline at end of file From 3aad802b82c56605aad32aa9c4209601bd87ca1e Mon Sep 17 00:00:00 2001 From: MJJ Date: Mon, 12 Aug 2024 15:29:51 +0900 Subject: [PATCH 14/35] =?UTF-8?q?[refactor]=20=EA=B0=80=EC=9E=A5=20?= =?UTF-8?q?=EB=A7=8E=EC=9D=B4=20=EA=B3=84=ED=9A=8D=ED=95=9C=20=EC=B9=B4?= =?UTF-8?q?=ED=85=8C=EA=B3=A0=EB=A6=AC=20=EB=B0=8F=20=ED=8F=89=EA=B7=A0=20?= =?UTF-8?q?=EB=AA=A9=ED=91=9C=EA=B8=88=EC=95=A1=20=EC=A1=B0=ED=9A=8C=20rep?= =?UTF-8?q?ository=20=EA=B5=AC=ED=98=84,=20dto=EB=AA=85=20=EC=88=98?= =?UTF-8?q?=EC=A0=95,=20=EB=A9=94=EC=84=9C=EB=93=9C=EB=AA=85=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/ConsumptionGoalRepository.java | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java index ebc77e16..5f54a716 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java @@ -10,8 +10,8 @@ import org.springframework.stereotype.Repository; import com.bbteam.budgetbuddies.domain.category.entity.Category; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.CategoryAvgConsumptionDTO; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.MyConsumptionDTO; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AvgConsumptionGoalDTO; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.MyConsumptionGoalDTO; import com.bbteam.budgetbuddies.domain.consumptiongoal.entity.ConsumptionGoal; import com.bbteam.budgetbuddies.domain.user.entity.User; import com.bbteam.budgetbuddies.enums.Gender; @@ -38,19 +38,36 @@ Optional findTopConsumptionByCategoryIdAndCurrentWeek(@Param("c @Param("startOfWeek") LocalDate startOfWeek, @Param("endOfWeek") LocalDate endOfWeek); @Query( - "SELECT new com.bbteam.budgetbuddies.domain.consumptiongoal.dto.CategoryAvgConsumptionDTO(cg.category.id, AVG(cg.consumeAmount))" + "SELECT new com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AvgConsumptionGoalDTO(cg.category.id, AVG(cg.consumeAmount))" + "FROM ConsumptionGoal cg " + "WHERE cg.category.isDefault = true " + "AND cg.user.age BETWEEN :peerAgeStart AND :peerAgeEnd " + "AND cg.user.gender = :peerGender " + "AND cg.goalMonth >= :currentMonth " + "GROUP BY cg.category.id " + "ORDER BY AVG(cg.consumeAmount) DESC") - List findAvgConsumptionByCategory( + List findAvgConsumptionAmountByCategory( @Param("peerAgeStart") int peerAgeStart, @Param("peerAgeEnd") int peerAgeEnd, @Param("peerGender") Gender peerGender, @Param("currentMonth") LocalDate currentMonth); - @Query("SELECT new com.bbteam.budgetbuddies.domain.consumptiongoal.dto.MyConsumptionDTO(" + @Query("SELECT new com.bbteam.budgetbuddies.domain.consumptiongoal.dto.MyConsumptionGoalDTO(" + "cg.category.id, SUM(cg.consumeAmount)) " + "FROM ConsumptionGoal cg " + "WHERE cg.category.isDefault = true " + "AND cg.user.id = :userId " + "GROUP BY cg.category.id " + "ORDER BY cg.category.id") - List findAllConsumptionAmountByUserId(@Param("userId") Long userId); + List findAllConsumptionAmountByUserId(@Param("userId") Long userId); + + @Query( + "SELECT new com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AvgConsumptionGoalDTO(cg.category.id, AVG(cg.goalAmount))" + + "FROM ConsumptionGoal cg " + "WHERE cg.category.isDefault = true " + + "AND cg.user.age BETWEEN :peerAgeStart AND :peerAgeEnd " + "AND cg.user.gender = :peerGender " + + "AND cg.goalMonth >= :currentMonth " + "GROUP BY cg.category.id " + "ORDER BY AVG(cg.goalAmount) DESC") + List findAvgGoalAmountByCategory( + @Param("peerAgeStart") int peerAgeStart, + @Param("peerAgeEnd") int peerAgeEnd, + @Param("peerGender") Gender peerGender, + @Param("currentMonth") LocalDate currentMonth); + + @Query("SELECT new com.bbteam.budgetbuddies.domain.consumptiongoal.dto.MyConsumptionGoalDTO(" + + "cg.category.id, SUM(cg.goalAmount)) " + "FROM ConsumptionGoal cg " + + "WHERE cg.category.isDefault = true " + "AND cg.user.id = :userId " + + "GROUP BY cg.category.id " + "ORDER BY cg.category.id") + List findAllGoalAmountByUserId(@Param("userId") Long userId); } From 7fcc777505ec056b741fc9c572b3cae02483b44d Mon Sep 17 00:00:00 2001 From: MJJ Date: Mon, 12 Aug 2024 15:30:49 +0900 Subject: [PATCH 15/35] =?UTF-8?q?[refactor]=20=EA=B0=80=EC=9E=A5=20?= =?UTF-8?q?=EB=A7=8E=EC=9D=B4=20=EA=B3=84=ED=9A=8D=ED=95=9C=20=EC=B9=B4?= =?UTF-8?q?=ED=85=8C=EA=B3=A0=EB=A6=AC=20=EB=B0=8F=20=ED=8F=89=EA=B7=A0=20?= =?UTF-8?q?=EB=AA=A9=ED=91=9C=EA=B8=88=EC=95=A1=20=EC=A1=B0=ED=9A=8C=20ser?= =?UTF-8?q?vice=20=EA=B5=AC=ED=98=84,=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=EB=AA=85=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/ConsumptionGoalService.java | 3 + .../service/ConsumptionGoalServiceImpl.java | 134 ++++++++++++++---- 2 files changed, 112 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java index 952edc35..6e2c2fc5 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java @@ -21,6 +21,9 @@ public interface ConsumptionGoalService { List getTopGoalCategoriesLimit(int top, Long userId, int peerAgeStart, int peerAgeEnd, String peerGender); + List getAllConsumptionGoalCategories(Long userId, int peerAgeS, int peerAgeE, + String peerG); + ConsumptionGoalResponseListDto findUserConsumptionGoalList(Long userId, LocalDate date); PeerInfoResponseDTO getPeerInfo(Long userId, int peerAgeStart, int peerAgeEnd, String peerGender); diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java index c27b94ee..9b316687 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java @@ -20,13 +20,13 @@ import com.bbteam.budgetbuddies.domain.consumptiongoal.converter.ConsumptionGoalConverter; import com.bbteam.budgetbuddies.domain.consumptiongoal.converter.PeerInfoConverter; import com.bbteam.budgetbuddies.domain.consumptiongoal.converter.TopCategoryConverter; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.CategoryAvgConsumptionDTO; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AvgConsumptionGoalDTO; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionAnalysisResponseDTO; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalListRequestDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalRequestDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseListDto; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.MyConsumptionDTO; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.MyConsumptionGoalDTO; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.PeerInfoResponseDTO; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopConsumptionResponseDTO; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDTO; @@ -72,6 +72,50 @@ public List getTopGoalCategoriesLimit(int top, Long return topGoals.stream().map(TopCategoryConverter::fromEntity).collect(Collectors.toList()); } + @Override + @Transactional(readOnly = true) + public List getAllConsumptionGoalCategories(Long userId, int peerAgeS, int peerAgeE, + String peerG) { + + checkPeerInfo(userId, peerAgeS, peerAgeE, peerG); + + List categoryAvgList = getAvgGoalAmount(); + + List myConsumptionAmountList = getMyGoalAmount(userId); + + List defaultCategories = categoryRepository.findAllByIsDefaultTrue(); + return defaultCategories.stream() + .map(category -> { + MyConsumptionGoalDTO myConsumptionAmountDTO = myConsumptionAmountList.stream() + .filter(dto -> dto.getCategoryId().equals(category.getId())) + .findFirst() + .orElse(new MyConsumptionGoalDTO(category.getId(), 0L)); + + AvgConsumptionGoalDTO avgDTO = categoryAvgList.stream() + .filter(dto -> dto.getCategoryId().equals(category.getId())) + .findFirst() + .orElse(new AvgConsumptionGoalDTO(category.getId(), 0L)); + + Long avgConsumeAmount = avgDTO.getAverageAmount(); + Long myConsumeAmount = myConsumptionAmountDTO.getMyAmount(); + Long consumeAmountDifference; + + if (avgConsumeAmount == 0L) { + consumeAmountDifference = -myConsumeAmount; + } else { + consumeAmountDifference = myConsumeAmount - avgConsumeAmount; + } + + return TopConsumptionResponseDTO.builder() + .categoryName(category.getName()) + .avgConsumeAmount(avgConsumeAmount) + .consumeAmountDifference(consumeAmountDifference) + .build(); + }) + .collect(Collectors.toList()); + + } + @Override @Transactional(readOnly = true) public PeerInfoResponseDTO getPeerInfo(Long userId, int peerAgeS, int peerAgeE, String peerG) { @@ -107,26 +151,26 @@ public List getAllConsumptionCategories(Long userId, checkPeerInfo(userId, peerAgeS, peerAgeE, peerG); - List categoryAvgList = getAvgGoalAmount(); + List categoryAvgList = getAvgConsumptionAmount(); - List myConsumptionAmountList = getMyConsumptionAmount(userId); + List myConsumptionAmountList = getMyConsumptionAmount(userId); List defaultCategories = categoryRepository.findAllByIsDefaultTrue(); return defaultCategories.stream() .map(category -> { - MyConsumptionDTO myConsumptionAmountDTO = myConsumptionAmountList.stream() + MyConsumptionGoalDTO myConsumptionAmountDTO = myConsumptionAmountList.stream() .filter(dto -> dto.getCategoryId().equals(category.getId())) .findFirst() - .orElse(new MyConsumptionDTO(category.getId(), 0L)); // 없을 경우 0L 설정 + .orElse(new MyConsumptionGoalDTO(category.getId(), 0L)); - CategoryAvgConsumptionDTO avgDTO = categoryAvgList.stream() + AvgConsumptionGoalDTO avgDTO = categoryAvgList.stream() .filter(dto -> dto.getCategoryId().equals(category.getId())) .findFirst() - .orElse(new CategoryAvgConsumptionDTO(category.getId(), 0L)); + .orElse(new AvgConsumptionGoalDTO(category.getId(), 0L)); - Long avgConsumeAmount = avgDTO.getAverageConsumeAmount(); - Long myConsumeAmount = myConsumptionAmountDTO.getConsumeAmount(); + Long avgConsumeAmount = avgDTO.getAverageAmount(); + Long myConsumeAmount = myConsumptionAmountDTO.getMyAmount(); Long consumeAmountDifference; if (avgConsumeAmount == 0L) { @@ -189,42 +233,82 @@ private void setAgeGroupByUser(int userAge) { } } - private List getAvgGoalAmount() { + private List getAvgConsumptionAmount() { + + List defaultCategories = categoryRepository.findAllByIsDefaultTrue(); + List categoryAvgList = new ArrayList<>(); + + List avgConsumptionGoalDTO = consumptionGoalRepository.findAvgConsumptionAmountByCategory( + peerAgeStart, peerAgeEnd, peerGender, currentMonth); + + Map categoryAvgMap = avgConsumptionGoalDTO.stream() + .collect(Collectors.toMap(AvgConsumptionGoalDTO::getCategoryId, Function.identity())); + + for (Category category : defaultCategories) { + AvgConsumptionGoalDTO avgDTO = categoryAvgMap.getOrDefault(category.getId(), + new AvgConsumptionGoalDTO(category.getId(), 0.0)); + + categoryAvgList.add(avgDTO); + } + return categoryAvgList; + } + + private List getMyConsumptionAmount(Long userId) { + + List defaultCategories = categoryRepository.findAllByIsDefaultTrue(); + List myConsumptionAmountList = new ArrayList<>(); + + List myConsumptionGoalDTO = consumptionGoalRepository.findAllConsumptionAmountByUserId( + userId); + + Map myConsumptionMap = myConsumptionGoalDTO.stream() + .collect(Collectors.toMap(MyConsumptionGoalDTO::getCategoryId, Function.identity())); + + for (Category category : defaultCategories) { + MyConsumptionGoalDTO mylDTO = myConsumptionMap.getOrDefault(category.getId(), + new MyConsumptionGoalDTO(category.getId(), 0L)); + + myConsumptionAmountList.add(mylDTO); + } + return myConsumptionAmountList; + } + + private List getAvgGoalAmount() { List defaultCategories = categoryRepository.findAllByIsDefaultTrue(); - List categoryAvgList = new ArrayList<>(); + List categoryAvgList = new ArrayList<>(); - List categoryAverageGoalDTOs = consumptionGoalRepository.findAvgConsumptionByCategory( + List avgConsumptionGoalDTO = consumptionGoalRepository.findAvgGoalAmountByCategory( peerAgeStart, peerAgeEnd, peerGender, currentMonth); - Map categoryAvgMap = categoryAverageGoalDTOs.stream() - .collect(Collectors.toMap(CategoryAvgConsumptionDTO::getCategoryId, Function.identity())); + Map categoryAvgMap = avgConsumptionGoalDTO.stream() + .collect(Collectors.toMap(AvgConsumptionGoalDTO::getCategoryId, Function.identity())); for (Category category : defaultCategories) { - CategoryAvgConsumptionDTO avgDTO = categoryAvgMap.getOrDefault(category.getId(), - new CategoryAvgConsumptionDTO(category.getId(), 0.0)); + AvgConsumptionGoalDTO avgDTO = categoryAvgMap.getOrDefault(category.getId(), + new AvgConsumptionGoalDTO(category.getId(), 0.0)); categoryAvgList.add(avgDTO); } return categoryAvgList; } - private List getMyConsumptionAmount(Long userId) { + private List getMyGoalAmount(Long userId) { List defaultCategories = categoryRepository.findAllByIsDefaultTrue(); - List myConsumptionAmountList = new ArrayList<>(); + List myConsumptionAmountList = new ArrayList<>(); - List myConsumptionAmountDTOs = consumptionGoalRepository.findAllConsumptionAmountByUserId( + List myConsumptionGoalDTO = consumptionGoalRepository.findAllGoalAmountByUserId( userId); - Map myConsumptionMap = myConsumptionAmountDTOs.stream() - .collect(Collectors.toMap(MyConsumptionDTO::getCategoryId, Function.identity())); + Map myConsumptionMap = myConsumptionGoalDTO.stream() + .collect(Collectors.toMap(MyConsumptionGoalDTO::getCategoryId, Function.identity())); for (Category category : defaultCategories) { - MyConsumptionDTO myConsumptionDTO = myConsumptionMap.getOrDefault(category.getId(), - new MyConsumptionDTO(category.getId(), 0L)); + MyConsumptionGoalDTO myDTO = myConsumptionMap.getOrDefault(category.getId(), + new MyConsumptionGoalDTO(category.getId(), 0L)); - myConsumptionAmountList.add(myConsumptionDTO); + myConsumptionAmountList.add(myDTO); } return myConsumptionAmountList; } From b4309bab4fa58c6dced92a24aeb4d24e31a115c3 Mon Sep 17 00:00:00 2001 From: MJJ Date: Mon, 12 Aug 2024 15:54:57 +0900 Subject: [PATCH 16/35] =?UTF-8?q?[refactor]=20consumptionGoal=20DTO=20->?= =?UTF-8?q?=20dto=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ConsumptionGoalController.java | 14 +-- .../ConsumptionAnalysisConverter.java | 6 +- .../converter/PeerInfoConverter.java | 6 +- .../converter/TopCategoryConverter.java | 6 +- .../converter/TopConsumptionConverter.java | 17 --- ...oalDTO.java => AvgConsumptionGoalDto.java} | 4 +- ...va => ConsumptionAnalysisResponseDto.java} | 2 +- ...GoalDTO.java => MyConsumptionGoalDto.java} | 2 +- ...ponseDTO.java => PeerInfoResponseDto.java} | 2 +- ...TO.java => TopConsumptionResponseDto.java} | 2 +- ...O.java => TopGoalCategoryResponseDto.java} | 2 +- .../repository/ConsumptionGoalRepository.java | 20 ++-- .../service/ConsumptionGoalService.java | 18 +-- .../service/ConsumptionGoalServiceImpl.java | 106 +++++++++--------- .../mainpage/converter/MainPageConverter.java | 30 ++--- .../mainpage/dto/MainPageResponseDto.java | 17 +-- .../mainpage/service/MainPageServiceImpl.java | 8 +- 17 files changed, 123 insertions(+), 139 deletions(-) delete mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/TopConsumptionConverter.java rename src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/{AvgConsumptionGoalDTO.java => AvgConsumptionGoalDto.java} (79%) rename src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/{ConsumptionAnalysisResponseDTO.java => ConsumptionAnalysisResponseDto.java} (87%) rename src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/{MyConsumptionGoalDTO.java => MyConsumptionGoalDto.java} (87%) rename src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/{PeerInfoResponseDTO.java => PeerInfoResponseDto.java} (90%) rename src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/{TopConsumptionResponseDTO.java => TopConsumptionResponseDto.java} (89%) rename src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/{TopGoalCategoryResponseDTO.java => TopGoalCategoryResponseDto.java} (87%) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java index 592a5491..72ad8158 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java @@ -15,9 +15,9 @@ import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalListRequestDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseListDto; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.PeerInfoResponseDTO; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopConsumptionResponseDTO; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDTO; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.PeerInfoResponseDto; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopConsumptionResponseDto; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.service.ConsumptionGoalService; import lombok.RequiredArgsConstructor; @@ -36,7 +36,7 @@ public ResponseEntity getTopGoalCategoriesList(@PathVariable(name = "top") in @RequestParam(name = "peerAgeStart", defaultValue = "0") int peerAgeStart, @RequestParam(name = "peerAgeEnd", defaultValue = "0") int peerAgeEnd, @RequestParam(name = "peerGender", defaultValue = "none") String peerGender) { - List topCategoriesList = consumptionGoalService.getTopGoalCategoriesLimit(top, + List topCategoriesList = consumptionGoalService.getTopGoalCategoriesLimit(top, userId, peerAgeStart, peerAgeEnd, peerGender); return ResponseEntity.ok(topCategoriesList); } @@ -46,7 +46,7 @@ public ResponseEntity getAllConsumptionGoalCategories(@RequestParam(name = "u @RequestParam(name = "peerAgeStart", defaultValue = "0") int peerAgeStart, @RequestParam(name = "peerAgeEnd", defaultValue = "0") int peerAgeEnd, @RequestParam(name = "peerGender", defaultValue = "none") String peerGender) { - List response = consumptionGoalService.getAllConsumptionGoalCategories(userId, + List response = consumptionGoalService.getAllConsumptionGoalCategories(userId, peerAgeStart, peerAgeEnd, peerGender); return ResponseEntity.ok(response); @@ -58,7 +58,7 @@ public ResponseEntity getPeerInfo(@RequestParam(name = "userId") Long userId, @RequestParam(name = "peerAgeStart", defaultValue = "0") int peerAgeStart, @RequestParam(name = "peerAgeEnd", defaultValue = "0") int peerAgeEnd, @RequestParam(name = "peerGender", defaultValue = "none") String peerGender) { - PeerInfoResponseDTO response = consumptionGoalService.getPeerInfo(userId, peerAgeStart, peerAgeEnd, peerGender); + PeerInfoResponseDto response = consumptionGoalService.getPeerInfo(userId, peerAgeStart, peerAgeEnd, peerGender); return ResponseEntity.ok(response); } @@ -85,7 +85,7 @@ public ResponseEntity getAllConsumptionCategories(@RequestParam(name = "userI @RequestParam(name = "peerAgeStart", defaultValue = "0") int peerAgeStart, @RequestParam(name = "peerAgeEnd", defaultValue = "0") int peerAgeEnd, @RequestParam(name = "peerGender", defaultValue = "none") String peerGender) { - List response = consumptionGoalService.getAllConsumptionCategories(userId, + List response = consumptionGoalService.getAllConsumptionCategories(userId, peerAgeStart, peerAgeEnd, peerGender); return ResponseEntity.ok(response); diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/ConsumptionAnalysisConverter.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/ConsumptionAnalysisConverter.java index 27b34819..f66e5e61 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/ConsumptionAnalysisConverter.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/ConsumptionAnalysisConverter.java @@ -1,13 +1,13 @@ package com.bbteam.budgetbuddies.domain.consumptiongoal.converter; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionAnalysisResponseDTO; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionAnalysisResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.entity.ConsumptionGoal; public class ConsumptionAnalysisConverter { - public static ConsumptionAnalysisResponseDTO fromEntity(ConsumptionGoal consumptionGoal, Long topAmount) { + public static ConsumptionAnalysisResponseDto fromEntity(ConsumptionGoal consumptionGoal, Long topAmount) { - return ConsumptionAnalysisResponseDTO.builder() + return ConsumptionAnalysisResponseDto.builder() .goalCategory(consumptionGoal.getCategory().getName()) .currentWeekConsumptionAmount(topAmount) .build(); diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/PeerInfoConverter.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/PeerInfoConverter.java index d6c3eb24..2b66f373 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/PeerInfoConverter.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/PeerInfoConverter.java @@ -1,13 +1,13 @@ package com.bbteam.budgetbuddies.domain.consumptiongoal.converter; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.PeerInfoResponseDTO; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.PeerInfoResponseDto; import com.bbteam.budgetbuddies.enums.Gender; public class PeerInfoConverter { - public static PeerInfoResponseDTO fromEntity(int peerAgeStart, int peerAgeEnd, Gender peerGender) { + public static PeerInfoResponseDto fromEntity(int peerAgeStart, int peerAgeEnd, Gender peerGender) { - return PeerInfoResponseDTO.builder() + return PeerInfoResponseDto.builder() .peerAgeStart(peerAgeStart) .peerAgeEnd(peerAgeEnd) .peerGender(peerGender.name()) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/TopCategoryConverter.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/TopCategoryConverter.java index 239dc8da..2bed4bb6 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/TopCategoryConverter.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/TopCategoryConverter.java @@ -2,18 +2,18 @@ import org.springframework.stereotype.Component; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDTO; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.entity.ConsumptionGoal; @Component public class TopCategoryConverter { - public static TopGoalCategoryResponseDTO fromEntity(ConsumptionGoal consumptionGoal) { + public static TopGoalCategoryResponseDto fromEntity(ConsumptionGoal consumptionGoal) { if (consumptionGoal == null || consumptionGoal.getCategory() == null) { return null; } - return TopGoalCategoryResponseDTO.builder() + return TopGoalCategoryResponseDto.builder() .categoryName(consumptionGoal.getCategory().getName()) .goalAmount(consumptionGoal.getGoalAmount()) .build(); diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/TopConsumptionConverter.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/TopConsumptionConverter.java deleted file mode 100644 index 9f37e9c1..00000000 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/TopConsumptionConverter.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.bbteam.budgetbuddies.domain.consumptiongoal.converter; - -import org.springframework.stereotype.Component; - -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopConsumptionResponseDTO; -import com.bbteam.budgetbuddies.domain.consumptiongoal.entity.ConsumptionGoal; - -@Component -public class TopConsumptionConverter { - public static TopConsumptionResponseDTO fromEntity(ConsumptionGoal consumptionGoal) { - - return TopConsumptionResponseDTO.builder() - .categoryName(consumptionGoal.getCategory().getName()) - .consumeAmount(consumptionGoal.getConsumeAmount()) - .build(); - } -} diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/AvgConsumptionGoalDTO.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/AvgConsumptionGoalDto.java similarity index 79% rename from src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/AvgConsumptionGoalDTO.java rename to src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/AvgConsumptionGoalDto.java index 249ace6f..e73a8c51 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/AvgConsumptionGoalDTO.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/AvgConsumptionGoalDto.java @@ -7,12 +7,12 @@ @Getter @AllArgsConstructor @NoArgsConstructor -public class AvgConsumptionGoalDTO { +public class AvgConsumptionGoalDto { private Long categoryId; private Long averageAmount; - public AvgConsumptionGoalDTO(Long categoryId, Double averageAmount) { + public AvgConsumptionGoalDto(Long categoryId, Double averageAmount) { this.categoryId = categoryId; this.averageAmount = (averageAmount != null) ? averageAmount.longValue() : 0L; } diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/ConsumptionAnalysisResponseDTO.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/ConsumptionAnalysisResponseDto.java similarity index 87% rename from src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/ConsumptionAnalysisResponseDTO.java rename to src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/ConsumptionAnalysisResponseDto.java index 10c505be..127cdd58 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/ConsumptionAnalysisResponseDTO.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/ConsumptionAnalysisResponseDto.java @@ -9,7 +9,7 @@ @Builder @NoArgsConstructor @AllArgsConstructor -public class ConsumptionAnalysisResponseDTO { +public class ConsumptionAnalysisResponseDto { private String goalCategory; diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/MyConsumptionGoalDTO.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/MyConsumptionGoalDto.java similarity index 87% rename from src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/MyConsumptionGoalDTO.java rename to src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/MyConsumptionGoalDto.java index 10cc56d3..b9217b90 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/MyConsumptionGoalDTO.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/MyConsumptionGoalDto.java @@ -7,7 +7,7 @@ @Getter @AllArgsConstructor @NoArgsConstructor -public class MyConsumptionGoalDTO { +public class MyConsumptionGoalDto { private Long categoryId; private Long MyAmount; diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/PeerInfoResponseDTO.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/PeerInfoResponseDto.java similarity index 90% rename from src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/PeerInfoResponseDTO.java rename to src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/PeerInfoResponseDto.java index 3ac0d041..a20e0172 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/PeerInfoResponseDTO.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/PeerInfoResponseDto.java @@ -9,7 +9,7 @@ @Builder @NoArgsConstructor @AllArgsConstructor -public class PeerInfoResponseDTO { +public class PeerInfoResponseDto { private int peerAgeStart; diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/TopConsumptionResponseDTO.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/TopConsumptionResponseDto.java similarity index 89% rename from src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/TopConsumptionResponseDTO.java rename to src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/TopConsumptionResponseDto.java index cfd0b44b..10a1903e 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/TopConsumptionResponseDTO.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/TopConsumptionResponseDto.java @@ -9,7 +9,7 @@ @Builder @NoArgsConstructor @AllArgsConstructor -public class TopConsumptionResponseDTO { +public class TopConsumptionResponseDto { private String categoryName; diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/TopGoalCategoryResponseDTO.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/TopGoalCategoryResponseDto.java similarity index 87% rename from src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/TopGoalCategoryResponseDTO.java rename to src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/TopGoalCategoryResponseDto.java index 99c394b4..ba8549a1 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/TopGoalCategoryResponseDTO.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/TopGoalCategoryResponseDto.java @@ -9,7 +9,7 @@ @Builder @NoArgsConstructor @AllArgsConstructor -public class TopGoalCategoryResponseDTO { +public class TopGoalCategoryResponseDto { private String categoryName; diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java index 5f54a716..17187ddf 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java @@ -10,8 +10,8 @@ import org.springframework.stereotype.Repository; import com.bbteam.budgetbuddies.domain.category.entity.Category; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AvgConsumptionGoalDTO; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.MyConsumptionGoalDTO; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AvgConsumptionGoalDto; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.MyConsumptionGoalDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.entity.ConsumptionGoal; import com.bbteam.budgetbuddies.domain.user.entity.User; import com.bbteam.budgetbuddies.enums.Gender; @@ -38,36 +38,36 @@ Optional findTopConsumptionByCategoryIdAndCurrentWeek(@Param("c @Param("startOfWeek") LocalDate startOfWeek, @Param("endOfWeek") LocalDate endOfWeek); @Query( - "SELECT new com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AvgConsumptionGoalDTO(cg.category.id, AVG(cg.consumeAmount))" + "SELECT new com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AvgConsumptionGoalDto(cg.category.id, AVG(cg.consumeAmount))" + "FROM ConsumptionGoal cg " + "WHERE cg.category.isDefault = true " + "AND cg.user.age BETWEEN :peerAgeStart AND :peerAgeEnd " + "AND cg.user.gender = :peerGender " + "AND cg.goalMonth >= :currentMonth " + "GROUP BY cg.category.id " + "ORDER BY AVG(cg.consumeAmount) DESC") - List findAvgConsumptionAmountByCategory( + List findAvgConsumptionAmountByCategory( @Param("peerAgeStart") int peerAgeStart, @Param("peerAgeEnd") int peerAgeEnd, @Param("peerGender") Gender peerGender, @Param("currentMonth") LocalDate currentMonth); - @Query("SELECT new com.bbteam.budgetbuddies.domain.consumptiongoal.dto.MyConsumptionGoalDTO(" + @Query("SELECT new com.bbteam.budgetbuddies.domain.consumptiongoal.dto.MyConsumptionGoalDto(" + "cg.category.id, SUM(cg.consumeAmount)) " + "FROM ConsumptionGoal cg " + "WHERE cg.category.isDefault = true " + "AND cg.user.id = :userId " + "GROUP BY cg.category.id " + "ORDER BY cg.category.id") - List findAllConsumptionAmountByUserId(@Param("userId") Long userId); + List findAllConsumptionAmountByUserId(@Param("userId") Long userId); @Query( - "SELECT new com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AvgConsumptionGoalDTO(cg.category.id, AVG(cg.goalAmount))" + "SELECT new com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AvgConsumptionGoalDto(cg.category.id, AVG(cg.goalAmount))" + "FROM ConsumptionGoal cg " + "WHERE cg.category.isDefault = true " + "AND cg.user.age BETWEEN :peerAgeStart AND :peerAgeEnd " + "AND cg.user.gender = :peerGender " + "AND cg.goalMonth >= :currentMonth " + "GROUP BY cg.category.id " + "ORDER BY AVG(cg.goalAmount) DESC") - List findAvgGoalAmountByCategory( + List findAvgGoalAmountByCategory( @Param("peerAgeStart") int peerAgeStart, @Param("peerAgeEnd") int peerAgeEnd, @Param("peerGender") Gender peerGender, @Param("currentMonth") LocalDate currentMonth); - @Query("SELECT new com.bbteam.budgetbuddies.domain.consumptiongoal.dto.MyConsumptionGoalDTO(" + @Query("SELECT new com.bbteam.budgetbuddies.domain.consumptiongoal.dto.MyConsumptionGoalDto(" + "cg.category.id, SUM(cg.goalAmount)) " + "FROM ConsumptionGoal cg " + "WHERE cg.category.isDefault = true " + "AND cg.user.id = :userId " + "GROUP BY cg.category.id " + "ORDER BY cg.category.id") - List findAllGoalAmountByUserId(@Param("userId") Long userId); + List findAllGoalAmountByUserId(@Param("userId") Long userId); } diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java index 6e2c2fc5..f59e1bdf 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java @@ -5,12 +5,12 @@ import org.springframework.stereotype.Service; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionAnalysisResponseDTO; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionAnalysisResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalListRequestDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseListDto; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.PeerInfoResponseDTO; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopConsumptionResponseDTO; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDTO; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.PeerInfoResponseDto; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopConsumptionResponseDto; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDto; import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseUpdateRequestDto; import com.bbteam.budgetbuddies.domain.expense.entity.Expense; import com.bbteam.budgetbuddies.domain.user.entity.User; @@ -18,20 +18,20 @@ @Service public interface ConsumptionGoalService { - List getTopGoalCategoriesLimit(int top, Long userId, int peerAgeStart, int peerAgeEnd, + List getTopGoalCategoriesLimit(int top, Long userId, int peerAgeStart, int peerAgeEnd, String peerGender); - List getAllConsumptionGoalCategories(Long userId, int peerAgeS, int peerAgeE, + List getAllConsumptionGoalCategories(Long userId, int peerAgeS, int peerAgeE, String peerG); ConsumptionGoalResponseListDto findUserConsumptionGoalList(Long userId, LocalDate date); - PeerInfoResponseDTO getPeerInfo(Long userId, int peerAgeStart, int peerAgeEnd, String peerGender); + PeerInfoResponseDto getPeerInfo(Long userId, int peerAgeStart, int peerAgeEnd, String peerGender); ConsumptionGoalResponseListDto updateConsumptionGoals(Long userId, ConsumptionGoalListRequestDto consumptionGoalListRequestDto); - ConsumptionAnalysisResponseDTO getTopCategoryAndConsumptionAmount(Long userId); + ConsumptionAnalysisResponseDto getTopCategoryAndConsumptionAmount(Long userId); void recalculateConsumptionAmount(Expense expense, ExpenseUpdateRequestDto request, User user); @@ -39,6 +39,6 @@ ConsumptionGoalResponseListDto updateConsumptionGoals(Long userId, void decreaseConsumeAmount(Long userId, Long categoryId, Long amount, LocalDate expenseDate); - List getAllConsumptionCategories(Long userId, int peerAgeS, int peerAgeE, + List getAllConsumptionCategories(Long userId, int peerAgeS, int peerAgeE, String peerG); } diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java index 9b316687..bf9e614e 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java @@ -20,16 +20,16 @@ import com.bbteam.budgetbuddies.domain.consumptiongoal.converter.ConsumptionGoalConverter; import com.bbteam.budgetbuddies.domain.consumptiongoal.converter.PeerInfoConverter; import com.bbteam.budgetbuddies.domain.consumptiongoal.converter.TopCategoryConverter; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AvgConsumptionGoalDTO; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionAnalysisResponseDTO; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AvgConsumptionGoalDto; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionAnalysisResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalListRequestDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalRequestDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseListDto; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.MyConsumptionGoalDTO; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.PeerInfoResponseDTO; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopConsumptionResponseDTO; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDTO; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.MyConsumptionGoalDto; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.PeerInfoResponseDto; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopConsumptionResponseDto; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.entity.ConsumptionGoal; import com.bbteam.budgetbuddies.domain.consumptiongoal.repository.ConsumptionGoalRepository; import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseUpdateRequestDto; @@ -62,7 +62,7 @@ public class ConsumptionGoalServiceImpl implements ConsumptionGoalService { @Override @Transactional(readOnly = true) - public List getTopGoalCategoriesLimit(int top, Long userId, int peerAgeS, int peerAgeE, + public List getTopGoalCategoriesLimit(int top, Long userId, int peerAgeS, int peerAgeE, String peerG) { checkPeerInfo(userId, peerAgeS, peerAgeE, peerG); @@ -74,27 +74,27 @@ public List getTopGoalCategoriesLimit(int top, Long @Override @Transactional(readOnly = true) - public List getAllConsumptionGoalCategories(Long userId, int peerAgeS, int peerAgeE, + public List getAllConsumptionGoalCategories(Long userId, int peerAgeS, int peerAgeE, String peerG) { checkPeerInfo(userId, peerAgeS, peerAgeE, peerG); - List categoryAvgList = getAvgGoalAmount(); + List categoryAvgList = getAvgGoalAmount(); - List myConsumptionAmountList = getMyGoalAmount(userId); + List myConsumptionAmountList = getMyGoalAmount(userId); List defaultCategories = categoryRepository.findAllByIsDefaultTrue(); return defaultCategories.stream() .map(category -> { - MyConsumptionGoalDTO myConsumptionAmountDTO = myConsumptionAmountList.stream() + MyConsumptionGoalDto myConsumptionAmountDTO = myConsumptionAmountList.stream() .filter(dto -> dto.getCategoryId().equals(category.getId())) .findFirst() - .orElse(new MyConsumptionGoalDTO(category.getId(), 0L)); + .orElse(new MyConsumptionGoalDto(category.getId(), 0L)); - AvgConsumptionGoalDTO avgDTO = categoryAvgList.stream() + AvgConsumptionGoalDto avgDTO = categoryAvgList.stream() .filter(dto -> dto.getCategoryId().equals(category.getId())) .findFirst() - .orElse(new AvgConsumptionGoalDTO(category.getId(), 0L)); + .orElse(new AvgConsumptionGoalDto(category.getId(), 0L)); Long avgConsumeAmount = avgDTO.getAverageAmount(); Long myConsumeAmount = myConsumptionAmountDTO.getMyAmount(); @@ -106,7 +106,7 @@ public List getAllConsumptionGoalCategories(Long user consumeAmountDifference = myConsumeAmount - avgConsumeAmount; } - return TopConsumptionResponseDTO.builder() + return TopConsumptionResponseDto.builder() .categoryName(category.getName()) .avgConsumeAmount(avgConsumeAmount) .consumeAmountDifference(consumeAmountDifference) @@ -118,7 +118,7 @@ public List getAllConsumptionGoalCategories(Long user @Override @Transactional(readOnly = true) - public PeerInfoResponseDTO getPeerInfo(Long userId, int peerAgeS, int peerAgeE, String peerG) { + public PeerInfoResponseDto getPeerInfo(Long userId, int peerAgeS, int peerAgeE, String peerG) { checkPeerInfo(userId, peerAgeS, peerAgeE, peerG); @@ -127,7 +127,7 @@ public PeerInfoResponseDTO getPeerInfo(Long userId, int peerAgeS, int peerAgeE, @Override @Transactional(readOnly = true) - public ConsumptionAnalysisResponseDTO getTopCategoryAndConsumptionAmount(Long userId) { + public ConsumptionAnalysisResponseDto getTopCategoryAndConsumptionAmount(Long userId) { checkPeerInfo(userId, 0, 0, "none"); @@ -146,28 +146,28 @@ public ConsumptionAnalysisResponseDTO getTopCategoryAndConsumptionAmount(Long us @Override @Transactional(readOnly = true) - public List getAllConsumptionCategories(Long userId, int peerAgeS, int peerAgeE, + public List getAllConsumptionCategories(Long userId, int peerAgeS, int peerAgeE, String peerG) { checkPeerInfo(userId, peerAgeS, peerAgeE, peerG); - List categoryAvgList = getAvgConsumptionAmount(); + List categoryAvgList = getAvgConsumptionAmount(); - List myConsumptionAmountList = getMyConsumptionAmount(userId); + List myConsumptionAmountList = getMyConsumptionAmount(userId); List defaultCategories = categoryRepository.findAllByIsDefaultTrue(); return defaultCategories.stream() .map(category -> { - MyConsumptionGoalDTO myConsumptionAmountDTO = myConsumptionAmountList.stream() + MyConsumptionGoalDto myConsumptionAmountDTO = myConsumptionAmountList.stream() .filter(dto -> dto.getCategoryId().equals(category.getId())) .findFirst() - .orElse(new MyConsumptionGoalDTO(category.getId(), 0L)); + .orElse(new MyConsumptionGoalDto(category.getId(), 0L)); - AvgConsumptionGoalDTO avgDTO = categoryAvgList.stream() + AvgConsumptionGoalDto avgDTO = categoryAvgList.stream() .filter(dto -> dto.getCategoryId().equals(category.getId())) .findFirst() - .orElse(new AvgConsumptionGoalDTO(category.getId(), 0L)); + .orElse(new AvgConsumptionGoalDto(category.getId(), 0L)); Long avgConsumeAmount = avgDTO.getAverageAmount(); Long myConsumeAmount = myConsumptionAmountDTO.getMyAmount(); @@ -179,7 +179,7 @@ public List getAllConsumptionCategories(Long userId, consumeAmountDifference = myConsumeAmount - avgConsumeAmount; } - return TopConsumptionResponseDTO.builder() + return TopConsumptionResponseDto.builder() .categoryName(category.getName()) .avgConsumeAmount(avgConsumeAmount) .consumeAmountDifference(consumeAmountDifference) @@ -233,80 +233,80 @@ private void setAgeGroupByUser(int userAge) { } } - private List getAvgConsumptionAmount() { + private List getAvgConsumptionAmount() { List defaultCategories = categoryRepository.findAllByIsDefaultTrue(); - List categoryAvgList = new ArrayList<>(); + List categoryAvgList = new ArrayList<>(); - List avgConsumptionGoalDTO = consumptionGoalRepository.findAvgConsumptionAmountByCategory( + List avgConsumptionGoalDTO = consumptionGoalRepository.findAvgConsumptionAmountByCategory( peerAgeStart, peerAgeEnd, peerGender, currentMonth); - Map categoryAvgMap = avgConsumptionGoalDTO.stream() - .collect(Collectors.toMap(AvgConsumptionGoalDTO::getCategoryId, Function.identity())); + Map categoryAvgMap = avgConsumptionGoalDTO.stream() + .collect(Collectors.toMap(AvgConsumptionGoalDto::getCategoryId, Function.identity())); for (Category category : defaultCategories) { - AvgConsumptionGoalDTO avgDTO = categoryAvgMap.getOrDefault(category.getId(), - new AvgConsumptionGoalDTO(category.getId(), 0.0)); + AvgConsumptionGoalDto avgDTO = categoryAvgMap.getOrDefault(category.getId(), + new AvgConsumptionGoalDto(category.getId(), 0.0)); categoryAvgList.add(avgDTO); } return categoryAvgList; } - private List getMyConsumptionAmount(Long userId) { + private List getMyConsumptionAmount(Long userId) { List defaultCategories = categoryRepository.findAllByIsDefaultTrue(); - List myConsumptionAmountList = new ArrayList<>(); + List myConsumptionAmountList = new ArrayList<>(); - List myConsumptionGoalDTO = consumptionGoalRepository.findAllConsumptionAmountByUserId( + List myConsumptionGoalDTO = consumptionGoalRepository.findAllConsumptionAmountByUserId( userId); - Map myConsumptionMap = myConsumptionGoalDTO.stream() - .collect(Collectors.toMap(MyConsumptionGoalDTO::getCategoryId, Function.identity())); + Map myConsumptionMap = myConsumptionGoalDTO.stream() + .collect(Collectors.toMap(MyConsumptionGoalDto::getCategoryId, Function.identity())); for (Category category : defaultCategories) { - MyConsumptionGoalDTO mylDTO = myConsumptionMap.getOrDefault(category.getId(), - new MyConsumptionGoalDTO(category.getId(), 0L)); + MyConsumptionGoalDto mylDTO = myConsumptionMap.getOrDefault(category.getId(), + new MyConsumptionGoalDto(category.getId(), 0L)); myConsumptionAmountList.add(mylDTO); } return myConsumptionAmountList; } - private List getAvgGoalAmount() { + private List getAvgGoalAmount() { List defaultCategories = categoryRepository.findAllByIsDefaultTrue(); - List categoryAvgList = new ArrayList<>(); + List categoryAvgList = new ArrayList<>(); - List avgConsumptionGoalDTO = consumptionGoalRepository.findAvgGoalAmountByCategory( + List avgConsumptionGoalDTO = consumptionGoalRepository.findAvgGoalAmountByCategory( peerAgeStart, peerAgeEnd, peerGender, currentMonth); - Map categoryAvgMap = avgConsumptionGoalDTO.stream() - .collect(Collectors.toMap(AvgConsumptionGoalDTO::getCategoryId, Function.identity())); + Map categoryAvgMap = avgConsumptionGoalDTO.stream() + .collect(Collectors.toMap(AvgConsumptionGoalDto::getCategoryId, Function.identity())); for (Category category : defaultCategories) { - AvgConsumptionGoalDTO avgDTO = categoryAvgMap.getOrDefault(category.getId(), - new AvgConsumptionGoalDTO(category.getId(), 0.0)); + AvgConsumptionGoalDto avgDTO = categoryAvgMap.getOrDefault(category.getId(), + new AvgConsumptionGoalDto(category.getId(), 0.0)); categoryAvgList.add(avgDTO); } return categoryAvgList; } - private List getMyGoalAmount(Long userId) { + private List getMyGoalAmount(Long userId) { List defaultCategories = categoryRepository.findAllByIsDefaultTrue(); - List myConsumptionAmountList = new ArrayList<>(); + List myConsumptionAmountList = new ArrayList<>(); - List myConsumptionGoalDTO = consumptionGoalRepository.findAllGoalAmountByUserId( + List myConsumptionGoalDTO = consumptionGoalRepository.findAllGoalAmountByUserId( userId); - Map myConsumptionMap = myConsumptionGoalDTO.stream() - .collect(Collectors.toMap(MyConsumptionGoalDTO::getCategoryId, Function.identity())); + Map myConsumptionMap = myConsumptionGoalDTO.stream() + .collect(Collectors.toMap(MyConsumptionGoalDto::getCategoryId, Function.identity())); for (Category category : defaultCategories) { - MyConsumptionGoalDTO myDTO = myConsumptionMap.getOrDefault(category.getId(), - new MyConsumptionGoalDTO(category.getId(), 0L)); + MyConsumptionGoalDto myDTO = myConsumptionMap.getOrDefault(category.getId(), + new MyConsumptionGoalDto(category.getId(), 0L)); myConsumptionAmountList.add(myDTO); } diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/mainpage/converter/MainPageConverter.java b/src/main/java/com/bbteam/budgetbuddies/domain/mainpage/converter/MainPageConverter.java index 02a7e32b..ffd6e3fd 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/mainpage/converter/MainPageConverter.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/mainpage/converter/MainPageConverter.java @@ -1,24 +1,24 @@ package com.bbteam.budgetbuddies.domain.mainpage.converter; +import java.util.List; + import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseListDto; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDTO; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDto; import com.bbteam.budgetbuddies.domain.discountinfo.dto.DiscountResponseDto; import com.bbteam.budgetbuddies.domain.mainpage.dto.MainPageResponseDto; import com.bbteam.budgetbuddies.domain.supportinfo.dto.SupportResponseDto; -import java.util.List; - public class MainPageConverter { - public static MainPageResponseDto toMainPageResponseDto( - List discountResponseDtoList, - List supportResponseDtoList, - TopGoalCategoryResponseDTO topGoalCategoryResponseDTO, - ConsumptionGoalResponseListDto consumptionGoalResponseListDto) { - return MainPageResponseDto.builder() - .discountResponseDtoList(discountResponseDtoList) - .supportResponseDtoList(supportResponseDtoList) - .topGoalCategoryResponseDTO(topGoalCategoryResponseDTO) - .consumptionGoalResponseListDto(consumptionGoalResponseListDto) - .build(); - } + public static MainPageResponseDto toMainPageResponseDto( + List discountResponseDtoList, + List supportResponseDtoList, + TopGoalCategoryResponseDto topGoalCategoryResponseDto, + ConsumptionGoalResponseListDto consumptionGoalResponseListDto) { + return MainPageResponseDto.builder() + .discountResponseDtoList(discountResponseDtoList) + .supportResponseDtoList(supportResponseDtoList) + .topGoalCategoryResponseDto(topGoalCategoryResponseDto) + .consumptionGoalResponseListDto(consumptionGoalResponseListDto) + .build(); + } } diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/mainpage/dto/MainPageResponseDto.java b/src/main/java/com/bbteam/budgetbuddies/domain/mainpage/dto/MainPageResponseDto.java index 2031c2d3..f9eb0820 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/mainpage/dto/MainPageResponseDto.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/mainpage/dto/MainPageResponseDto.java @@ -1,22 +1,23 @@ package com.bbteam.budgetbuddies.domain.mainpage.dto; +import java.util.List; + import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseListDto; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDTO; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDto; import com.bbteam.budgetbuddies.domain.discountinfo.dto.DiscountResponseDto; import com.bbteam.budgetbuddies.domain.supportinfo.dto.SupportResponseDto; + import lombok.Builder; import lombok.Getter; -import java.util.List; - @Getter @Builder public class MainPageResponseDto { - private TopGoalCategoryResponseDTO topGoalCategoryResponseDTO; - private ConsumptionGoalResponseListDto consumptionGoalResponseListDto; - private List discountResponseDtoList; - private List supportResponseDtoList; - // 기존 DTO들 활용 + private TopGoalCategoryResponseDto topGoalCategoryResponseDto; + private ConsumptionGoalResponseListDto consumptionGoalResponseListDto; + private List discountResponseDtoList; + private List supportResponseDtoList; + // 기존 DTO들 활용 } diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/mainpage/service/MainPageServiceImpl.java b/src/main/java/com/bbteam/budgetbuddies/domain/mainpage/service/MainPageServiceImpl.java index b5089589..c09ea31f 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/mainpage/service/MainPageServiceImpl.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/mainpage/service/MainPageServiceImpl.java @@ -7,7 +7,7 @@ import org.springframework.stereotype.Service; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseListDto; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDTO; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.service.ConsumptionGoalService; import com.bbteam.budgetbuddies.domain.discountinfo.dto.DiscountResponseDto; import com.bbteam.budgetbuddies.domain.discountinfo.service.DiscountInfoService; @@ -36,12 +36,12 @@ public MainPageResponseDto getMainPage(Long userId) { now.getMonthValue(), 0, 2) .getContent(); - List topGoalCategoryResponseDTOList = consumptionGoalService.getTopGoalCategoriesLimit( + List topGoalCategoryResponseDtoList = consumptionGoalService.getTopGoalCategoriesLimit( 1, userId, 0, 0, "NONE"); - if (topGoalCategoryResponseDTOList.size() == 0) { + if (topGoalCategoryResponseDtoList.size() == 0) { throw new NoSuchElementException("Category xx"); } - TopGoalCategoryResponseDTO topGoalCategoryResponseDTO = topGoalCategoryResponseDTOList.get(0); + TopGoalCategoryResponseDto topGoalCategoryResponseDTO = topGoalCategoryResponseDtoList.get(0); ConsumptionGoalResponseListDto userConsumptionGoal = consumptionGoalService.findUserConsumptionGoalList(userId, now); From 74e69151f3094c485c02ca1271aef12f554af28e Mon Sep 17 00:00:00 2001 From: MJJ Date: Mon, 12 Aug 2024 16:09:56 +0900 Subject: [PATCH 17/35] =?UTF-8?q?[refactor]=20consumption=20converter=20?= =?UTF-8?q?=ED=8C=8C=EC=9D=BC=20=EB=AC=B6=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ConsumptionAnalysisConverter.java | 15 ------- .../converter/ConsumptionGoalConverter.java | 45 ++++++++++++++++--- .../converter/PeerInfoConverter.java | 16 ------- .../converter/TopCategoryConverter.java | 21 --------- .../service/ConsumptionGoalServiceImpl.java | 10 ++--- 5 files changed, 43 insertions(+), 64 deletions(-) delete mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/ConsumptionAnalysisConverter.java delete mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/PeerInfoConverter.java delete mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/TopCategoryConverter.java diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/ConsumptionAnalysisConverter.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/ConsumptionAnalysisConverter.java deleted file mode 100644 index f66e5e61..00000000 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/ConsumptionAnalysisConverter.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.bbteam.budgetbuddies.domain.consumptiongoal.converter; - -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionAnalysisResponseDto; -import com.bbteam.budgetbuddies.domain.consumptiongoal.entity.ConsumptionGoal; - -public class ConsumptionAnalysisConverter { - - public static ConsumptionAnalysisResponseDto fromEntity(ConsumptionGoal consumptionGoal, Long topAmount) { - - return ConsumptionAnalysisResponseDto.builder() - .goalCategory(consumptionGoal.getCategory().getName()) - .currentWeekConsumptionAmount(topAmount) - .build(); - } -} diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/ConsumptionGoalConverter.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/ConsumptionGoalConverter.java index 2def3e93..8ddeb5a3 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/ConsumptionGoalConverter.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/ConsumptionGoalConverter.java @@ -3,13 +3,17 @@ import java.time.LocalDate; import java.util.List; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.UserConsumptionGoalResponse; import org.springframework.stereotype.Component; import com.bbteam.budgetbuddies.domain.category.entity.Category; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionAnalysisResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseListDto; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.PeerInfoResponseDto; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDto; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.UserConsumptionGoalResponse; import com.bbteam.budgetbuddies.domain.consumptiongoal.entity.ConsumptionGoal; +import com.bbteam.budgetbuddies.enums.Gender; @Component public class ConsumptionGoalConverter { @@ -52,10 +56,39 @@ private Long sumTotalGoalAmount(List consumptionGoal public UserConsumptionGoalResponse toUserConsumptionGoalResponse(ConsumptionGoal consumptionGoal) { return UserConsumptionGoalResponse.builder() - .categoryId(consumptionGoal.getCategory().getId()) - .goalMonth(consumptionGoal.getGoalMonth()) - .consumeAmount(consumptionGoal.getConsumeAmount()) - .goalAmount(consumptionGoal.getGoalAmount()) - .build(); + .categoryId(consumptionGoal.getCategory().getId()) + .goalMonth(consumptionGoal.getGoalMonth()) + .consumeAmount(consumptionGoal.getConsumeAmount()) + .goalAmount(consumptionGoal.getGoalAmount()) + .build(); + } + + public ConsumptionAnalysisResponseDto toTopCategoryAndConsumptionAmount(ConsumptionGoal consumptionGoal, + Long topAmount) { + + return ConsumptionAnalysisResponseDto.builder() + .goalCategory(consumptionGoal.getCategory().getName()) + .currentWeekConsumptionAmount(topAmount) + .build(); + } + + public PeerInfoResponseDto toPeerInfo(int peerAgeStart, int peerAgeEnd, Gender peerGender) { + + return PeerInfoResponseDto.builder() + .peerAgeStart(peerAgeStart) + .peerAgeEnd(peerAgeEnd) + .peerGender(peerGender.name()) + .build(); + } + + public TopGoalCategoryResponseDto toTopGoalCategories(ConsumptionGoal consumptionGoal) { + if (consumptionGoal == null || consumptionGoal.getCategory() == null) { + return null; + } + + return TopGoalCategoryResponseDto.builder() + .categoryName(consumptionGoal.getCategory().getName()) + .goalAmount(consumptionGoal.getGoalAmount()) + .build(); } } diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/PeerInfoConverter.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/PeerInfoConverter.java deleted file mode 100644 index 2b66f373..00000000 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/PeerInfoConverter.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.bbteam.budgetbuddies.domain.consumptiongoal.converter; - -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.PeerInfoResponseDto; -import com.bbteam.budgetbuddies.enums.Gender; - -public class PeerInfoConverter { - - public static PeerInfoResponseDto fromEntity(int peerAgeStart, int peerAgeEnd, Gender peerGender) { - - return PeerInfoResponseDto.builder() - .peerAgeStart(peerAgeStart) - .peerAgeEnd(peerAgeEnd) - .peerGender(peerGender.name()) - .build(); - } -} diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/TopCategoryConverter.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/TopCategoryConverter.java deleted file mode 100644 index 2bed4bb6..00000000 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/TopCategoryConverter.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.bbteam.budgetbuddies.domain.consumptiongoal.converter; - -import org.springframework.stereotype.Component; - -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDto; -import com.bbteam.budgetbuddies.domain.consumptiongoal.entity.ConsumptionGoal; - -@Component -public class TopCategoryConverter { - - public static TopGoalCategoryResponseDto fromEntity(ConsumptionGoal consumptionGoal) { - if (consumptionGoal == null || consumptionGoal.getCategory() == null) { - return null; - } - - return TopGoalCategoryResponseDto.builder() - .categoryName(consumptionGoal.getCategory().getName()) - .goalAmount(consumptionGoal.getGoalAmount()) - .build(); - } -} diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java index bf9e614e..733704d9 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java @@ -16,10 +16,7 @@ import com.bbteam.budgetbuddies.domain.category.entity.Category; import com.bbteam.budgetbuddies.domain.category.repository.CategoryRepository; -import com.bbteam.budgetbuddies.domain.consumptiongoal.converter.ConsumptionAnalysisConverter; import com.bbteam.budgetbuddies.domain.consumptiongoal.converter.ConsumptionGoalConverter; -import com.bbteam.budgetbuddies.domain.consumptiongoal.converter.PeerInfoConverter; -import com.bbteam.budgetbuddies.domain.consumptiongoal.converter.TopCategoryConverter; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AvgConsumptionGoalDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionAnalysisResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalListRequestDto; @@ -69,7 +66,7 @@ public List getTopGoalCategoriesLimit(int top, Long List topGoals = consumptionGoalRepository.findTopCategoriesAndGoalAmountLimit(top, peerAgeStart, peerAgeEnd, peerGender, currentMonth); - return topGoals.stream().map(TopCategoryConverter::fromEntity).collect(Collectors.toList()); + return topGoals.stream().map(consumptionGoalConverter::toTopGoalCategories).collect(Collectors.toList()); } @Override @@ -122,7 +119,7 @@ public PeerInfoResponseDto getPeerInfo(Long userId, int peerAgeS, int peerAgeE, checkPeerInfo(userId, peerAgeS, peerAgeE, peerG); - return PeerInfoConverter.fromEntity(peerAgeStart, peerAgeEnd, peerGender); + return consumptionGoalConverter.toPeerInfo(peerAgeStart, peerAgeEnd, peerGender); } @Override @@ -141,7 +138,8 @@ public ConsumptionAnalysisResponseDto getTopCategoryAndConsumptionAmount(Long us Long totalConsumptionAmountForCurrentWeek = currentWeekConsumptionAmount.getConsumeAmount(); - return ConsumptionAnalysisConverter.fromEntity(topConsumptionGoal, totalConsumptionAmountForCurrentWeek); + return consumptionGoalConverter.toTopCategoryAndConsumptionAmount(topConsumptionGoal, + totalConsumptionAmountForCurrentWeek); } @Override From 90a7f8097566a0d9f214cd457909d8b67f565fbb Mon Sep 17 00:00:00 2001 From: MJJ Date: Mon, 12 Aug 2024 17:27:29 +0900 Subject: [PATCH 18/35] =?UTF-8?q?[refactor]=20=EB=98=90=EB=9E=98=EB=93=A4?= =?UTF-8?q?=EC=9D=B4=20=EA=B0=80=EC=9E=A5=20=ED=81=B0=20=EA=B3=84=ED=9A=8D?= =?UTF-8?q?=EC=9D=80=20=EC=84=B8=EC=9A=B4=20=EC=B9=B4=ED=85=8C=EA=B3=A0?= =?UTF-8?q?=EB=A6=AC=20=EC=A1=B0=ED=9A=8C=20top4=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../consumptiongoal/controller/ConsumptionGoalApi.java | 5 ++--- .../controller/ConsumptionGoalController.java | 10 +++++----- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalApi.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalApi.java index a2a6fbab..976a4a92 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalApi.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalApi.java @@ -17,12 +17,11 @@ public interface ConsumptionGoalApi { @Operation(summary = "또래들이 가장 큰 계획을 세운 카테고리 조회 Top4 API", description = "특정 사용자의 소비 목표 카테고리별 소비 목표 금액을 조회하는 API 입니다.") @ApiResponses(value = {@ApiResponse(responseCode = "COMMON200", description = "OK, 성공")}) - @Parameters({@Parameter(name = "top", description = "가장 큰 목표를 세운 카테고리의 개수를 지정합니다."), - @Parameter(name = "userId", description = "로그인 한 유저 아이디"), + @Parameters({@Parameter(name = "userId", description = "로그인 한 유저 아이디"), @Parameter(name = "peerAgeStart", description = "또래나이 시작 범위"), @Parameter(name = "peerAgeEnd", description = "또래나이 끝 범위"), @Parameter(name = "peerGender", description = "또래 성별")}) - ResponseEntity getTopGoalCategoriesList(int top, Long userId, + ResponseEntity getTopConsumptionGoalCategories(Long userId, int peerAgeStart, int peerAgeEnd, String peerGender); @Operation(summary = "또래들이 가장 많이 계획한 카테고리와 평균 금액 및 내 목표금액 차이 조회 API", description = "특정 사용자의 또래 소비 카테고리별 평균 목표 금액을 조회하는 API 입니다.") diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java index 72ad8158..0afa778c 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java @@ -30,14 +30,14 @@ public class ConsumptionGoalController implements ConsumptionGoalApi { private final ConsumptionGoalService consumptionGoalService; @Override - @GetMapping("/top-categories/top-goal/{top}") - public ResponseEntity getTopGoalCategoriesList(@PathVariable(name = "top") int top, - @RequestParam(name = "userId") Long userId, + @GetMapping("/top-categories/top-goal/4") + public ResponseEntity getTopConsumptionGoalCategories(@RequestParam(name = "userId") Long userId, @RequestParam(name = "peerAgeStart", defaultValue = "0") int peerAgeStart, @RequestParam(name = "peerAgeEnd", defaultValue = "0") int peerAgeEnd, @RequestParam(name = "peerGender", defaultValue = "none") String peerGender) { - List topCategoriesList = consumptionGoalService.getTopGoalCategoriesLimit(top, - userId, peerAgeStart, peerAgeEnd, peerGender); + List topCategoriesList = consumptionGoalService.getTopConsumptionGoalCategories( + userId, + peerAgeStart, peerAgeEnd, peerGender); return ResponseEntity.ok(topCategoriesList); } From 11e6ae3f78177cd6af7285400455935fed40b3c3 Mon Sep 17 00:00:00 2001 From: MJJ Date: Mon, 12 Aug 2024 17:28:14 +0900 Subject: [PATCH 19/35] =?UTF-8?q?[refactor]=20=EB=98=90=EB=9E=98=EB=93=A4?= =?UTF-8?q?=EC=9D=B4=20=EA=B0=80=EC=9E=A5=20=ED=81=B0=20=EA=B3=84=ED=9A=8D?= =?UTF-8?q?=EC=9D=80=20=EC=84=B8=EC=9A=B4=20=EC=B9=B4=ED=85=8C=EA=B3=A0?= =?UTF-8?q?=EB=A6=AC=20=EC=A1=B0=ED=9A=8C=20top4=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - top파라미터 제거 - 메서드명 수정 --- .../domain/consumptiongoal/service/ConsumptionGoalService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java index f59e1bdf..c042fe27 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java @@ -18,7 +18,7 @@ @Service public interface ConsumptionGoalService { - List getTopGoalCategoriesLimit(int top, Long userId, int peerAgeStart, int peerAgeEnd, + List getTopConsumptionGoalCategories(Long userId, int peerAgeStart, int peerAgeEnd, String peerGender); List getAllConsumptionGoalCategories(Long userId, int peerAgeS, int peerAgeE, From 846313a1b36950686ed2c4fbbea02131821f07f8 Mon Sep 17 00:00:00 2001 From: MJJ Date: Mon, 12 Aug 2024 17:30:17 +0900 Subject: [PATCH 20/35] =?UTF-8?q?[refactor]=20=EB=98=90=EB=9E=98=EB=93=A4?= =?UTF-8?q?=EC=9D=B4=20=EA=B0=80=EC=9E=A5=20=ED=81=B0=20=EA=B3=84=ED=9A=8D?= =?UTF-8?q?=EC=9D=80=20=EC=84=B8=EC=9A=B4=20=EC=B9=B4=ED=85=8C=EA=B3=A0?= =?UTF-8?q?=EB=A6=AC=20=EC=A1=B0=ED=9A=8C=20top4=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 또래의 이번 달 카테고리 평균 목표 금액 별 상위 4개 반환 수정 - 메서드명 수정 --- .../service/ConsumptionGoalServiceImpl.java | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java index 733704d9..0c757d83 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java @@ -4,6 +4,7 @@ import java.time.LocalDate; import java.time.temporal.TemporalAdjusters; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; @@ -59,14 +60,21 @@ public class ConsumptionGoalServiceImpl implements ConsumptionGoalService { @Override @Transactional(readOnly = true) - public List getTopGoalCategoriesLimit(int top, Long userId, int peerAgeS, int peerAgeE, + public List getTopConsumptionGoalCategories(Long userId, int peerAgeS, int peerAgeE, String peerG) { checkPeerInfo(userId, peerAgeS, peerAgeE, peerG); - List topGoals = consumptionGoalRepository.findTopCategoriesAndGoalAmountLimit(top, - peerAgeStart, peerAgeEnd, peerGender, currentMonth); - return topGoals.stream().map(consumptionGoalConverter::toTopGoalCategories).collect(Collectors.toList()); + List categoryAvgList = getAvgGoalAmount(); + + return categoryAvgList.stream() + .sorted(Comparator.comparing(AvgConsumptionGoalDto::getAverageAmount).reversed()) + .limit(4) + .map(avgGoal -> TopGoalCategoryResponseDto.builder() + .categoryName(getCategoryNameById(avgGoal.getCategoryId())) + .goalAmount(avgGoal.getAverageAmount()) + .build()) + .collect(Collectors.toList()); } @Override @@ -128,8 +136,9 @@ public ConsumptionAnalysisResponseDto getTopCategoryAndConsumptionAmount(Long us checkPeerInfo(userId, 0, 0, "none"); - ConsumptionGoal topConsumptionGoal = consumptionGoalRepository.findTopCategoriesAndGoalAmountLimit(1, - peerAgeStart, peerAgeEnd, peerGender, currentMonth).get(0); + ConsumptionGoal topConsumptionGoal = consumptionGoalRepository.findTopCategoriesAndAvgGoalAmount(1, + peerAgeStart, + peerAgeEnd, peerGender, currentMonth).get(0); ConsumptionGoal currentWeekConsumptionAmount = consumptionGoalRepository.findTopConsumptionByCategoryIdAndCurrentWeek( topConsumptionGoal.getCategory().getId(), startOfWeek, endOfWeek) @@ -311,6 +320,12 @@ private List getMyGoalAmount(Long userId) { return myConsumptionAmountList; } + private String getCategoryNameById(Long categoryId) { + Category category = categoryRepository.findById(categoryId) + .orElseThrow(() -> new RuntimeException("카테고리 " + categoryId + "를 찾을 수 없습니다.: ")); + return category.getName(); + } + @Override @Transactional public ConsumptionGoalResponseListDto updateConsumptionGoals(Long userId, From f01b305109750c5a195e321f52dc5cfd4ed18875 Mon Sep 17 00:00:00 2001 From: MJJ Date: Mon, 12 Aug 2024 22:12:57 +0900 Subject: [PATCH 21/35] =?UTF-8?q?[refactor]=20=EB=98=90=EB=9E=98=EB=93=A4?= =?UTF-8?q?=EC=9D=B4=20=EA=B0=80=EC=9E=A5=20=ED=81=B0=20=EB=AA=A9=ED=91=9C?= =?UTF-8?q?=EB=A1=9C=20=EC=84=B8=EC=9A=B4=20=EC=B9=B4=ED=85=8C=EA=B3=A0?= =?UTF-8?q?=EB=A6=AC=EC=99=80=20=EA=B7=B8=20=EC=B9=B4=ED=85=8C=EA=B3=A0?= =?UTF-8?q?=EB=A6=AC=EC=97=90=EC=84=9C=20=EC=9D=B4=EB=B2=88=EC=A3=BC=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=ED=95=9C=20=EA=B8=88=EC=95=A1=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20API=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ConsumptionGoalApi.java | 5 +++++ .../controller/ConsumptionGoalController.java | 14 ++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalApi.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalApi.java index 976a4a92..e9bd1bb2 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalApi.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalApi.java @@ -3,6 +3,7 @@ import java.time.LocalDate; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PathVariable; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalListRequestDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseListDto; @@ -57,4 +58,8 @@ ResponseEntity updateOrElseGenerateConsumptionGo @Parameter(name = "peerGender", description = "또래 성별")}) ResponseEntity getAllConsumptionCategories(Long userId, int peerAgeStart, int peerAgeEnd, String peerGender); + + @Operation(summary = "또래들이 가장 큰 목표로 세운 카테고리와 그 카테고리에서 이번주 사용한 금액 조회 API", description = "특정 사용자의 또래 소비 카테고리별 이번주 소비 금액을 조회하는 API 입니다.") + @ApiResponses(value = {@ApiResponse(responseCode = "COMMON200", description = "OK, 성공")}) + ResponseEntity getTopCategoryAndConsumptionAmount(@PathVariable Long userId); } \ No newline at end of file diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java index 0afa778c..ccca6ef1 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java @@ -36,8 +36,7 @@ public ResponseEntity getTopConsumptionGoalCategories(@RequestParam(name = "u @RequestParam(name = "peerAgeEnd", defaultValue = "0") int peerAgeEnd, @RequestParam(name = "peerGender", defaultValue = "none") String peerGender) { List topCategoriesList = consumptionGoalService.getTopConsumptionGoalCategories( - userId, - peerAgeStart, peerAgeEnd, peerGender); + userId, peerAgeStart, peerAgeEnd, peerGender); return ResponseEntity.ok(topCategoriesList); } @@ -47,8 +46,7 @@ public ResponseEntity getAllConsumptionGoalCategories(@RequestParam(name = "u @RequestParam(name = "peerAgeEnd", defaultValue = "0") int peerAgeEnd, @RequestParam(name = "peerGender", defaultValue = "none") String peerGender) { List response = consumptionGoalService.getAllConsumptionGoalCategories(userId, - peerAgeStart, - peerAgeEnd, peerGender); + peerAgeStart, peerAgeEnd, peerGender); return ResponseEntity.ok(response); } @@ -86,8 +84,12 @@ public ResponseEntity getAllConsumptionCategories(@RequestParam(name = "userI @RequestParam(name = "peerAgeEnd", defaultValue = "0") int peerAgeEnd, @RequestParam(name = "peerGender", defaultValue = "none") String peerGender) { List response = consumptionGoalService.getAllConsumptionCategories(userId, - peerAgeStart, - peerAgeEnd, peerGender); + peerAgeStart, peerAgeEnd, peerGender); return ResponseEntity.ok(response); } + + @GetMapping("/top-categories/top-consumption/{userId}") + public ResponseEntity getTopCategoryAndConsumptionAmount(@PathVariable Long userId) { + return ResponseEntity.ok(consumptionGoalService.getTopCategoryAndConsumptionAmount(userId)); + } } \ No newline at end of file From 64d078408d9d374c0981348a8f9da72e5ece0a33 Mon Sep 17 00:00:00 2001 From: MJJ Date: Mon, 12 Aug 2024 22:13:21 +0900 Subject: [PATCH 22/35] =?UTF-8?q?[refactor]=20toTopCategoryAndConsumptionA?= =?UTF-8?q?mount=20=ED=8C=8C=EB=9D=BC=EB=AF=B8=ED=84=B0=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../consumptiongoal/converter/ConsumptionGoalConverter.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/ConsumptionGoalConverter.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/ConsumptionGoalConverter.java index 8ddeb5a3..3759fa56 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/ConsumptionGoalConverter.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/ConsumptionGoalConverter.java @@ -63,11 +63,11 @@ public UserConsumptionGoalResponse toUserConsumptionGoalResponse(ConsumptionGoal .build(); } - public ConsumptionAnalysisResponseDto toTopCategoryAndConsumptionAmount(ConsumptionGoal consumptionGoal, + public ConsumptionAnalysisResponseDto toTopCategoryAndConsumptionAmount(String categoryName, Long topAmount) { return ConsumptionAnalysisResponseDto.builder() - .goalCategory(consumptionGoal.getCategory().getName()) + .goalCategory(categoryName) .currentWeekConsumptionAmount(topAmount) .build(); } From 786c34dddf8d4becd8788afdea2764d4e0084a29 Mon Sep 17 00:00:00 2001 From: MJJ Date: Mon, 12 Aug 2024 22:14:36 +0900 Subject: [PATCH 23/35] =?UTF-8?q?[refactor]=20=ED=8A=B9=EC=A0=95=20?= =?UTF-8?q?=EC=B9=B4=ED=85=8C=EA=B3=A0=EB=A6=AC=EC=9D=98=20=EC=9D=B4?= =?UTF-8?q?=EB=B2=88=20=EC=A3=BC=20=EB=98=90=EB=9E=98=20=EB=B3=84=20?= =?UTF-8?q?=ED=8F=89=EA=B7=A0=20=EC=86=8C=EB=B9=84=20=EA=B8=88=EC=95=A1=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20=EA=B5=AC=ED=98=84,=20=EA=B0=80=EC=9E=A5?= =?UTF-8?q?=20=EB=86=92=EC=9D=80=20=EB=AA=A9=ED=91=9C=20=EA=B8=88=EC=95=A1?= =?UTF-8?q?=20=EB=A6=AC=EC=8A=A4=ED=8A=B8=20=ED=95=A8=EC=88=98=20=EC=BF=BC?= =?UTF-8?q?=EB=A6=AC=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/ConsumptionGoalRepository.java | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java index 17187ddf..0857799a 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java @@ -19,23 +19,22 @@ @Repository public interface ConsumptionGoalRepository extends JpaRepository { - @Query("SELECT cg FROM ConsumptionGoal cg " + "WHERE cg.category.isDefault = true " - + "AND cg.user.age BETWEEN :peerAgeStart AND :peerAgeEnd " + "AND cg.user.gender = :peerGender " - + "AND cg.goalMonth >= :currentMonth " + "ORDER BY cg.goalAmount DESC limit :top") - List findTopCategoriesAndGoalAmountLimit(@Param("top") int top, - @Param("peerAgeStart") int peerAgeStart, @Param("peerAgeEnd") int peerAgeEnd, - @Param("peerGender") Gender peerGender, @Param("currentMonth") LocalDate currentMonth); - @Query(value = "SELECT cg FROM ConsumptionGoal AS cg WHERE cg.user.id = :userId AND cg.goalMonth = :goalMonth") List findConsumptionGoalByUserIdAndGoalMonth(Long userId, LocalDate goalMonth); Optional findConsumptionGoalByUserAndCategoryAndGoalMonth(User user, Category category, LocalDate goalMonth); - @Query("SELECT cg FROM ConsumptionGoal cg JOIN cg.category c WHERE c.id = :categoryId AND cg.goalMonth " - + "BETWEEN :startOfWeek AND :endOfWeek ORDER BY cg.consumeAmount DESC limit 1") - Optional findTopConsumptionByCategoryIdAndCurrentWeek(@Param("categoryId") Long categoryId, - @Param("startOfWeek") LocalDate startOfWeek, @Param("endOfWeek") LocalDate endOfWeek); + @Query("SELECT AVG(cg.consumeAmount) FROM ConsumptionGoal cg " + + "JOIN cg.category c " + + "WHERE c.id = :categoryId " + + "AND cg.goalMonth BETWEEN :startOfWeek AND :endOfWeek " + + "AND cg.user.age BETWEEN :peerAgeStart AND :peerAgeEnd " + + "AND cg.user.gender = :peerGender") + Optional findAvgConsumptionByCategoryIdAndCurrentWeek( + @Param("categoryId") Long categoryId, @Param("startOfWeek") LocalDate startOfWeek, + @Param("endOfWeek") LocalDate endOfWeek, @Param("peerAgeStart") int peerAgeStart, + @Param("peerAgeEnd") int peerAgeEnd, @Param("peerGender") Gender peerGender); @Query( "SELECT new com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AvgConsumptionGoalDto(cg.category.id, AVG(cg.consumeAmount))" From b1795532b4a44c328d948fa5f83cb24e79d86035 Mon Sep 17 00:00:00 2001 From: MJJ Date: Mon, 12 Aug 2024 22:15:39 +0900 Subject: [PATCH 24/35] =?UTF-8?q?[refactor]=20=EB=98=90=EB=9E=98=EA=B0=80?= =?UTF-8?q?=20=EA=B0=80=EC=9E=A5=20=ED=81=B0=20=EB=AA=A9=ED=91=9C=EB=A1=9C?= =?UTF-8?q?=20=EC=84=B8=EC=9A=B4=20=EC=B9=B4=ED=85=8C=EA=B3=A0=EB=A6=AC?= =?UTF-8?q?=EC=99=80=20=EA=B7=B8=20=EC=B9=B4=ED=85=8C=EA=B3=A0=EB=A6=AC?= =?UTF-8?q?=EC=97=90=EC=84=9C=20=EC=9D=B4=EB=B2=88=EC=A3=BC=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=ED=95=9C=20=EA=B8=88=EC=95=A1=20=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?API=20service=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/ConsumptionGoalServiceImpl.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java index 0c757d83..703f4665 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java @@ -136,19 +136,21 @@ public ConsumptionAnalysisResponseDto getTopCategoryAndConsumptionAmount(Long us checkPeerInfo(userId, 0, 0, "none"); - ConsumptionGoal topConsumptionGoal = consumptionGoalRepository.findTopCategoriesAndAvgGoalAmount(1, + List avgConsumptionGoalList = consumptionGoalRepository.findAvgGoalAmountByCategory( peerAgeStart, - peerAgeEnd, peerGender, currentMonth).get(0); + peerAgeEnd, peerGender, currentMonth); - ConsumptionGoal currentWeekConsumptionAmount = consumptionGoalRepository.findTopConsumptionByCategoryIdAndCurrentWeek( - topConsumptionGoal.getCategory().getId(), startOfWeek, endOfWeek) - .orElseThrow(() -> new IllegalArgumentException( - "카테고리 ID " + topConsumptionGoal.getCategory().getId() + "에 대한 현재 주 소비 데이터가 없습니다.")); + Long topConsumptionGoalCategoryId = avgConsumptionGoalList.get(0).getCategoryId(); - Long totalConsumptionAmountForCurrentWeek = currentWeekConsumptionAmount.getConsumeAmount(); + Long currentWeekConsumptionAmount = consumptionGoalRepository + .findAvgConsumptionByCategoryIdAndCurrentWeek(topConsumptionGoalCategoryId, startOfWeek, endOfWeek, + peerAgeStart, peerAgeEnd, peerGender) + .orElse(0L); - return consumptionGoalConverter.toTopCategoryAndConsumptionAmount(topConsumptionGoal, - totalConsumptionAmountForCurrentWeek); + String topGoalCategory = getCategoryNameById(topConsumptionGoalCategoryId); + + return consumptionGoalConverter.toTopCategoryAndConsumptionAmount(topGoalCategory, + currentWeekConsumptionAmount); } @Override From 0b0f6422b8c168ee63efbbf31ca39f2312d40d12 Mon Sep 17 00:00:00 2001 From: MJJ Date: Mon, 12 Aug 2024 22:16:53 +0900 Subject: [PATCH 25/35] =?UTF-8?q?[refactor]=20toTopGoalCategories=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0=20>=20builder=20=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../converter/ConsumptionGoalConverter.java | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/ConsumptionGoalConverter.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/ConsumptionGoalConverter.java index 3759fa56..26ef9c81 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/ConsumptionGoalConverter.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/ConsumptionGoalConverter.java @@ -10,7 +10,6 @@ import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseListDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.PeerInfoResponseDto; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.UserConsumptionGoalResponse; import com.bbteam.budgetbuddies.domain.consumptiongoal.entity.ConsumptionGoal; import com.bbteam.budgetbuddies.enums.Gender; @@ -80,15 +79,4 @@ public PeerInfoResponseDto toPeerInfo(int peerAgeStart, int peerAgeEnd, Gender p .peerGender(peerGender.name()) .build(); } - - public TopGoalCategoryResponseDto toTopGoalCategories(ConsumptionGoal consumptionGoal) { - if (consumptionGoal == null || consumptionGoal.getCategory() == null) { - return null; - } - - return TopGoalCategoryResponseDto.builder() - .categoryName(consumptionGoal.getCategory().getName()) - .goalAmount(consumptionGoal.getGoalAmount()) - .build(); - } } From d9075f40f26df7d96f66bb76a6c4be653a034b42 Mon Sep 17 00:00:00 2001 From: MJJ Date: Mon, 12 Aug 2024 22:35:44 +0900 Subject: [PATCH 26/35] =?UTF-8?q?[refactor]=20TopConsumptionResponseDto=20?= =?UTF-8?q?>=20AllConsumptionCategoryResponseDto=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=EB=AA=85=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...=> AllConsumptionCategoryResponseDto.java} | 6 ++--- .../service/ConsumptionGoalService.java | 9 ++++--- .../service/ConsumptionGoalServiceImpl.java | 27 ++++++++++++------- 3 files changed, 27 insertions(+), 15 deletions(-) rename src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/{TopConsumptionResponseDto.java => AllConsumptionCategoryResponseDto.java} (70%) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/TopConsumptionResponseDto.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/AllConsumptionCategoryResponseDto.java similarity index 70% rename from src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/TopConsumptionResponseDto.java rename to src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/AllConsumptionCategoryResponseDto.java index 10a1903e..0ed7b0df 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/TopConsumptionResponseDto.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/AllConsumptionCategoryResponseDto.java @@ -9,11 +9,11 @@ @Builder @NoArgsConstructor @AllArgsConstructor -public class TopConsumptionResponseDto { +public class AllConsumptionCategoryResponseDto { private String categoryName; - private Long avgConsumeAmount; + private Long avgAmount; - private Long consumeAmountDifference; + private Long amountDifference; } diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java index c042fe27..29ec2672 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java @@ -5,11 +5,11 @@ import org.springframework.stereotype.Service; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AllConsumptionCategoryResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionAnalysisResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalListRequestDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseListDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.PeerInfoResponseDto; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopConsumptionResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDto; import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseUpdateRequestDto; import com.bbteam.budgetbuddies.domain.expense.entity.Expense; @@ -21,7 +21,7 @@ public interface ConsumptionGoalService { List getTopConsumptionGoalCategories(Long userId, int peerAgeStart, int peerAgeEnd, String peerGender); - List getAllConsumptionGoalCategories(Long userId, int peerAgeS, int peerAgeE, + List getAllConsumptionGoalCategories(Long userId, int peerAgeS, int peerAgeE, String peerG); ConsumptionGoalResponseListDto findUserConsumptionGoalList(Long userId, LocalDate date); @@ -39,6 +39,9 @@ ConsumptionGoalResponseListDto updateConsumptionGoals(Long userId, void decreaseConsumeAmount(Long userId, Long categoryId, Long amount, LocalDate expenseDate); - List getAllConsumptionCategories(Long userId, int peerAgeS, int peerAgeE, + List getTopConsumptionCategories(Long userId, int peerAgeStart, int peerAgeEnd, + String peerGender); + + List getAllConsumptionCategories(Long userId, int peerAgeS, int peerAgeE, String peerG); } diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java index 703f4665..645e352d 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java @@ -18,6 +18,7 @@ import com.bbteam.budgetbuddies.domain.category.entity.Category; import com.bbteam.budgetbuddies.domain.category.repository.CategoryRepository; import com.bbteam.budgetbuddies.domain.consumptiongoal.converter.ConsumptionGoalConverter; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AllConsumptionCategoryResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AvgConsumptionGoalDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionAnalysisResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalListRequestDto; @@ -26,7 +27,6 @@ import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseListDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.MyConsumptionGoalDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.PeerInfoResponseDto; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopConsumptionResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.entity.ConsumptionGoal; import com.bbteam.budgetbuddies.domain.consumptiongoal.repository.ConsumptionGoalRepository; @@ -79,7 +79,8 @@ public List getTopConsumptionGoalCategories(Long use @Override @Transactional(readOnly = true) - public List getAllConsumptionGoalCategories(Long userId, int peerAgeS, int peerAgeE, + public List getAllConsumptionGoalCategories(Long userId, int peerAgeS, + int peerAgeE, String peerG) { checkPeerInfo(userId, peerAgeS, peerAgeE, peerG); @@ -111,10 +112,10 @@ public List getAllConsumptionGoalCategories(Long user consumeAmountDifference = myConsumeAmount - avgConsumeAmount; } - return TopConsumptionResponseDto.builder() + return AllConsumptionCategoryResponseDto.builder() .categoryName(category.getName()) - .avgConsumeAmount(avgConsumeAmount) - .consumeAmountDifference(consumeAmountDifference) + .avgAmount(avgConsumeAmount) + .amountDifference(consumeAmountDifference) .build(); }) .collect(Collectors.toList()); @@ -155,7 +156,15 @@ public ConsumptionAnalysisResponseDto getTopCategoryAndConsumptionAmount(Long us @Override @Transactional(readOnly = true) - public List getAllConsumptionCategories(Long userId, int peerAgeS, int peerAgeE, + public List getTopConsumptionCategories(Long userId, int peerAgeStart, int peerAgeEnd, + String peerGender) { + // TODO + return List.of(); + } + + @Override + @Transactional(readOnly = true) + public List getAllConsumptionCategories(Long userId, int peerAgeS, int peerAgeE, String peerG) { checkPeerInfo(userId, peerAgeS, peerAgeE, peerG); @@ -188,10 +197,10 @@ public List getAllConsumptionCategories(Long userId, consumeAmountDifference = myConsumeAmount - avgConsumeAmount; } - return TopConsumptionResponseDto.builder() + return AllConsumptionCategoryResponseDto.builder() .categoryName(category.getName()) - .avgConsumeAmount(avgConsumeAmount) - .consumeAmountDifference(consumeAmountDifference) + .avgAmount(avgConsumeAmount) + .amountDifference(consumeAmountDifference) .build(); }) .collect(Collectors.toList()); From 55a45d13dcff1f1736bd452633e24824db9cce9b Mon Sep 17 00:00:00 2001 From: MJJ Date: Tue, 13 Aug 2024 13:06:46 +0900 Subject: [PATCH 27/35] =?UTF-8?q?[refactor]=20=EB=98=90=EB=9E=98=EB=93=A4?= =?UTF-8?q?=EC=9D=B4=20=EA=B0=80=EC=9E=A5=20=EB=A7=8E=EC=9D=B4=ED=95=9C=20?= =?UTF-8?q?=EC=86=8C=EB=B9=84=20=EC=B9=B4=ED=85=8C=EA=B3=A0=EB=A6=AC?= =?UTF-8?q?=EC=99=80=20=EC=86=8C=EB=B9=84=20=EA=B1=B4=EC=88=98=20dto=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/CategoryConsumptionCountDto.java | 13 +++++++++++++ .../dto/TopCategoryConsumptionDto.java | 15 +++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/CategoryConsumptionCountDto.java create mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/TopCategoryConsumptionDto.java diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/CategoryConsumptionCountDto.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/CategoryConsumptionCountDto.java new file mode 100644 index 00000000..05068c32 --- /dev/null +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/CategoryConsumptionCountDto.java @@ -0,0 +1,13 @@ +package com.bbteam.budgetbuddies.domain.consumptiongoal.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class CategoryConsumptionCountDto { + private Long categoryId; + private Long consumptionCount; +} diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/TopCategoryConsumptionDto.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/TopCategoryConsumptionDto.java new file mode 100644 index 00000000..d50c88e6 --- /dev/null +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/TopCategoryConsumptionDto.java @@ -0,0 +1,15 @@ +package com.bbteam.budgetbuddies.domain.consumptiongoal.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class TopCategoryConsumptionDto { + private String categoryName; + private Long consumptionCount; +} From 19d03f86e2d11c0f23c6ac7df18f64edc773588e Mon Sep 17 00:00:00 2001 From: MJJ Date: Tue, 13 Aug 2024 13:07:13 +0900 Subject: [PATCH 28/35] =?UTF-8?q?[refactor]=20=EB=98=90=EB=9E=98=EB=93=A4?= =?UTF-8?q?=EC=9D=B4=20=EA=B0=80=EC=9E=A5=20=EB=A7=8E=EC=9D=B4=ED=95=9C=20?= =?UTF-8?q?=EC=86=8C=EB=B9=84=20=EC=B9=B4=ED=85=8C=EA=B3=A0=EB=A6=AC?= =?UTF-8?q?=EC=99=80=20=EC=86=8C=EB=B9=84=20=EA=B1=B4=EC=88=98=20top3=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ConsumptionGoalApi.java | 17 +++++++++++------ .../controller/ConsumptionGoalController.java | 19 ++++++++++++++++--- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalApi.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalApi.java index e9bd1bb2..c14c7262 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalApi.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalApi.java @@ -22,8 +22,7 @@ public interface ConsumptionGoalApi { @Parameter(name = "peerAgeStart", description = "또래나이 시작 범위"), @Parameter(name = "peerAgeEnd", description = "또래나이 끝 범위"), @Parameter(name = "peerGender", description = "또래 성별")}) - ResponseEntity getTopConsumptionGoalCategories(Long userId, - int peerAgeStart, int peerAgeEnd, String peerGender); + ResponseEntity getTopConsumptionGoalCategories(Long userId, int peerAgeStart, int peerAgeEnd, String peerGender); @Operation(summary = "또래들이 가장 많이 계획한 카테고리와 평균 금액 및 내 목표금액 차이 조회 API", description = "특정 사용자의 또래 소비 카테고리별 평균 목표 금액을 조회하는 API 입니다.") @ApiResponses(value = {@ApiResponse(responseCode = "COMMON200", description = "OK, 성공")}) @@ -31,8 +30,7 @@ ResponseEntity getTopConsumptionGoalCategories(Long userId, @Parameter(name = "peerAgeStart", description = "또래나이 시작 범위"), @Parameter(name = "peerAgeEnd", description = "또래나이 끝 범위"), @Parameter(name = "peerGender", description = "또래 성별")}) - ResponseEntity getAllConsumptionGoalCategories(Long userId, int peerAgeStart, int peerAgeEnd, - String peerGender); + ResponseEntity getAllConsumptionGoalCategories(Long userId, int peerAgeStart, int peerAgeEnd, String peerGender); @Operation(summary = "또래나이와 성별 조회 API", description = "또래나이와 성별을 조회하는 API 입니다.") @ApiResponses(value = {@ApiResponse(responseCode = "COMMON200", description = "OK, 성공")}) @@ -50,14 +48,21 @@ ResponseEntity getAllConsumptionGoalCategories(Long userId, int peerAgeStart, ResponseEntity updateOrElseGenerateConsumptionGoal(Long userId, ConsumptionGoalListRequestDto consumptionGoalListRequestDto); + @Operation(summary = "또래들이 가장 많이한 소비 카테고리 조회 Top3 API", description = "특정 사용자의 또래 소비 카테고리별 소비 건 수을 조회하는 API 입니다.") + @ApiResponses(value = {@ApiResponse(responseCode = "COMMON200", description = "OK, 성공")}) + @Parameters({@Parameter(name = "userId", description = "로그인 한 유저 아이디"), + @Parameter(name = "peerAgeStart", description = "또래나이 시작 범위"), + @Parameter(name = "peerAgeEnd", description = "또래나이 끝 범위"), + @Parameter(name = "peerGender", description = "또래 성별")}) + ResponseEntity getTopConsumptionCategories(Long userId, int peerAgeStart, int peerAgeEnd, String peerGender); + @Operation(summary = "또래들이 가장 많이한 소비 카테고리와 평균 금액 및 내 소비금액 차이 조회 API", description = "특정 사용자의 또래 소비 카테고리별 평균 소비 금액을 조회하는 API 입니다.") @ApiResponses(value = {@ApiResponse(responseCode = "COMMON200", description = "OK, 성공")}) @Parameters({@Parameter(name = "userId", description = "로그인 한 유저 아이디"), @Parameter(name = "peerAgeStart", description = "또래나이 시작 범위"), @Parameter(name = "peerAgeEnd", description = "또래나이 끝 범위"), @Parameter(name = "peerGender", description = "또래 성별")}) - ResponseEntity getAllConsumptionCategories(Long userId, int peerAgeStart, int peerAgeEnd, - String peerGender); + ResponseEntity getAllConsumptionCategories(Long userId, int peerAgeStart, int peerAgeEnd, String peerGender); @Operation(summary = "또래들이 가장 큰 목표로 세운 카테고리와 그 카테고리에서 이번주 사용한 금액 조회 API", description = "특정 사용자의 또래 소비 카테고리별 이번주 소비 금액을 조회하는 API 입니다.") @ApiResponses(value = {@ApiResponse(responseCode = "COMMON200", description = "OK, 성공")}) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java index ccca6ef1..04add868 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/controller/ConsumptionGoalController.java @@ -13,10 +13,11 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AllConsumptionCategoryResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalListRequestDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseListDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.PeerInfoResponseDto; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopConsumptionResponseDto; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopCategoryConsumptionDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.service.ConsumptionGoalService; @@ -45,7 +46,8 @@ public ResponseEntity getAllConsumptionGoalCategories(@RequestParam(name = "u @RequestParam(name = "peerAgeStart", defaultValue = "0") int peerAgeStart, @RequestParam(name = "peerAgeEnd", defaultValue = "0") int peerAgeEnd, @RequestParam(name = "peerGender", defaultValue = "none") String peerGender) { - List response = consumptionGoalService.getAllConsumptionGoalCategories(userId, + List response = consumptionGoalService.getAllConsumptionGoalCategories( + userId, peerAgeStart, peerAgeEnd, peerGender); return ResponseEntity.ok(response); } @@ -78,12 +80,22 @@ public ResponseEntity updateOrElseGenerateConsum .body(consumptionGoalService.updateConsumptionGoals(userId, consumptionGoalListRequestDto)); } + @GetMapping("/top-categories/top-consumption/3") + public ResponseEntity getTopConsumptionCategories(@RequestParam(name = "userId") Long userId, + @RequestParam(name = "peerAgeStart", defaultValue = "0") int peerAgeStart, + @RequestParam(name = "peerAgeEnd", defaultValue = "0") int peerAgeEnd, + @RequestParam(name = "peerGender", defaultValue = "none") String peerGender) { + List topCategoriesList = consumptionGoalService.getTopConsumptionCategories(userId, + peerAgeStart, peerAgeEnd, peerGender); + return ResponseEntity.ok(topCategoriesList); + } + @GetMapping("/top-categories/top-consumption") public ResponseEntity getAllConsumptionCategories(@RequestParam(name = "userId") Long userId, @RequestParam(name = "peerAgeStart", defaultValue = "0") int peerAgeStart, @RequestParam(name = "peerAgeEnd", defaultValue = "0") int peerAgeEnd, @RequestParam(name = "peerGender", defaultValue = "none") String peerGender) { - List response = consumptionGoalService.getAllConsumptionCategories(userId, + List response = consumptionGoalService.getAllConsumptionCategories(userId, peerAgeStart, peerAgeEnd, peerGender); return ResponseEntity.ok(response); } @@ -92,4 +104,5 @@ public ResponseEntity getAllConsumptionCategories(@RequestParam(name = "userI public ResponseEntity getTopCategoryAndConsumptionAmount(@PathVariable Long userId) { return ResponseEntity.ok(consumptionGoalService.getTopCategoryAndConsumptionAmount(userId)); } + } \ No newline at end of file From 9835523260b055a113a810c6abd75f0bc70355c2 Mon Sep 17 00:00:00 2001 From: MJJ Date: Tue, 13 Aug 2024 13:08:05 +0900 Subject: [PATCH 29/35] =?UTF-8?q?[refactor]=20=EB=98=90=EB=9E=98=EB=93=A4?= =?UTF-8?q?=EC=9D=B4=20=EA=B0=80=EC=9E=A5=20=EB=A7=8E=EC=9D=B4=ED=95=9C=20?= =?UTF-8?q?=EC=86=8C=EB=B9=84=20=EC=B9=B4=ED=85=8C=EA=B3=A0=EB=A6=AC?= =?UTF-8?q?=EC=99=80=20=EC=86=8C=EB=B9=84=20=EA=B1=B4=EC=88=98=20repositor?= =?UTF-8?q?y=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/ConsumptionGoalRepository.java | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java index 0857799a..00986dd8 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepository.java @@ -11,6 +11,7 @@ import com.bbteam.budgetbuddies.domain.category.entity.Category; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AvgConsumptionGoalDto; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.CategoryConsumptionCountDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.MyConsumptionGoalDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.entity.ConsumptionGoal; import com.bbteam.budgetbuddies.domain.user.entity.User; @@ -25,19 +26,17 @@ public interface ConsumptionGoalRepository extends JpaRepository findConsumptionGoalByUserAndCategoryAndGoalMonth(User user, Category category, LocalDate goalMonth); - @Query("SELECT AVG(cg.consumeAmount) FROM ConsumptionGoal cg " + - "JOIN cg.category c " + - "WHERE c.id = :categoryId " + - "AND cg.goalMonth BETWEEN :startOfWeek AND :endOfWeek " + - "AND cg.user.age BETWEEN :peerAgeStart AND :peerAgeEnd " + - "AND cg.user.gender = :peerGender") + @Query("SELECT AVG(cg.consumeAmount) FROM ConsumptionGoal cg " + "JOIN cg.category c " + "WHERE c.id = :categoryId " + + "AND cg.goalMonth BETWEEN :startOfWeek AND :endOfWeek " + + "AND cg.user.age BETWEEN :peerAgeStart AND :peerAgeEnd " + "AND cg.user.gender = :peerGender") Optional findAvgConsumptionByCategoryIdAndCurrentWeek( @Param("categoryId") Long categoryId, @Param("startOfWeek") LocalDate startOfWeek, @Param("endOfWeek") LocalDate endOfWeek, @Param("peerAgeStart") int peerAgeStart, @Param("peerAgeEnd") int peerAgeEnd, @Param("peerGender") Gender peerGender); @Query( - "SELECT new com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AvgConsumptionGoalDto(cg.category.id, AVG(cg.consumeAmount))" + "SELECT new com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AvgConsumptionGoalDto(" + + "cg.category.id, AVG(cg.consumeAmount))" + "FROM ConsumptionGoal cg " + "WHERE cg.category.isDefault = true " + "AND cg.user.age BETWEEN :peerAgeStart AND :peerAgeEnd " + "AND cg.user.gender = :peerGender " + "AND cg.goalMonth >= :currentMonth " + "GROUP BY cg.category.id " + "ORDER BY AVG(cg.consumeAmount) DESC") @@ -54,7 +53,8 @@ List findAvgConsumptionAmountByCategory( List findAllConsumptionAmountByUserId(@Param("userId") Long userId); @Query( - "SELECT new com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AvgConsumptionGoalDto(cg.category.id, AVG(cg.goalAmount))" + "SELECT new com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AvgConsumptionGoalDto(cg.category.id, AVG(" + + "cg.goalAmount))" + "FROM ConsumptionGoal cg " + "WHERE cg.category.isDefault = true " + "AND cg.user.age BETWEEN :peerAgeStart AND :peerAgeEnd " + "AND cg.user.gender = :peerGender " + "AND cg.goalMonth >= :currentMonth " + "GROUP BY cg.category.id " + "ORDER BY AVG(cg.goalAmount) DESC") @@ -69,4 +69,15 @@ List findAvgGoalAmountByCategory( + "WHERE cg.category.isDefault = true " + "AND cg.user.id = :userId " + "GROUP BY cg.category.id " + "ORDER BY cg.category.id") List findAllGoalAmountByUserId(@Param("userId") Long userId); + + @Query( + "SELECT new com.bbteam.budgetbuddies.domain.consumptiongoal.dto.CategoryConsumptionCountDto(" + + "cg.category.id, COUNT(cg)) " + "FROM ConsumptionGoal cg " + "WHERE cg.category.isDefault = true " + + "AND cg.user.age BETWEEN :peerAgeStart AND :peerAgeEnd " + "AND cg.user.gender = :peerGender " + + "AND cg.goalMonth >= :currentMonth " + "GROUP BY cg.category.id " + "ORDER BY COUNT(cg) DESC") + List findTopCategoriesByConsumptionCount( + @Param("peerAgeStart") int peerAgeStart, + @Param("peerAgeEnd") int peerAgeEnd, + @Param("peerGender") Gender peerGender, + @Param("currentMonth") LocalDate currentMonth); } From 2140207ca447e6aa1182105b762d398971092f60 Mon Sep 17 00:00:00 2001 From: MJJ Date: Tue, 13 Aug 2024 13:08:44 +0900 Subject: [PATCH 30/35] =?UTF-8?q?[refactor]=20=EB=98=90=EB=9E=98=EB=93=A4?= =?UTF-8?q?=EC=9D=B4=20=EA=B0=80=EC=9E=A5=20=EB=A7=8E=EC=9D=B4=ED=95=9C=20?= =?UTF-8?q?=EC=86=8C=EB=B9=84=20=EC=B9=B4=ED=85=8C=EA=B3=A0=EB=A6=AC?= =?UTF-8?q?=EC=99=80=20=EC=86=8C=EB=B9=84=20=EA=B1=B4=EC=88=98=20service?= =?UTF-8?q?=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/ConsumptionGoalService.java | 3 +- .../service/ConsumptionGoalServiceImpl.java | 78 +++++++++++-------- 2 files changed, 47 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java index 29ec2672..27553813 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalService.java @@ -10,6 +10,7 @@ import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalListRequestDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseListDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.PeerInfoResponseDto; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopCategoryConsumptionDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDto; import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseUpdateRequestDto; import com.bbteam.budgetbuddies.domain.expense.entity.Expense; @@ -39,7 +40,7 @@ ConsumptionGoalResponseListDto updateConsumptionGoals(Long userId, void decreaseConsumeAmount(Long userId, Long categoryId, Long amount, LocalDate expenseDate); - List getTopConsumptionCategories(Long userId, int peerAgeStart, int peerAgeEnd, + List getTopConsumptionCategories(Long userId, int peerAgeStart, int peerAgeEnd, String peerGender); List getAllConsumptionCategories(Long userId, int peerAgeS, int peerAgeE, diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java index 645e352d..4eb41f37 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceImpl.java @@ -20,6 +20,7 @@ import com.bbteam.budgetbuddies.domain.consumptiongoal.converter.ConsumptionGoalConverter; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AllConsumptionCategoryResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AvgConsumptionGoalDto; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.CategoryConsumptionCountDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionAnalysisResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalListRequestDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalRequestDto; @@ -27,6 +28,7 @@ import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseListDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.MyConsumptionGoalDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.PeerInfoResponseDto; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopCategoryConsumptionDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.entity.ConsumptionGoal; import com.bbteam.budgetbuddies.domain.consumptiongoal.repository.ConsumptionGoalRepository; @@ -80,8 +82,7 @@ public List getTopConsumptionGoalCategories(Long use @Override @Transactional(readOnly = true) public List getAllConsumptionGoalCategories(Long userId, int peerAgeS, - int peerAgeE, - String peerG) { + int peerAgeE, String peerG) { checkPeerInfo(userId, peerAgeS, peerAgeE, peerG); @@ -92,19 +93,19 @@ public List getAllConsumptionGoalCategories(L List defaultCategories = categoryRepository.findAllByIsDefaultTrue(); return defaultCategories.stream() .map(category -> { - MyConsumptionGoalDto myConsumptionAmountDTO = myConsumptionAmountList.stream() + MyConsumptionGoalDto myConsumptionAmountDto = myConsumptionAmountList.stream() .filter(dto -> dto.getCategoryId().equals(category.getId())) .findFirst() .orElse(new MyConsumptionGoalDto(category.getId(), 0L)); - AvgConsumptionGoalDto avgDTO = categoryAvgList.stream() + AvgConsumptionGoalDto avgDto = categoryAvgList.stream() .filter(dto -> dto.getCategoryId().equals(category.getId())) .findFirst() .orElse(new AvgConsumptionGoalDto(category.getId(), 0L)); - Long avgConsumeAmount = avgDTO.getAverageAmount(); - Long myConsumeAmount = myConsumptionAmountDTO.getMyAmount(); - Long consumeAmountDifference; + Long avgConsumeAmount = avgDto.getAverageAmount(); + Long myConsumeAmount = myConsumptionAmountDto.getMyAmount(); + long consumeAmountDifference; if (avgConsumeAmount == 0L) { consumeAmountDifference = -myConsumeAmount; @@ -156,10 +157,21 @@ public ConsumptionAnalysisResponseDto getTopCategoryAndConsumptionAmount(Long us @Override @Transactional(readOnly = true) - public List getTopConsumptionCategories(Long userId, int peerAgeStart, int peerAgeEnd, - String peerGender) { - // TODO - return List.of(); + public List getTopConsumptionCategories(Long userId, int peerAgeS, int peerAgeE, + String peerG) { + + checkPeerInfo(userId, peerAgeS, peerAgeE, peerG); + + List categoryConsumptionCountDto = consumptionGoalRepository + .findTopCategoriesByConsumptionCount(peerAgeStart, peerAgeEnd, peerGender, currentMonth); + + return categoryConsumptionCountDto.stream() + .limit(3) + .map(category -> TopCategoryConsumptionDto.builder() + .categoryName(getCategoryNameById(category.getCategoryId())) + .consumptionCount(category.getConsumptionCount()) + .build()) + .collect(Collectors.toList()); } @Override @@ -177,19 +189,19 @@ public List getAllConsumptionCategories(Long return defaultCategories.stream() .map(category -> { - MyConsumptionGoalDto myConsumptionAmountDTO = myConsumptionAmountList.stream() + MyConsumptionGoalDto myConsumptionAmountDto = myConsumptionAmountList.stream() .filter(dto -> dto.getCategoryId().equals(category.getId())) .findFirst() .orElse(new MyConsumptionGoalDto(category.getId(), 0L)); - AvgConsumptionGoalDto avgDTO = categoryAvgList.stream() + AvgConsumptionGoalDto avgDto = categoryAvgList.stream() .filter(dto -> dto.getCategoryId().equals(category.getId())) .findFirst() .orElse(new AvgConsumptionGoalDto(category.getId(), 0L)); - Long avgConsumeAmount = avgDTO.getAverageAmount(); - Long myConsumeAmount = myConsumptionAmountDTO.getMyAmount(); - Long consumeAmountDifference; + Long avgConsumeAmount = avgDto.getAverageAmount(); + Long myConsumeAmount = myConsumptionAmountDto.getMyAmount(); + long consumeAmountDifference; if (avgConsumeAmount == 0L) { consumeAmountDifference = -myConsumeAmount; @@ -256,17 +268,17 @@ private List getAvgConsumptionAmount() { List defaultCategories = categoryRepository.findAllByIsDefaultTrue(); List categoryAvgList = new ArrayList<>(); - List avgConsumptionGoalDTO = consumptionGoalRepository.findAvgConsumptionAmountByCategory( - peerAgeStart, peerAgeEnd, peerGender, currentMonth); + List avgConsumptionGoalDto = consumptionGoalRepository + .findAvgConsumptionAmountByCategory(peerAgeStart, peerAgeEnd, peerGender, currentMonth); - Map categoryAvgMap = avgConsumptionGoalDTO.stream() + Map categoryAvgMap = avgConsumptionGoalDto.stream() .collect(Collectors.toMap(AvgConsumptionGoalDto::getCategoryId, Function.identity())); for (Category category : defaultCategories) { - AvgConsumptionGoalDto avgDTO = categoryAvgMap.getOrDefault(category.getId(), + AvgConsumptionGoalDto avgDto = categoryAvgMap.getOrDefault(category.getId(), new AvgConsumptionGoalDto(category.getId(), 0.0)); - categoryAvgList.add(avgDTO); + categoryAvgList.add(avgDto); } return categoryAvgList; } @@ -276,17 +288,17 @@ private List getMyConsumptionAmount(Long userId) { List defaultCategories = categoryRepository.findAllByIsDefaultTrue(); List myConsumptionAmountList = new ArrayList<>(); - List myConsumptionGoalDTO = consumptionGoalRepository.findAllConsumptionAmountByUserId( + List myConsumptionGoalDto = consumptionGoalRepository.findAllConsumptionAmountByUserId( userId); - Map myConsumptionMap = myConsumptionGoalDTO.stream() + Map myConsumptionMap = myConsumptionGoalDto.stream() .collect(Collectors.toMap(MyConsumptionGoalDto::getCategoryId, Function.identity())); for (Category category : defaultCategories) { - MyConsumptionGoalDto mylDTO = myConsumptionMap.getOrDefault(category.getId(), + MyConsumptionGoalDto myConsumptionGaolDto = myConsumptionMap.getOrDefault(category.getId(), new MyConsumptionGoalDto(category.getId(), 0L)); - myConsumptionAmountList.add(mylDTO); + myConsumptionAmountList.add(myConsumptionGaolDto); } return myConsumptionAmountList; } @@ -296,17 +308,17 @@ private List getAvgGoalAmount() { List defaultCategories = categoryRepository.findAllByIsDefaultTrue(); List categoryAvgList = new ArrayList<>(); - List avgConsumptionGoalDTO = consumptionGoalRepository.findAvgGoalAmountByCategory( + List avgConsumptionGoalDto = consumptionGoalRepository.findAvgGoalAmountByCategory( peerAgeStart, peerAgeEnd, peerGender, currentMonth); - Map categoryAvgMap = avgConsumptionGoalDTO.stream() + Map categoryAvgMap = avgConsumptionGoalDto.stream() .collect(Collectors.toMap(AvgConsumptionGoalDto::getCategoryId, Function.identity())); for (Category category : defaultCategories) { - AvgConsumptionGoalDto avgDTO = categoryAvgMap.getOrDefault(category.getId(), + AvgConsumptionGoalDto avgDto = categoryAvgMap.getOrDefault(category.getId(), new AvgConsumptionGoalDto(category.getId(), 0.0)); - categoryAvgList.add(avgDTO); + categoryAvgList.add(avgDto); } return categoryAvgList; } @@ -316,17 +328,17 @@ private List getMyGoalAmount(Long userId) { List defaultCategories = categoryRepository.findAllByIsDefaultTrue(); List myConsumptionAmountList = new ArrayList<>(); - List myConsumptionGoalDTO = consumptionGoalRepository.findAllGoalAmountByUserId( + List myConsumptionGoalDto = consumptionGoalRepository.findAllGoalAmountByUserId( userId); - Map myConsumptionMap = myConsumptionGoalDTO.stream() + Map myConsumptionMap = myConsumptionGoalDto.stream() .collect(Collectors.toMap(MyConsumptionGoalDto::getCategoryId, Function.identity())); for (Category category : defaultCategories) { - MyConsumptionGoalDto myDTO = myConsumptionMap.getOrDefault(category.getId(), + MyConsumptionGoalDto myDto = myConsumptionMap.getOrDefault(category.getId(), new MyConsumptionGoalDto(category.getId(), 0L)); - myConsumptionAmountList.add(myDTO); + myConsumptionAmountList.add(myDto); } return myConsumptionAmountList; } From c3184b36d4c444402b5820a7f14adaaa1aefaf62 Mon Sep 17 00:00:00 2001 From: MJJ Date: Tue, 13 Aug 2024 13:41:41 +0900 Subject: [PATCH 31/35] =?UTF-8?q?[refactor]=20=EB=A9=94=EC=9D=B8=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=A7=80=20=EC=86=8C=EB=B9=84=EB=B6=84=EC=84=9D=20ser?= =?UTF-8?q?vice=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/mainpage/service/MainPageServiceImpl.java | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/mainpage/service/MainPageServiceImpl.java b/src/main/java/com/bbteam/budgetbuddies/domain/mainpage/service/MainPageServiceImpl.java index c09ea31f..fd3b0b9c 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/mainpage/service/MainPageServiceImpl.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/mainpage/service/MainPageServiceImpl.java @@ -2,12 +2,11 @@ import java.time.LocalDate; import java.util.List; -import java.util.NoSuchElementException; import org.springframework.stereotype.Service; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionAnalysisResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseListDto; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.service.ConsumptionGoalService; import com.bbteam.budgetbuddies.domain.discountinfo.dto.DiscountResponseDto; import com.bbteam.budgetbuddies.domain.discountinfo.service.DiscountInfoService; @@ -36,17 +35,12 @@ public MainPageResponseDto getMainPage(Long userId) { now.getMonthValue(), 0, 2) .getContent(); - List topGoalCategoryResponseDtoList = consumptionGoalService.getTopGoalCategoriesLimit( - 1, userId, 0, 0, "NONE"); - if (topGoalCategoryResponseDtoList.size() == 0) { - throw new NoSuchElementException("Category xx"); - } - TopGoalCategoryResponseDto topGoalCategoryResponseDTO = topGoalCategoryResponseDtoList.get(0); + ConsumptionAnalysisResponseDto responseDto = consumptionGoalService.getTopCategoryAndConsumptionAmount(userId); ConsumptionGoalResponseListDto userConsumptionGoal = consumptionGoalService.findUserConsumptionGoalList(userId, now); return MainPageConverter.toMainPageResponseDto(discountResponseDtoList, supportResponseDtoList, - topGoalCategoryResponseDTO, userConsumptionGoal); + responseDto, userConsumptionGoal); } } From 641df16072abab91984a5d24d310f52d3c9841e0 Mon Sep 17 00:00:00 2001 From: MJJ Date: Tue, 13 Aug 2024 13:41:51 +0900 Subject: [PATCH 32/35] =?UTF-8?q?[refactor]=20=EB=A9=94=EC=9D=B8=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=A7=80=20=EC=86=8C=EB=B9=84=EB=B6=84=EC=84=9D=20dto?= =?UTF-8?q?=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/mainpage/converter/MainPageConverter.java | 6 +++--- .../domain/mainpage/dto/MainPageResponseDto.java | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/mainpage/converter/MainPageConverter.java b/src/main/java/com/bbteam/budgetbuddies/domain/mainpage/converter/MainPageConverter.java index ffd6e3fd..7bee8e6a 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/mainpage/converter/MainPageConverter.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/mainpage/converter/MainPageConverter.java @@ -2,8 +2,8 @@ import java.util.List; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionAnalysisResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseListDto; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDto; import com.bbteam.budgetbuddies.domain.discountinfo.dto.DiscountResponseDto; import com.bbteam.budgetbuddies.domain.mainpage.dto.MainPageResponseDto; import com.bbteam.budgetbuddies.domain.supportinfo.dto.SupportResponseDto; @@ -12,12 +12,12 @@ public class MainPageConverter { public static MainPageResponseDto toMainPageResponseDto( List discountResponseDtoList, List supportResponseDtoList, - TopGoalCategoryResponseDto topGoalCategoryResponseDto, + ConsumptionAnalysisResponseDto consumptionAnalysisResponseDto, ConsumptionGoalResponseListDto consumptionGoalResponseListDto) { return MainPageResponseDto.builder() .discountResponseDtoList(discountResponseDtoList) .supportResponseDtoList(supportResponseDtoList) - .topGoalCategoryResponseDto(topGoalCategoryResponseDto) + .consumptionAnalysisResponseDto(consumptionAnalysisResponseDto) .consumptionGoalResponseListDto(consumptionGoalResponseListDto) .build(); } diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/mainpage/dto/MainPageResponseDto.java b/src/main/java/com/bbteam/budgetbuddies/domain/mainpage/dto/MainPageResponseDto.java index f9eb0820..ec371c18 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/mainpage/dto/MainPageResponseDto.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/mainpage/dto/MainPageResponseDto.java @@ -2,8 +2,8 @@ import java.util.List; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionAnalysisResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseListDto; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDto; import com.bbteam.budgetbuddies.domain.discountinfo.dto.DiscountResponseDto; import com.bbteam.budgetbuddies.domain.supportinfo.dto.SupportResponseDto; @@ -14,7 +14,7 @@ @Builder public class MainPageResponseDto { - private TopGoalCategoryResponseDto topGoalCategoryResponseDto; + private ConsumptionAnalysisResponseDto consumptionAnalysisResponseDto; private ConsumptionGoalResponseListDto consumptionGoalResponseListDto; private List discountResponseDtoList; private List supportResponseDtoList; From 36c667235d01bf7381a05226fa8a141bc420025d Mon Sep 17 00:00:00 2001 From: MJJ Date: Wed, 14 Aug 2024 01:04:07 +0900 Subject: [PATCH 33/35] =?UTF-8?q?[refactor]=20ConsumptionGoalRepository=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ConsumptionGoalRepositoryTest.java | 382 +++++++++++++----- 1 file changed, 280 insertions(+), 102 deletions(-) diff --git a/src/test/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepositoryTest.java b/src/test/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepositoryTest.java index 360c66ce..1d076703 100644 --- a/src/test/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepositoryTest.java +++ b/src/test/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepositoryTest.java @@ -4,8 +4,8 @@ import java.time.LocalDate; import java.util.List; -import java.util.Optional; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -14,6 +14,9 @@ import com.bbteam.budgetbuddies.domain.category.entity.Category; import com.bbteam.budgetbuddies.domain.category.repository.CategoryRepository; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AvgConsumptionGoalDto; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.CategoryConsumptionCountDto; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.MyConsumptionGoalDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.entity.ConsumptionGoal; import com.bbteam.budgetbuddies.domain.user.entity.User; import com.bbteam.budgetbuddies.domain.user.repository.UserRepository; @@ -30,7 +33,66 @@ class ConsumptionGoalRepositoryTest { @Autowired CategoryRepository categoryRepository; - LocalDate currentMonth = LocalDate.now().withDayOfMonth(1); + private User peerUser1; + private User peerUser2; + private Category defaultCategory1; + private Category defaultCategory2; + + @BeforeEach + void setUp() { + defaultCategory1 = categoryRepository.save( + Category.builder().name("Category 1").user(null).isDefault(true).build()); + + defaultCategory2 = categoryRepository.save( + Category.builder().name("Category 2").user(null).isDefault(true).build()); + + peerUser1 = userRepository.save( + User.builder() + .email("peer1@example.com") + .age(24) + .name("Peer User 1") + .gender(Gender.MALE) + .phoneNumber("010-1111-1111") + .build()); + + peerUser2 = userRepository.save( + User.builder() + .email("peer2@example.com") + .age(25) + .name("Peer User 2") + .gender(Gender.MALE) + .phoneNumber("010-2222-2222") + .build()); + + LocalDate currentMonth = LocalDate.now(); + + consumptionGoalRepository.save( + ConsumptionGoal.builder() + .goalAmount(100L) + .consumeAmount(50L) + .user(peerUser1) + .category(defaultCategory1) + .goalMonth(currentMonth) + .build()); + + consumptionGoalRepository.save( + ConsumptionGoal.builder() + .goalAmount(150L) + .consumeAmount(100L) + .user(peerUser1) + .category(defaultCategory1) + .goalMonth(currentMonth) + .build()); + + consumptionGoalRepository.save( + ConsumptionGoal.builder() + .goalAmount(200L) + .consumeAmount(150L) + .user(peerUser2) + .category(defaultCategory2) + .goalMonth(currentMonth) + .build()); + } @Test @DisplayName("유저 아이디와 goalMonth를 통해 GoalConsumption 조회 성공") @@ -77,95 +139,96 @@ void findConsumptionGoalByUserIdAndGoalMonth_Success() { } @Test - @DisplayName("또래 나이와 성별 정보를 통해 GoalConsumption 조회 성공") - void findTopCategoriesAndGoalAmount_Success() { - //given - User mainUser = userRepository.save( - User.builder() - .email("email") - .age(24) - .name("name") - .gender(Gender.MALE) - .phoneNumber("010-1234-5678") + @DisplayName("또래 나이와 성별 정보를 통해 카테고리와 평균 소비 금액 조회 성공") + void findAvgConsumptionAmountByCategory_Success() { + // given + Category defaultCategory1 = categoryRepository.save( + Category.builder() + .name("Default Category 1") + .user(null) + .isDefault(true) .build()); - Category defaultCategory = categoryRepository.save( - Category.builder().name("디폴트 카테고리").user(null).isDefault(true).build()); + Category defaultCategory2 = categoryRepository.save( + Category.builder() + .name("Default Category 2") + .user(null) + .isDefault(true) + .build()); - LocalDate goalMonth = LocalDate.now(); + LocalDate currentMonth = LocalDate.now(); - ConsumptionGoal defaultCategoryConsumptionGoal = consumptionGoalRepository.save(ConsumptionGoal.builder() - .goalAmount(1L) - .consumeAmount(1L) - .user(mainUser) - .goalMonth(goalMonth) - .category(defaultCategory) - .build()); + consumptionGoalRepository.save( + ConsumptionGoal.builder() + .goalAmount(100L) + .consumeAmount(50L) + .user(peerUser1) + .goalMonth(currentMonth) + .category(defaultCategory1) + .build()); + + consumptionGoalRepository.save( + ConsumptionGoal.builder() + .goalAmount(100L) + .consumeAmount(150L) + .user(peerUser1) + .goalMonth(currentMonth) + .category(defaultCategory1) + .build()); + + consumptionGoalRepository.save( + ConsumptionGoal.builder() + .goalAmount(200L) + .consumeAmount(300L) + .user(peerUser2) + .goalMonth(currentMonth) + .category(defaultCategory2) + .build()); // when - int top = 4; - int peerAgeStart = 23; - int peerAgeEnd = 25; + int peerAgeStart = 20; + int peerAgeEnd = 40; Gender peerGender = Gender.MALE; - List result = consumptionGoalRepository.findTopCategoriesAndGoalAmountLimit( - top, peerAgeStart, peerAgeEnd, peerGender, currentMonth); + List result = consumptionGoalRepository.findAvgConsumptionAmountByCategory( + peerAgeStart, peerAgeEnd, peerGender, currentMonth); // then - ConsumptionGoal resultGoal = result.get(0); - assertThat(resultGoal.getGoalAmount()).isEqualTo(1L); - assertThat(resultGoal.getConsumeAmount()).isEqualTo(1L); - assertThat(resultGoal.getUser().getAge()).isEqualTo(24); - assertThat(resultGoal.getCategory().getName()).isEqualTo("디폴트 카테고리"); - assertThat(resultGoal.getUser().getGender()).isEqualTo(Gender.MALE); + assertThat(result).isNotEmpty(); + + AvgConsumptionGoalDto resultGoal1 = result.stream() + .filter(dto -> dto.getCategoryId().equals(defaultCategory1.getId())) + .findFirst() + .orElseThrow(); + assertThat(resultGoal1.getAverageAmount()).isEqualTo(100L); // (50L + 150L) / 2 = 100L + + AvgConsumptionGoalDto resultGoal2 = result.stream() + .filter(dto -> dto.getCategoryId().equals(defaultCategory2.getId())) + .findFirst() + .orElseThrow(); + assertThat(resultGoal2.getAverageAmount()).isEqualTo(300L); // Only one value so average is the same } @Test - @DisplayName("또래들이 가장 큰 목표로 세운 카테고리와 그 카테고리에서 이번 주의 소비 목표 조회 성공") - void findTopConsumptionByCategoryIdAndCurrentWeek_Success() { - // given - LocalDate startOfWeek = LocalDate.of(2024, 7, 1); // 월요일 - LocalDate endOfWeek = LocalDate.of(2024, 7, 7); // 일요일 - - User mainUser = userRepository.save(User.builder() - .email("email") - .age(24) - .name("name") - .gender(Gender.MALE) - .phoneNumber("010-1234-5678") - .build()); + @DisplayName("또래 나이와 성별 정보를 통해 카테고리와 나의 소비 금액 조회 성공") + void findAllConsumptionAmountByUserId_Success() { + // when + List result = consumptionGoalRepository.findAllConsumptionAmountByUserId( + peerUser1.getId()); - Category category = categoryRepository.save(Category.builder() - .name("Test Category") - .user(mainUser) - .isDefault(false) - .build()); + // then + assertThat(result).isNotEmpty(); - ConsumptionGoal goal1 = consumptionGoalRepository.save(ConsumptionGoal.builder() - .goalAmount(5000L) - .consumeAmount(2000L) - .user(mainUser) - .category(category) - .goalMonth(startOfWeek) - .build()); + Long categoryId1 = defaultCategory1.getId(); - ConsumptionGoal goal2 = consumptionGoalRepository.save(ConsumptionGoal.builder() - .goalAmount(3000L) - .consumeAmount(1500L) - .user(mainUser) - .category(category) - .goalMonth(startOfWeek) - .build()); + MyConsumptionGoalDto firstResult = result.stream() + .filter(dto -> dto.getCategoryId().equals(categoryId1)) + .findFirst() + .orElseThrow(() -> new AssertionError("Category ID " + categoryId1 + " not found")); - // when - Optional result = consumptionGoalRepository.findTopConsumptionByCategoryIdAndCurrentWeek( - category.getId(), startOfWeek, endOfWeek); + assertThat(firstResult.getCategoryId()).isEqualTo(categoryId1); + assertThat(firstResult.getMyAmount()).isEqualTo(150L); - // then - ConsumptionGoal topGoal = result.get(); - assertThat(topGoal.getConsumeAmount()).isEqualTo(2000L); - assertThat(topGoal.getGoalAmount()).isEqualTo(5000L); - assertThat(topGoal.getCategory().getId()).isEqualTo(category.getId()); } private void setUnselectedConsumptionGoal(User mainUser, LocalDate goalMonth, Category defaultCategory) { @@ -192,46 +255,161 @@ private void setUnselectedConsumptionGoal(User mainUser, LocalDate goalMonth, Ca } @Test - @DisplayName("또래 나이와 성별 정보를 통해 GoalConsumption 조회 성공") - void findTopConsumptionAndConsumeAmount_Success() { - //given - User mainUser = userRepository.save( - User.builder() - .email("email") - .age(24) - .name("name") - .gender(Gender.MALE) - .phoneNumber("010-1234-5678") + @DisplayName("또래 나이와 성별 정보를 통해 카테고리와 평균 목표 금액 조회 성공") + void findAvgGoalAmountByCategory_Success() { + // given + Category defaultCategory1 = categoryRepository.save( + Category.builder() + .name("Default Category 1") + .user(null) + .isDefault(true) .build()); - Category defaultCategory = categoryRepository.save( - Category.builder().name("디폴트 카테고리").user(null).isDefault(true).build()); + Category defaultCategory2 = categoryRepository.save( + Category.builder() + .name("Default Category 2") + .user(null) + .isDefault(true) + .build()); - LocalDate goalMonth = LocalDate.now(); + LocalDate currentMonth = LocalDate.now(); - ConsumptionGoal defaultCategoryConsumptionGoal = consumptionGoalRepository.save(ConsumptionGoal.builder() - .goalAmount(1L) - .consumeAmount(1L) - .user(mainUser) - .goalMonth(goalMonth) - .category(defaultCategory) - .build()); + consumptionGoalRepository.save( + ConsumptionGoal.builder() + .goalAmount(100L) + .consumeAmount(50L) + .user(peerUser1) + .goalMonth(currentMonth) + .category(defaultCategory1) + .build()); + + consumptionGoalRepository.save( + ConsumptionGoal.builder() + .goalAmount(150L) + .consumeAmount(100L) + .user(peerUser2) + .goalMonth(currentMonth) + .category(defaultCategory1) + .build()); + + consumptionGoalRepository.save( + ConsumptionGoal.builder() + .goalAmount(200L) + .consumeAmount(150L) + .user(peerUser1) + .goalMonth(currentMonth) + .category(defaultCategory2) + .build()); // when - int top = 4; int peerAgeStart = 23; int peerAgeEnd = 25; Gender peerGender = Gender.MALE; - List result = consumptionGoalRepository.findTopConsumptionAndConsumeAmountLimit( - top, peerAgeStart, peerAgeEnd, peerGender, currentMonth); + List result = consumptionGoalRepository.findAvgGoalAmountByCategory( + peerAgeStart, peerAgeEnd, peerGender, currentMonth); + + // then + assertThat(result).isNotEmpty(); + + AvgConsumptionGoalDto firstResult = result.stream() + .filter(dto -> dto.getCategoryId().equals(defaultCategory1.getId())) + .findFirst() + .orElseThrow(() -> new AssertionError("Category ID " + defaultCategory1.getId() + " not found")); + + AvgConsumptionGoalDto secondResult = result.stream() + .filter(dto -> dto.getCategoryId().equals(defaultCategory2.getId())) + .findFirst() + .orElseThrow(() -> new AssertionError("Category ID " + defaultCategory2.getId() + " not found")); + + assertThat(firstResult.getAverageAmount()).isEqualTo(125L); + assertThat(secondResult.getAverageAmount()).isEqualTo(200L); + } + + @Test + @DisplayName("또래 나이와 성별 정보를 통해 카테고리와 평균 목표 금액 조회 성공") + void findAllGoalAmountByUserId_Success() { + // given + Category defaultCategory1 = categoryRepository.save( + Category.builder() + .name("Default Category 1") + .user(null) + .isDefault(true) + .build()); + + Category defaultCategory2 = categoryRepository.save( + Category.builder() + .name("Default Category 2") + .user(null) + .isDefault(true) + .build()); + + LocalDate currentMonth = LocalDate.now(); + + consumptionGoalRepository.save( + ConsumptionGoal.builder() + .goalAmount(100L) + .consumeAmount(50L) + .user(peerUser1) + .goalMonth(currentMonth) + .category(defaultCategory1) + .build()); + + consumptionGoalRepository.save( + ConsumptionGoal.builder() + .goalAmount(200L) + .consumeAmount(150L) + .user(peerUser1) + .goalMonth(currentMonth) + .category(defaultCategory2) + .build()); + + // when + List result = consumptionGoalRepository.findAllGoalAmountByUserId(peerUser1.getId()); + + // then + assertThat(result).isNotEmpty(); + + MyConsumptionGoalDto firstResult = result.stream() + .filter(dto -> dto.getCategoryId().equals(defaultCategory1.getId())) + .findFirst() + .orElseThrow(() -> new AssertionError("Category ID " + defaultCategory1.getId() + " not found")); + + MyConsumptionGoalDto secondResult = result.stream() + .filter(dto -> dto.getCategoryId().equals(defaultCategory2.getId())) + .findFirst() + .orElseThrow(() -> new AssertionError("Category ID " + defaultCategory2.getId() + " not found")); + + assertThat(firstResult.getMyAmount()).isEqualTo(100L); + assertThat(secondResult.getMyAmount()).isEqualTo(200L); + } + + @Test + @DisplayName("또래 나이와 성별 정보를 통해 카테고리별 소비 횟수 조회 성공") + void findTopCategoriesByConsumptionCount_Success() { + // when + int peerAgeStart = 20; + int peerAgeEnd = 30; + Gender peerGender = Gender.MALE; + LocalDate currentMonth = LocalDate.now(); + + List result = consumptionGoalRepository.findTopCategoriesByConsumptionCount( + peerAgeStart, peerAgeEnd, peerGender, currentMonth); // then - ConsumptionGoal resultGoal = result.get(0); - assertThat(resultGoal.getGoalAmount()).isEqualTo(1L); - assertThat(resultGoal.getConsumeAmount()).isEqualTo(1L); - assertThat(resultGoal.getUser().getAge()).isEqualTo(24); - assertThat(resultGoal.getCategory().getName()).isEqualTo("디폴트 카테고리"); - assertThat(resultGoal.getUser().getGender()).isEqualTo(Gender.MALE); + assertThat(result).isNotEmpty(); + + CategoryConsumptionCountDto firstResult = result.stream() + .filter(dto -> dto.getCategoryId().equals(defaultCategory1.getId())) + .findFirst() + .orElseThrow(() -> new AssertionError("Category ID " + defaultCategory1.getId() + " not found")); + + CategoryConsumptionCountDto secondResult = result.stream() + .filter(dto -> dto.getCategoryId().equals(defaultCategory2.getId())) + .findFirst() + .orElseThrow(() -> new AssertionError("Category ID " + defaultCategory2.getId() + " not found")); + + assertThat(firstResult.getConsumptionCount()).isEqualTo(2); + assertThat(secondResult.getConsumptionCount()).isEqualTo(1); } } \ No newline at end of file From 223d22191c2f5c038419252d5cddb84eee87057f Mon Sep 17 00:00:00 2001 From: MJJ Date: Wed, 14 Aug 2024 01:33:56 +0900 Subject: [PATCH 34/35] =?UTF-8?q?[refactor]=20ConsumptionGoalRepository=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ConsumptionGoalRepositoryTest.java | 152 ++---------------- 1 file changed, 15 insertions(+), 137 deletions(-) diff --git a/src/test/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepositoryTest.java b/src/test/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepositoryTest.java index 1d076703..054b35de 100644 --- a/src/test/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepositoryTest.java +++ b/src/test/java/com/bbteam/budgetbuddies/domain/consumptiongoal/repository/ConsumptionGoalRepositoryTest.java @@ -37,6 +37,7 @@ class ConsumptionGoalRepositoryTest { private User peerUser2; private Category defaultCategory1; private Category defaultCategory2; + private LocalDate currentMonth; @BeforeEach void setUp() { @@ -64,7 +65,7 @@ void setUp() { .phoneNumber("010-2222-2222") .build()); - LocalDate currentMonth = LocalDate.now(); + currentMonth = LocalDate.now(); consumptionGoalRepository.save( ConsumptionGoal.builder() @@ -80,7 +81,7 @@ void setUp() { .goalAmount(150L) .consumeAmount(100L) .user(peerUser1) - .category(defaultCategory1) + .category(defaultCategory2) .goalMonth(currentMonth) .build()); @@ -141,53 +142,9 @@ void findConsumptionGoalByUserIdAndGoalMonth_Success() { @Test @DisplayName("또래 나이와 성별 정보를 통해 카테고리와 평균 소비 금액 조회 성공") void findAvgConsumptionAmountByCategory_Success() { - // given - Category defaultCategory1 = categoryRepository.save( - Category.builder() - .name("Default Category 1") - .user(null) - .isDefault(true) - .build()); - - Category defaultCategory2 = categoryRepository.save( - Category.builder() - .name("Default Category 2") - .user(null) - .isDefault(true) - .build()); - - LocalDate currentMonth = LocalDate.now(); - - consumptionGoalRepository.save( - ConsumptionGoal.builder() - .goalAmount(100L) - .consumeAmount(50L) - .user(peerUser1) - .goalMonth(currentMonth) - .category(defaultCategory1) - .build()); - - consumptionGoalRepository.save( - ConsumptionGoal.builder() - .goalAmount(100L) - .consumeAmount(150L) - .user(peerUser1) - .goalMonth(currentMonth) - .category(defaultCategory1) - .build()); - - consumptionGoalRepository.save( - ConsumptionGoal.builder() - .goalAmount(200L) - .consumeAmount(300L) - .user(peerUser2) - .goalMonth(currentMonth) - .category(defaultCategory2) - .build()); - // when - int peerAgeStart = 20; - int peerAgeEnd = 40; + int peerAgeStart = 23; + int peerAgeEnd = 25; Gender peerGender = Gender.MALE; List result = consumptionGoalRepository.findAvgConsumptionAmountByCategory( @@ -200,13 +157,13 @@ void findAvgConsumptionAmountByCategory_Success() { .filter(dto -> dto.getCategoryId().equals(defaultCategory1.getId())) .findFirst() .orElseThrow(); - assertThat(resultGoal1.getAverageAmount()).isEqualTo(100L); // (50L + 150L) / 2 = 100L + assertThat(resultGoal1.getAverageAmount()).isEqualTo(50L); AvgConsumptionGoalDto resultGoal2 = result.stream() .filter(dto -> dto.getCategoryId().equals(defaultCategory2.getId())) .findFirst() .orElseThrow(); - assertThat(resultGoal2.getAverageAmount()).isEqualTo(300L); // Only one value so average is the same + assertThat(resultGoal2.getAverageAmount()).isEqualTo(125L); } @Test @@ -227,7 +184,7 @@ void findAllConsumptionAmountByUserId_Success() { .orElseThrow(() -> new AssertionError("Category ID " + categoryId1 + " not found")); assertThat(firstResult.getCategoryId()).isEqualTo(categoryId1); - assertThat(firstResult.getMyAmount()).isEqualTo(150L); + assertThat(firstResult.getMyAmount()).isEqualTo(50L); } @@ -257,50 +214,6 @@ private void setUnselectedConsumptionGoal(User mainUser, LocalDate goalMonth, Ca @Test @DisplayName("또래 나이와 성별 정보를 통해 카테고리와 평균 목표 금액 조회 성공") void findAvgGoalAmountByCategory_Success() { - // given - Category defaultCategory1 = categoryRepository.save( - Category.builder() - .name("Default Category 1") - .user(null) - .isDefault(true) - .build()); - - Category defaultCategory2 = categoryRepository.save( - Category.builder() - .name("Default Category 2") - .user(null) - .isDefault(true) - .build()); - - LocalDate currentMonth = LocalDate.now(); - - consumptionGoalRepository.save( - ConsumptionGoal.builder() - .goalAmount(100L) - .consumeAmount(50L) - .user(peerUser1) - .goalMonth(currentMonth) - .category(defaultCategory1) - .build()); - - consumptionGoalRepository.save( - ConsumptionGoal.builder() - .goalAmount(150L) - .consumeAmount(100L) - .user(peerUser2) - .goalMonth(currentMonth) - .category(defaultCategory1) - .build()); - - consumptionGoalRepository.save( - ConsumptionGoal.builder() - .goalAmount(200L) - .consumeAmount(150L) - .user(peerUser1) - .goalMonth(currentMonth) - .category(defaultCategory2) - .build()); - // when int peerAgeStart = 23; int peerAgeEnd = 25; @@ -322,48 +235,13 @@ void findAvgGoalAmountByCategory_Success() { .findFirst() .orElseThrow(() -> new AssertionError("Category ID " + defaultCategory2.getId() + " not found")); - assertThat(firstResult.getAverageAmount()).isEqualTo(125L); - assertThat(secondResult.getAverageAmount()).isEqualTo(200L); + assertThat(firstResult.getAverageAmount()).isEqualTo(100L); + assertThat(secondResult.getAverageAmount()).isEqualTo(175L); } @Test @DisplayName("또래 나이와 성별 정보를 통해 카테고리와 평균 목표 금액 조회 성공") void findAllGoalAmountByUserId_Success() { - // given - Category defaultCategory1 = categoryRepository.save( - Category.builder() - .name("Default Category 1") - .user(null) - .isDefault(true) - .build()); - - Category defaultCategory2 = categoryRepository.save( - Category.builder() - .name("Default Category 2") - .user(null) - .isDefault(true) - .build()); - - LocalDate currentMonth = LocalDate.now(); - - consumptionGoalRepository.save( - ConsumptionGoal.builder() - .goalAmount(100L) - .consumeAmount(50L) - .user(peerUser1) - .goalMonth(currentMonth) - .category(defaultCategory1) - .build()); - - consumptionGoalRepository.save( - ConsumptionGoal.builder() - .goalAmount(200L) - .consumeAmount(150L) - .user(peerUser1) - .goalMonth(currentMonth) - .category(defaultCategory2) - .build()); - // when List result = consumptionGoalRepository.findAllGoalAmountByUserId(peerUser1.getId()); @@ -381,15 +259,15 @@ void findAllGoalAmountByUserId_Success() { .orElseThrow(() -> new AssertionError("Category ID " + defaultCategory2.getId() + " not found")); assertThat(firstResult.getMyAmount()).isEqualTo(100L); - assertThat(secondResult.getMyAmount()).isEqualTo(200L); + assertThat(secondResult.getMyAmount()).isEqualTo(150L); } @Test @DisplayName("또래 나이와 성별 정보를 통해 카테고리별 소비 횟수 조회 성공") void findTopCategoriesByConsumptionCount_Success() { // when - int peerAgeStart = 20; - int peerAgeEnd = 30; + int peerAgeStart = 23; + int peerAgeEnd = 25; Gender peerGender = Gender.MALE; LocalDate currentMonth = LocalDate.now(); @@ -409,7 +287,7 @@ void findTopCategoriesByConsumptionCount_Success() { .findFirst() .orElseThrow(() -> new AssertionError("Category ID " + defaultCategory2.getId() + " not found")); - assertThat(firstResult.getConsumptionCount()).isEqualTo(2); - assertThat(secondResult.getConsumptionCount()).isEqualTo(1); + assertThat(firstResult.getConsumptionCount()).isEqualTo(1); + assertThat(secondResult.getConsumptionCount()).isEqualTo(2); } } \ No newline at end of file From 57959c8599baf42bb316155c2ab28222d3675d0c Mon Sep 17 00:00:00 2001 From: MJJ Date: Wed, 14 Aug 2024 15:22:28 +0900 Subject: [PATCH 35/35] =?UTF-8?q?[refactor]=20ConsumptionGoalService=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/ConsumptionGoalServiceTest.java | 267 +++++++++++------- 1 file changed, 160 insertions(+), 107 deletions(-) diff --git a/src/test/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceTest.java b/src/test/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceTest.java index aa5e839a..79eb00ed 100644 --- a/src/test/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceTest.java +++ b/src/test/java/com/bbteam/budgetbuddies/domain/consumptiongoal/service/ConsumptionGoalServiceTest.java @@ -25,14 +25,17 @@ import com.bbteam.budgetbuddies.domain.category.entity.Category; import com.bbteam.budgetbuddies.domain.category.repository.CategoryRepository; import com.bbteam.budgetbuddies.domain.consumptiongoal.converter.ConsumptionGoalConverter; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionAnalysisResponseDTO; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AllConsumptionCategoryResponseDto; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AvgConsumptionGoalDto; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.CategoryConsumptionCountDto; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionAnalysisResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalListRequestDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalRequestDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseListDto; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.PeerInfoResponseDTO; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopConsumptionResponseDTO; -import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDTO; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.MyConsumptionGoalDto; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.PeerInfoResponseDto; +import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopCategoryConsumptionDto; import com.bbteam.budgetbuddies.domain.consumptiongoal.entity.ConsumptionGoal; import com.bbteam.budgetbuddies.domain.consumptiongoal.repository.ConsumptionGoalRepository; import com.bbteam.budgetbuddies.domain.user.entity.User; @@ -256,7 +259,7 @@ void getPeerInfo_Success() { int peerAgeEnd = 25; String peerGender = "MALE"; - PeerInfoResponseDTO result = consumptionGoalService.getPeerInfo(user.getId(), peerAgeStart, peerAgeEnd, + PeerInfoResponseDto result = consumptionGoalService.getPeerInfo(user.getId(), peerAgeStart, peerAgeEnd, peerGender); // then @@ -287,10 +290,16 @@ void getPeerInfo_UserNotFound() { @DisplayName("getTopCategoryAndConsumptionAmount : 가장 큰 계획 카테고리와 이번 주 소비 금액 조회 성공") void getTopCategoryAndConsumptionAmount_Success() { // given - Category defaultCategory = Mockito.spy(Category.builder().name("디폴트 카테고리").user(null).isDefault(true).build()); - given(defaultCategory.getId()).willReturn(-1L); + Category defaultCategory = Mockito.spy(Category.builder() + .name("디폴트 카테고리") + .user(null) + .isDefault(true) + .build()); + given(defaultCategory.getId()).willReturn(1L); LocalDate goalMonthRandomDay = LocalDate.now(); + LocalDate startOfWeek = goalMonthRandomDay.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); + LocalDate endOfWeek = goalMonthRandomDay.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)); ConsumptionGoal topConsumptionGoal = ConsumptionGoal.builder() .goalAmount(5000L) @@ -308,19 +317,29 @@ void getTopCategoryAndConsumptionAmount_Success() { .goalMonth(goalMonthRandomDay) .build(); - LocalDate startOfWeek = goalMonthRandomDay.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); - LocalDate endOfWeek = goalMonthRandomDay.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)); + List avgConsumptionGoalList = List.of( + new AvgConsumptionGoalDto(defaultCategory.getId(), 5000L) + ); + + int peerAgeStart = 23; + int peerAgeEnd = 25; + Gender peerGender = Gender.MALE; given(userRepository.findById(user.getId())).willReturn(Optional.of(user)); - given(consumptionGoalRepository.findTopCategoriesAndGoalAmountLimit(1, 23, 25, Gender.MALE, - currentMonth)).willReturn( - List.of(topConsumptionGoal)); - given( - consumptionGoalRepository.findTopConsumptionByCategoryIdAndCurrentWeek(defaultCategory.getId(), startOfWeek, - endOfWeek)).willReturn(Optional.of(currentWeekConsumptionGoal)); + given(consumptionGoalRepository.findAvgGoalAmountByCategory( + peerAgeStart, peerAgeEnd, peerGender, currentMonth)) + .willReturn(avgConsumptionGoalList); + + given(consumptionGoalRepository.findAvgConsumptionByCategoryIdAndCurrentWeek( + defaultCategory.getId(), startOfWeek, endOfWeek, + peerAgeStart, peerAgeEnd, peerGender)) + .willReturn(Optional.of(currentWeekConsumptionGoal.getConsumeAmount())); + + given(categoryRepository.findById(defaultCategory.getId())) + .willReturn(Optional.of(defaultCategory)); // when - ConsumptionAnalysisResponseDTO result = consumptionGoalService.getTopCategoryAndConsumptionAmount(user.getId()); + ConsumptionAnalysisResponseDto result = consumptionGoalService.getTopCategoryAndConsumptionAmount(user.getId()); // then assertThat(result.getGoalCategory()).isEqualTo(defaultCategory.getName()); @@ -328,121 +347,155 @@ void getTopCategoryAndConsumptionAmount_Success() { } @Test - @DisplayName("getTopGoalCategories : 또래들이 세운 가장 큰 목표 카테고리 조회 top 4 성공") - void getTopGoalCategories_Success() { + @DisplayName("getTopConsumptionCategories : 또래들이 가장 많이 소비한 카테고리 top3 조회 성공") + void getTopConsumptionCategories_Success() { // given - Category defaultCategory = Mockito.spy(Category.builder().name("디폴트 카테고리").user(null).isDefault(true).build()); - Category defaultCategory2 = Mockito.spy( - Category.builder().name("디폴트 카테고리2").user(null).isDefault(true).build()); - Category defaultCategory3 = Mockito.spy( - Category.builder().name("디폴트 카테고리3").user(null).isDefault(true).build()); - Category defaultCategory4 = Mockito.spy( - Category.builder().name("디폴트 카테고리4").user(null).isDefault(true).build()); - - ConsumptionGoal topConsumptionGoal1 = ConsumptionGoal.builder() - .goalAmount(5000L) - .consumeAmount(3000L) - .user(user) - .category(defaultCategory) - .goalMonth(goalMonthRandomDay) - .build(); + Category defaultCategory1 = Mockito.mock(Category.class); + Category defaultCategory2 = Mockito.mock(Category.class); + Category defaultCategory3 = Mockito.mock(Category.class); - ConsumptionGoal topConsumptionGoal2 = ConsumptionGoal.builder() - .goalAmount(6000L) - .consumeAmount(3000L) - .user(user) - .category(defaultCategory2) - .goalMonth(goalMonthRandomDay) - .build(); + given(defaultCategory1.getId()).willReturn(1L); + given(defaultCategory2.getId()).willReturn(2L); + given(defaultCategory3.getId()).willReturn(3L); - ConsumptionGoal topConsumptionGoal3 = ConsumptionGoal.builder() - .goalAmount(7000L) - .consumeAmount(3000L) - .user(user) - .category(defaultCategory3) - .goalMonth(goalMonthRandomDay) - .build(); + given(defaultCategory1.getName()).willReturn("디폴트 카테고리"); + given(defaultCategory2.getName()).willReturn("디폴트 카테고리2"); + given(defaultCategory3.getName()).willReturn("디폴트 카테고리3"); - ConsumptionGoal topConsumptionGoal4 = ConsumptionGoal.builder() - .goalAmount(8000L) - .consumeAmount(3000L) - .user(user) - .category(defaultCategory4) - .goalMonth(goalMonthRandomDay) - .build(); + CategoryConsumptionCountDto topConsumption1 = new CategoryConsumptionCountDto(defaultCategory1.getId(), 30L); + CategoryConsumptionCountDto topConsumption2 = new CategoryConsumptionCountDto(defaultCategory2.getId(), 25L); + CategoryConsumptionCountDto topConsumption3 = new CategoryConsumptionCountDto(defaultCategory3.getId(), 20L); + + int peerAgeStart = 23; + int peerAgeEnd = 25; + String peerGender = "MALE"; given(userRepository.findById(user.getId())).willReturn(Optional.of(user)); - given(consumptionGoalRepository.findTopCategoriesAndGoalAmountLimit(4, 23, 25, Gender.MALE, - currentMonth)).willReturn( - List.of(topConsumptionGoal1, topConsumptionGoal2, topConsumptionGoal3, topConsumptionGoal4)); + given(consumptionGoalRepository.findTopCategoriesByConsumptionCount(peerAgeStart, peerAgeEnd, + Gender.valueOf(peerGender), currentMonth)) + .willReturn(List.of(topConsumption1, topConsumption2, topConsumption3)); + + given(categoryRepository.findById(defaultCategory1.getId())).willReturn(Optional.of(defaultCategory1)); + given(categoryRepository.findById(defaultCategory2.getId())).willReturn(Optional.of(defaultCategory2)); + given(categoryRepository.findById(defaultCategory3.getId())).willReturn(Optional.of(defaultCategory3)); // when - List result = consumptionGoalService.getTopGoalCategoriesLimit(4, user.getId(), 0, - 0, - "none"); + List result = consumptionGoalService.getTopConsumptionCategories( + user.getId(), peerAgeStart, peerAgeEnd, peerGender); // then - assertThat(result).hasSize(4); - assertThat(result.get(0).getCategoryName()).isEqualTo(defaultCategory.getName()); - assertThat(result.get(0).getGoalAmount()).isEqualTo(topConsumptionGoal1.getGoalAmount()); + assertThat(result).hasSize(3); + + assertThat(result.get(0).getCategoryName()).isEqualTo(defaultCategory1.getName()); + assertThat(result.get(0).getConsumptionCount()).isEqualTo(topConsumption1.getConsumptionCount()); + assertThat(result.get(1).getCategoryName()).isEqualTo(defaultCategory2.getName()); - assertThat(result.get(1).getGoalAmount()).isEqualTo(topConsumptionGoal2.getGoalAmount()); + assertThat(result.get(1).getConsumptionCount()).isEqualTo(topConsumption2.getConsumptionCount()); + assertThat(result.get(2).getCategoryName()).isEqualTo(defaultCategory3.getName()); - assertThat(result.get(2).getGoalAmount()).isEqualTo(topConsumptionGoal3.getGoalAmount()); - assertThat(result.get(3).getCategoryName()).isEqualTo(defaultCategory4.getName()); - assertThat(result.get(3).getGoalAmount()).isEqualTo(topConsumptionGoal4.getGoalAmount()); + assertThat(result.get(2).getConsumptionCount()).isEqualTo(topConsumption3.getConsumptionCount()); } @Test - @DisplayName("getTopConsumption : 또래들이 가장 많이 소비한 카테고리 top3 조회 성공") - void getTopConsumptionCategories_Success() { + @DisplayName("getAllConsumptionGoalCategories : 또래들이 세운 가장 큰 목표 카테고리 전체조회 성공") + void getAllConsumptionGoalCategories_Success() { // given - Category defaultCategory = Mockito.spy(Category.builder().name("디폴트 카테고리").user(null).isDefault(true).build()); - Category defaultCategory2 = Mockito.spy( - Category.builder().name("디폴트 카테고리2").user(null).isDefault(true).build()); - Category defaultCategory3 = Mockito.spy( - Category.builder().name("디폴트 카테고리3").user(null).isDefault(true).build()); + Category defaultCategory1 = new Category(); + Category defaultCategory2 = new Category(); + + defaultCategory1.setId(1L); + defaultCategory1.setName("디폴트 카테고리1"); + defaultCategory1.setIsDefault(true); + + defaultCategory2.setId(2L); + defaultCategory2.setName("디폴트 카테고리2"); + defaultCategory2.setIsDefault(true); + + List categoryAvgList = List.of( + new AvgConsumptionGoalDto(1L, 3000L), + new AvgConsumptionGoalDto(2L, 4000L) + ); + + List myConsumptionAmountList = List.of( + new MyConsumptionGoalDto(1L, 5000L), + new MyConsumptionGoalDto(2L, 2000L) + ); + + List defaultCategories = List.of(defaultCategory1, defaultCategory2); + + given(categoryRepository.findAllByIsDefaultTrue()).willReturn(defaultCategories); + given(consumptionGoalRepository.findAvgGoalAmountByCategory( + anyInt(), anyInt(), any(), any())).willReturn(categoryAvgList); + given(consumptionGoalRepository.findAllGoalAmountByUserId(user.getId())) + .willReturn(myConsumptionAmountList); + given(userRepository.findById(user.getId())).willReturn(Optional.of(user)); - ConsumptionGoal topConsumptionGoal1 = ConsumptionGoal.builder() - .goalAmount(5000L) - .consumeAmount(3000L) - .user(user) - .category(defaultCategory) - .goalMonth(goalMonthRandomDay) - .build(); + // when + List result = consumptionGoalService.getAllConsumptionGoalCategories( + user.getId(), 23, 25, "MALE"); - ConsumptionGoal topConsumptionGoal2 = ConsumptionGoal.builder() - .goalAmount(6000L) - .consumeAmount(3000L) - .user(user) - .category(defaultCategory2) - .goalMonth(goalMonthRandomDay) - .build(); + // then + assertThat(result).isNotNull(); + assertThat(result).hasSize(2); - ConsumptionGoal topConsumptionGoal3 = ConsumptionGoal.builder() - .goalAmount(7000L) - .consumeAmount(3000L) - .user(user) - .category(defaultCategory3) - .goalMonth(goalMonthRandomDay) - .build(); + AllConsumptionCategoryResponseDto firstCategory = result.get(0); + assertThat(firstCategory.getCategoryName()).isEqualTo("디폴트 카테고리1"); + assertThat(firstCategory.getAvgAmount()).isEqualTo(3000L); + assertThat(firstCategory.getAmountDifference()).isEqualTo(2000L); + + AllConsumptionCategoryResponseDto secondCategory = result.get(1); + assertThat(secondCategory.getCategoryName()).isEqualTo("디폴트 카테고리2"); + assertThat(secondCategory.getAvgAmount()).isEqualTo(4000L); + assertThat(secondCategory.getAmountDifference()).isEqualTo(-2000L); + } + @Test + @DisplayName("getAllConsumptionCategories : 또래들이 가장 많이 소비한 카테고리 전체조회 성공") + void getAllConsumptionCategories_Success() { + // given + Category defaultCategory1 = new Category(); + defaultCategory1.setId(1L); + defaultCategory1.setName("디폴트 카테고리1"); + defaultCategory1.setIsDefault(true); + + Category defaultCategory2 = new Category(); + defaultCategory2.setId(2L); + defaultCategory2.setName("디폴트 카테고리2"); + defaultCategory2.setIsDefault(true); + + List categoryAvgList = List.of( + new AvgConsumptionGoalDto(1L, 3000L), + new AvgConsumptionGoalDto(2L, 4000L) + ); + + List myConsumptionAmountList = List.of( + new MyConsumptionGoalDto(1L, 5000L), + new MyConsumptionGoalDto(2L, 2000L) + ); + + List defaultCategories = List.of(defaultCategory1, defaultCategory2); + + given(categoryRepository.findAllByIsDefaultTrue()).willReturn(defaultCategories); + given(consumptionGoalRepository.findAvgConsumptionAmountByCategory( + anyInt(), anyInt(), any(), any())).willReturn(categoryAvgList); given(userRepository.findById(user.getId())).willReturn(Optional.of(user)); - given(consumptionGoalRepository.findTopConsumptionAndConsumeAmountLimit(3, 23, 25, Gender.MALE, - currentMonth)).willReturn( - List.of(topConsumptionGoal1, topConsumptionGoal2, topConsumptionGoal3)); // when - List result = consumptionGoalService.getTopConsumptionsLimit(3, user.getId(), 23, 25, - "MALE"); + List result = consumptionGoalService.getAllConsumptionCategories( + user.getId(), 23, 25, "MALE"); // then - assertThat(result).hasSize(3); - assertThat(result.get(0).getCategoryName()).isEqualTo(defaultCategory.getName()); - assertThat(result.get(0).getConsumeAmount()).isEqualTo(topConsumptionGoal1.getConsumeAmount()); - assertThat(result.get(1).getCategoryName()).isEqualTo(defaultCategory2.getName()); - assertThat(result.get(1).getConsumeAmount()).isEqualTo(topConsumptionGoal2.getConsumeAmount()); - assertThat(result.get(2).getCategoryName()).isEqualTo(defaultCategory3.getName()); - assertThat(result.get(2).getConsumeAmount()).isEqualTo(topConsumptionGoal3.getConsumeAmount()); + assertThat(result).isNotNull(); + assertThat(result).hasSize(2); + + AllConsumptionCategoryResponseDto firstCategory = result.get(0); + assertThat(firstCategory.getCategoryName()).isEqualTo("디폴트 카테고리1"); + assertThat(firstCategory.getAvgAmount()).isEqualTo(3000L); + assertThat(firstCategory.getAmountDifference()).isEqualTo(-3000); + + AllConsumptionCategoryResponseDto secondCategory = result.get(1); + assertThat(secondCategory.getCategoryName()).isEqualTo("디폴트 카테고리2"); + assertThat(secondCategory.getAvgAmount()).isEqualTo(4000L); + assertThat(secondCategory.getAmountDifference()).isEqualTo(-4000L); } } \ No newline at end of file