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 com.example.demo.app.infra.entity.GoalEntity;
import com.example.demo.app.infra.entity.UserEntity;
import com.example.demo.app.presentation.form.GoalForm;
import lombok.AllArgsConstructor;
@@ -27,8 +28,10 @@ public class GoalModel {
private Integer achievementRate; // 達成率
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(
goalEntity.getGoalId(),
goalEntity.getGoalTitle(),
@@ -36,10 +39,12 @@ public class GoalModel {
goalEntity.getTargetAmount(),
goalEntity.getDeadline(),
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(
goalModel.getGoalId(),
goalModel.getGoalTitle(),
@@ -47,7 +52,7 @@ public class GoalModel {
goalModel.getTargetAmount(),
goalModel.getDeadline(),
goalModel.getGoalComplete(),
null);
userEntity, 0);
}
public static GoalModel convertFrom(GoalForm goalForm) {
@@ -58,7 +63,8 @@ public class GoalModel {
goalForm.getTargetAmount(),
goalForm.getDeadline(),
goalForm.getGoalComplete()
,null,null);
,null,null,
goalForm.getUserId());
}
}

View File

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

View File

@@ -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<GoalModel> getGoalByUserId(Long userId) {
UserEntity userEntity = userRepository.findById(userId).orElseThrow();
GoalEntity goalEntity = userEntity.getGoalEntity();
List<GoalEntity> 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<GoalModel> goalModelList = new ArrayList<GoalModel>();
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);
}
}

View File

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

View File

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

View File

@@ -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<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);
goalService.updateGoal(goalModel);
goalService.updateGoal(goalModel, userId);
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.Expense;
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.TransactionForm;
import com.example.demo.app.presentation.form.UserForm;
@@ -349,19 +348,19 @@ public class TransactionController {
model.addAttribute("totalIncome", totalIncome);
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()));
model.addAttribute("achievementRate", goalForm.getAchievementRate());
model.addAttribute("currentAmount", goalForm.getCurrentAmount());
model.addAttribute("targetAmount", goalForm.getTargetAmount());
// model.addAttribute("achievementRate", goalForm.getAchievementRate());
//
// model.addAttribute("currentAmount", goalForm.getCurrentAmount());
//
// model.addAttribute("targetAmount", goalForm.getTargetAmount());
model.addAttribute("saving", userForm.getSaving());
model.addAttribute("deadline", goalForm.getDeadline());
// model.addAttribute("deadline", goalForm.getDeadline());
return "menu";
}

View File

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

View File

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