[EDIT] 一覧画面でのつき選択機能を追加しました。

This commit is contained in:
s-choi
2026-09-18 15:35:53 +09:00
parent e0a7a3496f
commit d2cf2e6e98
4 changed files with 148 additions and 85 deletions

View File

@@ -42,36 +42,81 @@ public class TransactionController {
// 一覧画面 // 一覧画面
@GetMapping("/transaction/list") @GetMapping("/transaction/list")
public String transactionList(@RequestParam(required = false) LocalDate startDates, public String transactionList(
@RequestParam(required = false) LocalDate endDates, Model model, @RequestParam(required = false, defaultValue = "0") String years,
@RequestParam(required = false, defaultValue = "0") String months,
Model model,
HttpSession session) { HttpSession session) {
LocalDate startDate = (startDates == null) ? LocalDate.now().withDayOfMonth(1) : startDates;
LocalDate endDate = (endDates == null) ? LocalDate.now().withDayOfMonth(LocalDate.now().lengthOfMonth())
: endDates;
LoginModel loginUser = (LoginModel) session.getAttribute("loginUser"); LoginModel loginUser = (LoginModel) session.getAttribute("loginUser");
LocalDate today = LocalDate.now();
int year = years.equals("0")
? today.getYear()
: Integer.parseInt(years);
int month = months.equals("0")
? today.getMonthValue()
: Integer.parseInt(months);
YearMonth yearMonth = YearMonth.of(year, month);
YearMonth previousYearMonth = yearMonth.minusMonths(1);
int previousYear = previousYearMonth.getYear();
int previousMonth = previousYearMonth.getMonthValue();
YearMonth nextYearMonth = yearMonth.plusMonths(1);
int nextYear = nextYearMonth.getYear();
int nextMonth = nextYearMonth.getMonthValue();
LocalDate startDate = yearMonth.atDay(1);
LocalDate endDate = yearMonth.atEndOfMonth();
Map<LocalDate, List<TransactionForm>> transactionFormMap = new HashMap<>(); Map<LocalDate, List<TransactionForm>> transactionFormMap = new HashMap<>();
transactionService.findAllByDate(startDate, endDate, loginUser.getUserId()).forEach((date, list) -> { transactionService
List<TransactionForm> transactionFormList = list.stream().map(transaction -> { .findAllByDate(
System.out.println(transaction.getTransactionDate()); startDate,
TransactionForm transactionForm = TransactionForm.convertFrom( endDate,
transaction, loginUser.getUserId())
CategoryForm.convertFrom(categoryService.findOne(transaction.getCategoryId())) .forEach((date, list) -> {
.getCategoryName(),
transactionService.findTotalExpense(startDate, endDate, loginUser.getUserId()), List<TransactionForm> transactionFormList = list.stream()
transactionService.findTotalIncome(startDate, endDate, loginUser.getUserId()), .map(transaction -> {
CategoryForm.convertFrom(categoryService.findOne(transaction.getCategoryId()))
.getTransactionType()); CategoryForm categoryForm = CategoryForm.convertFrom(
System.out.println("startDate = " + startDate); categoryService.findOne(
System.out.println("endDate = " + endDate); transaction.getCategoryId()));
return transactionForm;
}).collect(Collectors.toList()); return TransactionForm.convertFrom(
transactionFormMap.put(date, transactionFormList); transaction,
}); categoryForm.getCategoryName(),
transactionService.findTotalExpense(
startDate,
endDate,
loginUser.getUserId()),
transactionService.findTotalIncome(
startDate,
endDate,
loginUser.getUserId()),
categoryForm.getTransactionType());
})
.collect(Collectors.toList());
transactionFormMap.put(date, transactionFormList);
});
model.addAttribute("year", year);
model.addAttribute("month", month);
model.addAttribute("previousYear", previousYear);
model.addAttribute("previousMonth", previousMonth);
model.addAttribute("nextYear", nextYear);
model.addAttribute("nextMonth", nextMonth);
model.addAttribute("transactionFormMap", transactionFormMap); model.addAttribute("transactionFormMap", transactionFormMap);
@@ -189,7 +234,6 @@ public class TransactionController {
System.out.println("category: " + transactionForm.getCategoryId() + "金額: " + transactionForm.getAmount()); System.out.println("category: " + transactionForm.getCategoryId() + "金額: " + transactionForm.getAmount());
model.addAttribute("transactionId", transactionId); model.addAttribute("transactionId", transactionId);
model.addAttribute("transactionForm", transactionForm); model.addAttribute("transactionForm", transactionForm);
@@ -203,7 +247,8 @@ public class TransactionController {
} }
@PostMapping("/transaction/update") @PostMapping("/transaction/update")
public String updateOnePost(@RequestParam Long transactionId, @ModelAttribute TransactionForm transactionForm, @RequestParam LocalDate today, Model model, public String updateOnePost(@RequestParam Long transactionId, @ModelAttribute TransactionForm transactionForm,
@RequestParam LocalDate today, Model model,
HttpSession session) { HttpSession session) {
LoginModel loginUser = (LoginModel) session.getAttribute("loginUser"); LoginModel loginUser = (LoginModel) session.getAttribute("loginUser");
@@ -228,85 +273,76 @@ public class TransactionController {
@GetMapping("/menu") @GetMapping("/menu")
public String menu(HttpSession session, Model model, public String menu(HttpSession session, Model model,
@RequestParam(required = false, defaultValue = "0") String years, @RequestParam(required = false, defaultValue = "0") String years,
@RequestParam(required = false, defaultValue = "0") String months) { @RequestParam(required = false, defaultValue = "0") String months) {
LoginModel loginUser = (LoginModel) session.getAttribute("loginUser"); LoginModel loginUser = (LoginModel) session.getAttribute("loginUser");
LocalDate today = LocalDate.now(); LocalDate today = LocalDate.now();
int year = (years.equals("0")) ? today.getYear() : Integer.parseInt(years); int year = (years.equals("0")) ? today.getYear() : Integer.parseInt(years);
int month = (months.equals("0")) ? today.getMonthValue() : Integer.parseInt(months); int month = (months.equals("0")) ? today.getMonthValue() : Integer.parseInt(months);
YearMonth yearMonth = YearMonth.of(year, month); YearMonth yearMonth = YearMonth.of(year, month);
int lastDay = yearMonth.lengthOfMonth(); int lastDay = yearMonth.lengthOfMonth();
int firstDayOfWeek = yearMonth.atDay(1).getDayOfWeek().getValue() % 7; int firstDayOfWeek = yearMonth.atDay(1).getDayOfWeek().getValue() % 7;
YearMonth previousYearMonth = yearMonth.minusMonths(1); YearMonth previousYearMonth = yearMonth.minusMonths(1);
int previousYear = previousYearMonth.getYear(); int previousYear = previousYearMonth.getYear();
int previousMonth = previousYearMonth.getMonthValue(); int previousMonth = previousYearMonth.getMonthValue();
YearMonth nextYearMonth = yearMonth.plusMonths(1); YearMonth nextYearMonth = yearMonth.plusMonths(1);
int nextYear = nextYearMonth.getYear(); int nextYear = nextYearMonth.getYear();
int nextMonth = nextYearMonth.getMonthValue(); int nextMonth = nextYearMonth.getMonthValue();
model.addAttribute("year", year);
model.addAttribute("month", month);
model.addAttribute("day", today.getDayOfMonth());
model.addAttribute("year", year); model.addAttribute("lastDay", lastDay);
model.addAttribute("month", month); model.addAttribute("firstDayOfWeek", firstDayOfWeek);
model.addAttribute("day", today.getDayOfMonth());
model.addAttribute("lastDay", lastDay); model.addAttribute("previousYear", previousYear);
model.addAttribute("firstDayOfWeek", firstDayOfWeek); model.addAttribute("previousMonth", previousMonth);
model.addAttribute("previousYear", previousYear); model.addAttribute("nextYear", nextYear);
model.addAttribute("previousMonth", previousMonth); model.addAttribute("nextMonth", nextMonth);
model.addAttribute("nextYear", nextYear); LocalDate startDate = yearMonth.atDay(1);
model.addAttribute("nextMonth", nextMonth); LocalDate endDate = yearMonth.atEndOfMonth();
LocalDate startDate = yearMonth.atDay(1); List<Income> incomeList = transactionService.findTotalIncomeList(startDate, endDate, loginUser.getUserId());
LocalDate endDate = yearMonth.atEndOfMonth();
List<Expense> expenseList = transactionService.findTotalExpenseList(startDate, endDate, loginUser.getUserId());
List<Income> incomeList = transactionService.findTotalIncomeList(startDate, endDate, loginUser.getUserId()); model.addAttribute("incomeList", incomeList);
model.addAttribute("expenseList", expenseList);
List<Expense> expenseList = transactionService.findTotalExpenseList(startDate, endDate, loginUser.getUserId()); int totalIncome = transactionService.findTotalIncome(startDate, endDate, loginUser.getUserId());
model.addAttribute("incomeList", incomeList); int totalExpense = transactionService.findTotalExpense(startDate, endDate, loginUser.getUserId());
model.addAttribute("expenseList", expenseList);
model.addAttribute("totalIncome", totalIncome);
model.addAttribute("totalExpense", totalExpense);
int totalIncome = transactionService.findTotalIncome(startDate, endDate, loginUser.getUserId()); GoalForm goalForm = GoalForm.convertFrom(goalService.getGoalByUserId(loginUser.getUserId()));
int totalExpense = transactionService.findTotalExpense(startDate, endDate, loginUser.getUserId()); UserForm userForm = UserForm.convertFrom(userService.findByUserId(loginUser.getUserId()));
model.addAttribute("achievementRate", goalForm.getAchievementRate());
model.addAttribute("totalIncome", totalIncome); model.addAttribute("targetAmount", goalForm.getTargetAmount());
model.addAttribute("totalExpense", totalExpense);
model.addAttribute("saving", userForm.getSaving());
model.addAttribute("deadline", goalForm.getDeadline());
GoalForm goalForm = GoalForm.convertFrom(goalService.getGoalByUserId(loginUser.getUserId())); return "menu";
UserForm userForm = UserForm.convertFrom(userService.findByUserId(loginUser.getUserId()));
model.addAttribute("achievementRate", goalForm.getAchievementRate());
model.addAttribute("targetAmount", goalForm.getTargetAmount());
model.addAttribute("saving", userForm.getSaving());
model.addAttribute("deadline", goalForm.getDeadline());
return "menu";
} }
} }

View File

@@ -22,7 +22,7 @@ th {
} }
td { td {
width: 14.28%; width: 14%;
height: 70px; height: 70px;
padding: 5px; padding: 5px;
text-align: left; text-align: left;

View File

@@ -129,7 +129,7 @@
<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>

View File

@@ -13,6 +13,33 @@
<div th:replace="~{header :: header}"></div> <div th:replace="~{header :: header}"></div>
<div class="calendar-title">
<a class="month-button" th:href="@{/transaction/list(
years=${previousYear},
months=${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="@{/transaction/list(
years=${nextYear},
months=${nextMonth}
)}">
→
</a>
</div>
<div th:each="transactionForm : ${transactionFormMap}"> <div th:each="transactionForm : ${transactionFormMap}">
<a th:href="@{/transaction/detail(todayDates=${transactionForm.key})}"> <a th:href="@{/transaction/detail(todayDates=${transactionForm.key})}">