[EDIT]二次実装である支出分析の年月表示を実装しました

This commit is contained in:
2026-09-18 16:03:32 +09:00
parent 1d010dc02b
commit 79b1abeac6
4 changed files with 62 additions and 8 deletions

View File

@@ -4,6 +4,9 @@ import com.example.demo.app.domain.model.AnalysisModel;
public interface AnalysisService { public interface AnalysisService {
AnalysisModel getAnalysis(Long userId); AnalysisModel getAnalysis(
Long userId,
Integer year,
Integer month);
} }

View File

@@ -30,7 +30,7 @@ public class AnalysisServiceImpl implements AnalysisService {
private final CategoryRepository categoryRepository; private final CategoryRepository categoryRepository;
@Override @Override
public AnalysisModel getAnalysis(Long userId) { public AnalysisModel getAnalysis(Long userId, Integer year, Integer month) {
// ユーザー情報取得、月別使用可能資金取得 // ユーザー情報取得、月別使用可能資金取得
UserEntity userEntity = userRepository.findById(userId).orElseThrow(() -> UserEntity userEntity = userRepository.findById(userId).orElseThrow(() ->
@@ -44,12 +44,15 @@ public class AnalysisServiceImpl implements AnalysisService {
Integer monthlyBudget =userEntity.getMonthlyBudget(); Integer monthlyBudget =userEntity.getMonthlyBudget();
// 今月の期間取得 // 今月の期間取得
LocalDate currentMonthStartDate = LocalDate.now().withDayOfMonth(1); LocalDate targetDate = LocalDate.of(year, month, 1);
LocalDate currentMonthEndDate = LocalDate.now().withDayOfMonth(LocalDate.now().lengthOfMonth()); LocalDate currentMonthStartDate = targetDate.withDayOfMonth(1);
LocalDate currentMonthEndDate = targetDate.withDayOfMonth(targetDate.lengthOfMonth());
// 前月の期間取得 // 前月の期間取得
LocalDate previousMonthStartDate = LocalDate.now().minusMonths(1).withDayOfMonth(1); LocalDate previousDate = targetDate.minusMonths(1);
LocalDate previousMonthEndDate = LocalDate.now().minusMonths(1).withDayOfMonth(LocalDate.now().minusMonths(1).lengthOfMonth());
LocalDate previousMonthStartDate = previousDate.withDayOfMonth(1);
LocalDate previousMonthEndDate = previousDate.withDayOfMonth(previousDate.lengthOfMonth());
// 今月の支出取得 // 今月の支出取得
Integer currentMonthTotalAmount = transactionRepository.findTotalExpense(currentMonthStartDate, currentMonthEndDate, userId); Integer currentMonthTotalAmount = transactionRepository.findTotalExpense(currentMonthStartDate, currentMonthEndDate, userId);

View File

@@ -1,10 +1,13 @@
package com.example.demo.app.presentation.controller; package com.example.demo.app.presentation.controller;
import java.time.LocalDate;
import jakarta.servlet.http.HttpSession; import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.example.demo.app.domain.model.AnalysisModel; import com.example.demo.app.domain.model.AnalysisModel;
import com.example.demo.app.domain.model.LoginModel; import com.example.demo.app.domain.model.LoginModel;
@@ -21,18 +24,45 @@ public class AnalysisController {
private final AnalysisService analysisService; private final AnalysisService analysisService;
@GetMapping("/analysis") @GetMapping("/analysis")
public String showAnalysis(Model model, HttpSession session) { public String showAnalysis(
@RequestParam(required = false) Integer year,
@RequestParam(required = false) Integer month,
Model model,
HttpSession session) {
//ログインユーザーの取得(セッション) //ログインユーザーの取得(セッション)
LoginModel loginUser = (LoginModel) session.getAttribute("loginUser"); LoginModel loginUser = (LoginModel) session.getAttribute("loginUser");
//初期年月表示
LocalDate nowDate = LocalDate.now();
if (year == null) {
year = nowDate.getYear();
}
if (month == null) {
month = nowDate.getMonthValue();
}
//前月・翌月作成
LocalDate targetDate = LocalDate.of(year, month, 1);
LocalDate previousDate = targetDate.minusMonths(1);
LocalDate nextDate = targetDate.plusMonths(1);
//分析結果の取得(Service) //分析結果の取得(Service)
AnalysisModel analysisModel = analysisService.getAnalysis(loginUser.getUserId()); AnalysisModel analysisModel = analysisService.getAnalysis(loginUser.getUserId(), year, month);
AnalysisForm analysisForm = AnalysisForm.convertFrom(analysisModel); AnalysisForm analysisForm = AnalysisForm.convertFrom(analysisModel);
//model格納 //model格納
model.addAttribute("analysisForm", analysisForm); model.addAttribute("analysisForm", analysisForm);
model.addAttribute("year", year);
model.addAttribute("month", month);
model.addAttribute("previousYear", previousDate.getYear());
model.addAttribute("previousMonth", previousDate.getMonthValue());
model.addAttribute("nextYear", nextDate.getYear());
model.addAttribute("nextMonth", nextDate.getMonthValue());
return "analysis"; return "analysis";
} }

View File

@@ -5,6 +5,7 @@
<title>分析画面</title> <title>分析画面</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css" rel="stylesheet"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
</head> </head>
<body class="pb-5 pt-5"> <body class="pb-5 pt-5">
<div th:replace="~{header :: header}"> <div th:replace="~{header :: header}">
@@ -13,6 +14,23 @@
<div class="container mt-4"> <div class="container mt-4">
<h2 class="text-center">支出分析</h2> <h2 class="text-center">支出分析</h2>
<div class="text-center">
<a class="month-button" th:href="@{/analysis(year=${previousYear}, month=${previousMonth})}">
←
</a>
<div class="calendar-date">
<span class="year" th:text="|${year}年|"></span>
<span class="month" th:text="|${month}月|"></span>
</div>
<a class="month-button" th:href="@{/analysis(year=${nextYear}, month=${nextMonth})}">
→
</a>
</div>
<table class="table text-center"> <table class="table text-center">
<thead> <thead>
<tr> <tr>