diff --git a/src/main/java/com/example/demo/app/domain/service/GoalService.java b/src/main/java/com/example/demo/app/domain/service/GoalService.java index 294f7ac..500c990 100644 --- a/src/main/java/com/example/demo/app/domain/service/GoalService.java +++ b/src/main/java/com/example/demo/app/domain/service/GoalService.java @@ -1,16 +1,18 @@ package com.example.demo.app.domain.service; +import java.util.List; + import com.example.demo.app.domain.model.GoalModel; public interface GoalService { GoalModel getGoalById(Long goalId); - GoalModel getGoalByUserId(Long userId); + public List getGoalByUserId(Long userId); - GoalModel createGoal(GoalModel goalModel); + public GoalModel createGoal(GoalModel goalModel, Long userId); - GoalModel updateGoal(GoalModel goalModel); + GoalModel updateGoal(GoalModel goalModel, Long userId); // void deleteGoal(Long goalId); diff --git a/src/main/java/com/example/demo/app/domain/service/impl/GoalServiceImpl.java b/src/main/java/com/example/demo/app/domain/service/impl/GoalServiceImpl.java index a1d31e8..0d4d75a 100644 --- a/src/main/java/com/example/demo/app/domain/service/impl/GoalServiceImpl.java +++ b/src/main/java/com/example/demo/app/domain/service/impl/GoalServiceImpl.java @@ -1,5 +1,8 @@ package com.example.demo.app.domain.service.impl; +import java.util.ArrayList; +import java.util.List; + import org.springframework.stereotype.Service; import com.example.demo.app.domain.model.GoalModel; @@ -21,14 +24,14 @@ public class GoalServiceImpl implements GoalService { @Override public GoalModel getGoalById(Long goalId) { GoalEntity goalEntity = goalRepository.findById(goalId).orElse(null); - return GoalModel.convertFrom(goalEntity); + return GoalModel.convertFrom(goalEntity, 0, 0); } @Override - public GoalModel getGoalByUserId(Long userId) { + public List getGoalByUserId(Long userId) { UserEntity userEntity = userRepository.findById(userId).orElseThrow(); - GoalEntity goalEntity = userEntity.getGoalEntity(); + List goalEntityList = goalRepository.findAllByUserId(userId); // 収支の合計を計算(収入はプラス、支出はマイナス) int transactionTotal = userEntity.getTransactionEntityList().stream() @@ -42,42 +45,38 @@ public class GoalServiceImpl implements GoalService { int currentAmount = userEntity.getSaving() + transactionTotal; // 達成率(%) = 現在の資産 ÷ 目標金額 × 100 - int achievementRate = - (goalEntity.getTargetAmount() == null) - ? 0 : - (int) ((currentAmount * 100.0) / goalEntity.getTargetAmount()); - GoalModel goalModel = GoalModel.convertFrom(goalEntity); + List goalModelList = new ArrayList(); - return new GoalModel( - goalModel.getGoalId(), - goalModel.getGoalTitle(), - goalModel.getGoalSubtitle(), - goalModel.getTargetAmount(), - goalModel.getDeadline(), - goalModel.getGoalComplete(), - achievementRate, - currentAmount); + goalEntityList.stream() + .forEach(goalEntity -> goalModelList.add(GoalModel.convertFrom(goalEntity, + (goalEntity.getTargetAmount() == null) + ? 0 + : (int) ((currentAmount * 100.0) / goalEntity.getTargetAmount()), + currentAmount))); + + + return goalModelList; } - - @Override - public GoalModel createGoal(GoalModel goalModel) { - GoalEntity goalEntity = GoalModel.convertTo(goalModel); + @Override + public GoalModel createGoal(GoalModel goalModel, Long userId) { + + GoalEntity goalEntity = GoalModel.convertTo(goalModel, userRepository.findById(userId).get()); GoalEntity savedGoalEntity = goalRepository.save(goalEntity); - return GoalModel.convertFrom(savedGoalEntity); + return GoalModel.convertFrom(savedGoalEntity, 0, 0); } @Override - public GoalModel updateGoal(GoalModel goalModel) { + public GoalModel updateGoal(GoalModel goalModel, Long userId) { - GoalEntity goalEntity = GoalModel.convertTo(goalModel); + GoalEntity goalEntity = GoalModel.convertTo(goalModel, userRepository.findById(userId).get()); GoalEntity updatedGoalEntity = goalRepository.save(goalEntity); - return GoalModel.convertFrom(updatedGoalEntity); + return GoalModel.convertFrom(updatedGoalEntity, 0, 0); } } \ No newline at end of file diff --git a/src/main/java/com/example/demo/app/infra/repository/GoalRepository.java b/src/main/java/com/example/demo/app/infra/repository/GoalRepository.java index c66aa15..1de888e 100644 --- a/src/main/java/com/example/demo/app/infra/repository/GoalRepository.java +++ b/src/main/java/com/example/demo/app/infra/repository/GoalRepository.java @@ -1,6 +1,9 @@ package com.example.demo.app.infra.repository; +import java.util.List; + import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; import com.example.demo.app.infra.entity.GoalEntity; @@ -8,4 +11,7 @@ import com.example.demo.app.infra.entity.GoalEntity; @Repository public interface GoalRepository extends JpaRepository { + @Query("select g from GoalEntity g where g.userEntity.userId = :userId") + public List findAllByUserId(Long userId); + } \ No newline at end of file diff --git a/src/main/java/com/example/demo/app/presentation/controller/GoalController.java b/src/main/java/com/example/demo/app/presentation/controller/GoalController.java index 856d77b..3657719 100644 --- a/src/main/java/com/example/demo/app/presentation/controller/GoalController.java +++ b/src/main/java/com/example/demo/app/presentation/controller/GoalController.java @@ -69,7 +69,7 @@ public class GoalController { } GoalModel goalModel = GoalModel.convertFrom(goalForm); - goalService.updateGoal(goalModel); + goalService.updateGoal(goalModel, userId); return "redirect:/goal"; }