4 Commits

10 changed files with 91 additions and 72 deletions

View File

@@ -3,6 +3,7 @@ package com.example.demo.app.domain.model;
import java.time.LocalDate; import java.time.LocalDate;
import com.example.demo.app.infra.entity.GoalEntity; import com.example.demo.app.infra.entity.GoalEntity;
import com.example.demo.app.infra.entity.UserEntity;
import com.example.demo.app.presentation.form.GoalForm; import com.example.demo.app.presentation.form.GoalForm;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
@@ -27,8 +28,10 @@ public class GoalModel {
private Integer achievementRate; // 達成率 private Integer achievementRate; // 達成率
private Integer currentAmount; // 現在の資産 private Integer currentAmount; // 現在の資産
private Long userId;
public static GoalModel convertFrom(GoalEntity goalEntity) { public static GoalModel convertFrom(GoalEntity goalEntity, int achievementRate, int currentAmount) {
return new GoalModel( return new GoalModel(
goalEntity.getGoalId(), goalEntity.getGoalId(),
goalEntity.getGoalTitle(), goalEntity.getGoalTitle(),
@@ -36,10 +39,12 @@ public class GoalModel {
goalEntity.getTargetAmount(), goalEntity.getTargetAmount(),
goalEntity.getDeadline(), goalEntity.getDeadline(),
goalEntity.getGoalComplete(), goalEntity.getGoalComplete(),
null,null); achievementRate,
currentAmount,
goalEntity.getUserEntity().getUserId());
} }
public static GoalEntity convertTo(GoalModel goalModel) { public static GoalEntity convertTo(GoalModel goalModel, UserEntity userEntity) {
return new GoalEntity( return new GoalEntity(
goalModel.getGoalId(), goalModel.getGoalId(),
goalModel.getGoalTitle(), goalModel.getGoalTitle(),
@@ -47,7 +52,7 @@ public class GoalModel {
goalModel.getTargetAmount(), goalModel.getTargetAmount(),
goalModel.getDeadline(), goalModel.getDeadline(),
goalModel.getGoalComplete(), goalModel.getGoalComplete(),
null); userEntity, 0);
} }
public static GoalModel convertFrom(GoalForm goalForm) { public static GoalModel convertFrom(GoalForm goalForm) {
@@ -58,7 +63,8 @@ public class GoalModel {
goalForm.getTargetAmount(), goalForm.getTargetAmount(),
goalForm.getDeadline(), goalForm.getDeadline(),
goalForm.getGoalComplete() goalForm.getGoalComplete()
,null,null); ,null,null,
goalForm.getUserId());
} }
} }

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
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); 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

@@ -4,10 +4,12 @@ import java.time.LocalDate;
import jakarta.persistence.Column; import jakarta.persistence.Column;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue; import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType; import jakarta.persistence.GenerationType;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import jakarta.persistence.OneToOne; import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table; import jakarta.persistence.Table;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
@@ -41,7 +43,10 @@ public class GoalEntity {
@Column(name = "goal_complete", nullable = false) @Column(name = "goal_complete", nullable = false)
private Integer goalComplete = 0; private Integer goalComplete = 0;
@OneToOne(mappedBy = "goalEntity") @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private UserEntity userEntity; private UserEntity userEntity;
private Integer deleteFlag;
} }

View File

@@ -12,7 +12,6 @@ import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn; import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne; import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany; import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table; import jakarta.persistence.Table;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
@@ -65,9 +64,8 @@ public class UserEntity {
@JoinColumn(name = "secret_question_id") @JoinColumn(name = "secret_question_id")
private QuestionEntity questionEntity; private QuestionEntity questionEntity;
@OneToOne(fetch = FetchType.LAZY) @OneToMany(mappedBy = "userEntity")
@JoinColumn(name = "goal_id") private List<GoalEntity> goalEntityList;
private GoalEntity goalEntity;
@ManyToOne(fetch = FetchType.LAZY) @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "course_id") @JoinColumn(name = "course_id")

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";
} }

View File

