[EDIT] 変更したテーブルに合わせてメソッドを修正しました。

This commit is contained in:
s-choi
2026-09-24 13:20:14 +09:00
parent 70ad3678a5
commit e9d3e16157
4 changed files with 36 additions and 29 deletions

View File

@@ -1,16 +1,18 @@
package com.example.demo.app.domain.service; package com.example.demo.app.domain.service;
import java.util.List;
import com.example.demo.app.domain.model.GoalModel; import com.example.demo.app.domain.model.GoalModel;
public interface GoalService { public interface GoalService {
GoalModel getGoalById(Long goalId); GoalModel getGoalById(Long goalId);
GoalModel getGoalByUserId(Long userId); public List<GoalModel> 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); // void deleteGoal(Long goalId);

View File

@@ -1,5 +1,8 @@
package com.example.demo.app.domain.service.impl; package com.example.demo.app.domain.service.impl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.example.demo.app.domain.model.GoalModel; import com.example.demo.app.domain.model.GoalModel;
@@ -21,14 +24,14 @@ public class GoalServiceImpl implements GoalService {
@Override @Override
public GoalModel getGoalById(Long goalId) { public GoalModel getGoalById(Long goalId) {
GoalEntity goalEntity = goalRepository.findById(goalId).orElse(null); GoalEntity goalEntity = goalRepository.findById(goalId).orElse(null);
return GoalModel.convertFrom(goalEntity); return GoalModel.convertFrom(goalEntity, 0, 0);
} }
@Override @Override
public GoalModel getGoalByUserId(Long userId) { public List<GoalModel> getGoalByUserId(Long userId) {
UserEntity userEntity = userRepository.findById(userId).orElseThrow(); UserEntity userEntity = userRepository.findById(userId).orElseThrow();
GoalEntity goalEntity = userEntity.getGoalEntity(); List<GoalEntity> goalEntityList = goalRepository.findAllByUserId(userId);
// 収支の合計を計算(収入はプラス、支出はマイナス) // 収支の合計を計算(収入はプラス、支出はマイナス)
int transactionTotal = userEntity.getTransactionEntityList().stream() int transactionTotal = userEntity.getTransactionEntityList().stream()
@@ -42,42 +45,38 @@ public class GoalServiceImpl implements GoalService {
int currentAmount = userEntity.getSaving() + transactionTotal; int currentAmount = userEntity.getSaving() + transactionTotal;
// 達成率(%) = 現在の資産 ÷ 目標金額 × 100 // 達成率(%) = 現在の資産 ÷ 目標金額 × 100
int achievementRate =
(goalEntity.getTargetAmount() == null)
? 0 :
(int) ((currentAmount * 100.0) / goalEntity.getTargetAmount());
GoalModel goalModel = GoalModel.convertFrom(goalEntity); List<GoalModel> goalModelList = new ArrayList<GoalModel>();
return new GoalModel( goalEntityList.stream()
goalModel.getGoalId(), .forEach(goalEntity -> goalModelList.add(GoalModel.convertFrom(goalEntity,
goalModel.getGoalTitle(), (goalEntity.getTargetAmount() == null)
goalModel.getGoalSubtitle(), ? 0
goalModel.getTargetAmount(), : (int) ((currentAmount * 100.0) / goalEntity.getTargetAmount()),
goalModel.getDeadline(), currentAmount)));
goalModel.getGoalComplete(),
achievementRate,
currentAmount); return goalModelList;
} }
@Override @Override
public GoalModel createGoal(GoalModel goalModel) { public GoalModel createGoal(GoalModel goalModel, Long userId) {
GoalEntity goalEntity = GoalModel.convertTo(goalModel); GoalEntity goalEntity = GoalModel.convertTo(goalModel, userRepository.findById(userId).get());
GoalEntity savedGoalEntity = goalRepository.save(goalEntity); GoalEntity savedGoalEntity = goalRepository.save(goalEntity);
return GoalModel.convertFrom(savedGoalEntity); return GoalModel.convertFrom(savedGoalEntity, 0, 0);
} }
@Override @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); GoalEntity updatedGoalEntity = goalRepository.save(goalEntity);
return GoalModel.convertFrom(updatedGoalEntity); return GoalModel.convertFrom(updatedGoalEntity, 0, 0);
} }
} }

View File

@@ -1,6 +1,9 @@
package com.example.demo.app.infra.repository; package com.example.demo.app.infra.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import com.example.demo.app.infra.entity.GoalEntity; import com.example.demo.app.infra.entity.GoalEntity;
@@ -8,4 +11,7 @@ import com.example.demo.app.infra.entity.GoalEntity;
@Repository @Repository
public interface GoalRepository extends JpaRepository<GoalEntity, Long> { public interface GoalRepository extends JpaRepository<GoalEntity, Long> {
@Query("select g from GoalEntity g where g.userEntity.userId = :userId")
public List<GoalEntity> findAllByUserId(Long userId);
} }

View File

@@ -69,7 +69,7 @@ public class GoalController {
} }
GoalModel goalModel = GoalModel.convertFrom(goalForm); GoalModel goalModel = GoalModel.convertFrom(goalForm);
goalService.updateGoal(goalModel); goalService.updateGoal(goalModel, userId);
return "redirect:/goal"; return "redirect:/goal";
} }