From 4726b61517148972a59b35cca9bcda9141f8a7ef Mon Sep 17 00:00:00 2001 From: Seo Jemin <82302520+jemlog@users.noreply.github.com> Date: Fri, 1 Sep 2023 15:23:05 +0900 Subject: [PATCH] =?UTF-8?q?=ED=9A=8C=EA=B3=A0=20=EC=9E=91=EC=84=B1=20?= =?UTF-8?q?=EC=8B=9C=20=EB=B3=B4=EA=B4=80=ED=95=A8=20=EB=82=B4=20=EB=AA=A9?= =?UTF-8?q?=ED=91=9C=20=EB=A6=AC=EB=A7=88=EC=9D=B8=EB=93=9C=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EC=B6=94=EA=B0=80=20(#58)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 보관함 목표 리마인드 기능 개발 --- .../backend/goal/application/GoalService.java | 30 ++++++++++++++++++- .../domain/repository/GoalRepository.java | 2 ++ .../goal/presentation/GoalController.java | 13 +++++++- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/backend/goal/application/GoalService.java b/src/main/java/com/backend/goal/application/GoalService.java index d03d203..00d0cff 100644 --- a/src/main/java/com/backend/goal/application/GoalService.java +++ b/src/main/java/com/backend/goal/application/GoalService.java @@ -7,6 +7,7 @@ import com.backend.goal.domain.*; import com.backend.goal.application.dto.response.GoalResponse; import com.backend.goal.domain.enums.GoalStatus; +import com.backend.goal.domain.enums.RewardType; import com.backend.goal.domain.event.RemoveRelatedDetailGoalEvent; import com.backend.goal.domain.repository.GoalListResponseDto; import com.backend.goal.domain.repository.GoalQueryRepository; @@ -22,9 +23,11 @@ import org.springframework.transaction.annotation.Transactional; import java.time.LocalDate; +import java.util.ArrayList; import java.util.List; import java.util.Map; - +import java.util.Random; +import java.util.stream.Collectors; @Service @@ -32,6 +35,8 @@ @Transactional(readOnly = true) public class GoalService { + private static final int RANDOM_GOAL_COUNT = 3; + private final GoalRepository goalRepository; private final GoalQueryRepository goalQueryRepository; @@ -92,4 +97,27 @@ public void recoverGoal(Long goalId, GoalRecoverRequest goalRecoverRequest) Goal goal = goalRepository.getByIdAndIsDeletedFalse(goalId); goal.recover(goalRecoverRequest.startDate(), goalRecoverRequest.endDate(), goalRecoverRequest.reminderEnabled()); } + + public List getStoredGoalList() { + + List storedGoalList = goalRepository.getGoalsByGoalStatusAndIsDeletedFalse(GoalStatus.STORE); + + Random random = new Random(); + + List randomStoredGoalList = new ArrayList<>(); + + int goalCount = Math.min(storedGoalList.size(), RANDOM_GOAL_COUNT); + + while (randomStoredGoalList.size() < goalCount) { + + int index = random.nextInt(storedGoalList.size()); + Goal goal = storedGoalList.get(index); + + if (!randomStoredGoalList.contains(goal)) { + randomStoredGoalList.add(goal); + } + } + + return randomStoredGoalList.stream().map(GoalListResponseDto::from).collect(Collectors.toList()); + } } diff --git a/src/main/java/com/backend/goal/domain/repository/GoalRepository.java b/src/main/java/com/backend/goal/domain/repository/GoalRepository.java index 26a59ce..f046e3d 100644 --- a/src/main/java/com/backend/goal/domain/repository/GoalRepository.java +++ b/src/main/java/com/backend/goal/domain/repository/GoalRepository.java @@ -20,4 +20,6 @@ default Goal getByIdAndIsDeletedFalse(Long id){ int countByGoalStatusAndIsDeletedFalse(GoalStatus goalStatus); List getGoalsByGoalStatusAndIsDeletedFalse(GoalStatus goalStatus); + + } diff --git a/src/main/java/com/backend/goal/presentation/GoalController.java b/src/main/java/com/backend/goal/presentation/GoalController.java index 94875ec..67594ad 100644 --- a/src/main/java/com/backend/goal/presentation/GoalController.java +++ b/src/main/java/com/backend/goal/presentation/GoalController.java @@ -6,6 +6,7 @@ import com.backend.goal.application.dto.response.GoalListResponse; import com.backend.goal.application.dto.response.GoalResponse; import com.backend.goal.application.dto.response.RetrospectEnabledGoalCountResponse; +import com.backend.goal.domain.repository.GoalListResponseDto; import com.backend.goal.presentation.dto.GoalRecoverRequest; import com.backend.goal.presentation.dto.GoalSaveRequest; import com.backend.goal.presentation.dto.GoalUpdateRequest; @@ -20,6 +21,8 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import java.util.List; + import static com.backend.global.common.code.SuccessCode.*; @RestController @@ -41,7 +44,7 @@ public ResponseEntity> getGoalList( return CustomResponse.success(SELECT_SUCCESS,goalService.getGoalList(lastId,pageable,goalStatus)); } - + @Operation(summary = "상위 목표 상태별 개수 조회", description = "상위 목표 상태별 개수를 조회하는 API 입니다.") @ApiResponse(responseCode = "200", description = "code : 200, message : SELECT_SUCCESS") @GetMapping("/count") @@ -58,6 +61,14 @@ public ResponseEntity> getRet return CustomResponse.success(SELECT_SUCCESS,goalService.getGoalCountRetrospectEnabled()); } + @Operation(summary = "회고 완료 후 보관함 내 목표 추천", description = "보관함에 들어있는 목표들 중 랜덤하게 3개를 추천해줍니다") + @ApiResponse(responseCode = "200", description = "code : 200, message : SELECT_SUCCESS") + @GetMapping("/stored-goals") + public ResponseEntity>> getStoredGoalList() + { + return CustomResponse.success(SELECT_SUCCESS,goalService.getStoredGoalList()); + } + @Operation(summary = "상위 목표 삭제", description = "상위 목표를 삭제하는 API 입니다.") @ApiResponse(responseCode = "200", description = "code : 200, message : DELETE_SUCCESS")