@@ -27,7 +27,6 @@ import com.example.demo.app.domain.service.UserService;
import com.example.demo.app.presentation.form.CategoryForm; import com.example.demo.app.presentation.form.CategoryForm;
import com.example.demo.app.presentation.form.Expense; import com.example.demo.app.presentation.form.Expense;
import com.example.demo.app.presentation.form.FixedCostForm; import com.example.demo.app.presentation.form.FixedCostForm;
import com.example.demo.app.presentation.form.GoalForm;
import com.example.demo.app.presentation.form.Income; import com.example.demo.app.presentation.form.Income;
import com.example.demo.app.presentation.form.TransactionForm; import com.example.demo.app.presentation.form.TransactionForm;
import com.example.demo.app.presentation.form.UserForm; import com.example.demo.app.presentation.form.UserForm;
@@ -349,19 +348,19 @@ public class TransactionController {
model.addAttribute("totalIncome", totalIncome); model.addAttribute("totalIncome", totalIncome);
model.addAttribute("totalExpense", totalExpense); model.addAttribute("totalExpense", totalExpense);
GoalForm goalForm = GoalForm.convertFrom(goalService.getGoalByUserId(loginUser.getUserId())); // List<GoalForm> goalForm = GoalForm.convertFrom(goalService.getGoalByUserId(loginUser.getUserId()));
UserForm userForm = UserForm.convertFrom(userService.findByUserId(loginUser.getUserId())); UserForm userForm = UserForm.convertFrom(userService.findByUserId(loginUser.getUserId()));
model.addAttribute("achievementRate", goalForm.getAchievementRate()); // model.addAttribute("achievementRate", goalForm.getAchievementRate());
//
model.addAttribute("currentAmount", goalForm.getCurrentAmount()); // model.addAttribute("currentAmount", goalForm.getCurrentAmount());
//
model.addAttribute("targetAmount", goalForm.getTargetAmount()); // model.addAttribute("targetAmount", goalForm.getTargetAmount());
model.addAttribute("saving", userForm.getSaving()); model.addAttribute("saving", userForm.getSaving());
model.addAttribute("deadline", goalForm.getDeadline()); // model.addAttribute("deadline", goalForm.getDeadline());
return "menu"; return "menu";
} }

View File

@@ -1,6 +1,7 @@
package com.example.demo.app.presentation.form; package com.example.demo.app.presentation.form;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.List;
import org.springframework.format.annotation.DateTimeFormat; import org.springframework.format.annotation.DateTimeFormat;
@@ -31,6 +32,8 @@ public class GoalForm {
private Integer achievementRate; // 達成率 private Integer achievementRate; // 達成率
private Integer currentAmount; // 現在の資産 private Integer currentAmount; // 現在の資産
private Long userId;
public static GoalForm convertFrom(GoalModel goalModel) { public static GoalForm convertFrom(GoalModel goalModel) {
return new GoalForm( return new GoalForm(
@@ -41,11 +44,12 @@ public class GoalForm {
goalModel.getDeadline(), goalModel.getDeadline(),
goalModel.getGoalComplete(), goalModel.getGoalComplete(),
goalModel.getAchievementRate(), goalModel.getAchievementRate(),
goalModel.getCurrentAmount()); goalModel.getCurrentAmount(),
goalModel.getUserId());
} }
// public static List<GoalForm> convertFrom(List<GoalModel> goalModelList) { public static List<GoalForm> convertFrom(List<GoalModel> goalModelList) {
// return goalModelList.stream().map(GoalForm::convertFrom).toList(); return goalModelList.stream().map(GoalForm::convertFrom).toList();
// } }
} }

View File

@@ -113,17 +113,17 @@
</div> </div>
<div> <!-- <div>-->
<span>目標貯金</span> <!-- <span>目標貯金</span>-->
<span th:text="${targetAmount}"></span> <!-- <span th:text="${targetAmount}"></span>-->
<span>円</span> <!-- <span>円</span>-->
</div> <!-- </div>-->
<div> <!-- <div>-->
<span>現在高</span> <!-- <span>現在高</span>-->
<span th:text="${currentAmount}"></span> <!-- <span th:text="${currentAmount}"></span>-->
<span>円</span> <!-- <span>円</span>-->
</div> <!-- </div>-->
<div> <div>
<span>貯金額</span> <span>貯金額</span>
@@ -133,16 +133,16 @@
<hr> <hr>
<div> <!-- <div>-->
<span>達成率</span> <!-- <span>達成率</span>-->
<span th:text="${achievementRate}"></span> <!-- <span th:text="${achievementRate}"></span>-->
<span>%</span> <!-- <span>%</span>-->
</div> <!-- </div>-->
<div> <!-- <div>-->
<span>達成期限</span> <!-- <span>達成期限</span>-->
<span th:text="${deadline}"></span> <!-- <span th:text="${deadline}"></span>-->
<span>円</span> <!-- <span>円</span>-->
</div> <!-- </div>-->
<div th:replace="~{footer :: footer}"></div> <div th:replace="~{footer :: footer}"></div>