6 Commits

11 changed files with 283 additions and 108 deletions

View File

@@ -0,0 +1,11 @@
package com.example.demo.aop.aspect;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface SessionCheck {
}

View File

@@ -0,0 +1,41 @@
package com.example.demo.aop.aspect;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.example.demo.exception.NoSessionException;
@Aspect
@Component
public class SessionCheckAspect {
HttpServletRequest request;
@Autowired
public SessionCheckAspect(HttpServletRequest request) {
this.request = request;
}
@Pointcut("@annotation(com.example.demo.aop.aspect.SessionCheck)")
public void sessionCheckMethod() {
}
@Before("sessionCheckMethod()")
public void checkSession() {
HttpSession session = request.getSession(false);
if (session == null || session.getAttribute("loginUser") == null) {
throw new NoSessionException("不正なログインです");
}
}
}

View File

@@ -0,0 +1,61 @@
package com.example.demo.aop.handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.ui.Model;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import com.example.demo.exception.NoSessionException;
@ControllerAdvice
public class GlobalExceptionHandler {
private final static Logger LOGGER = Logger.getLogger(GlobalExceptionHandler.class.getName());
private final String SEVERE_ERROR_MESSAGE_ILLEGAL_TRANSITION = "不正な遷移です。";
private final String SEVERE_ERROR_MESSAGE_BAD_REQUEST = "不正なリクエストです。";
@ExceptionHandler(NoSessionException.class)
public String handleNoSessionException(Model model, Exception e) {
e.printStackTrace();
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE_ILLEGAL_TRANSITION, e);
model.addAttribute(
"errorMessage",
"システムエラーが発生しました。"
+ "初めからやり直してください。");
return "error";
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public String handleBadRequestException(Model model, Exception e) {
model.addAttribute("error", SEVERE_ERROR_MESSAGE_BAD_REQUEST);
model.addAttribute("status", HttpStatus.BAD_REQUEST.value());
e.printStackTrace();
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE_ILLEGAL_TRANSITION, e);
return "error";
}
@ExceptionHandler(Exception.class)
public String AllException(Model model, Exception e) {
model.addAttribute("error", SEVERE_ERROR_MESSAGE_BAD_REQUEST);
e.printStackTrace();
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE_ILLEGAL_TRANSITION, e);
return "error";
}
}

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);
@@ -134,7 +179,7 @@ public class TransactionController {
@GetMapping("/transaction/create") @GetMapping("/transaction/create")
public String createOneGet(@RequestParam(defaultValue = "") LocalDate today, public String createOneGet(@RequestParam(defaultValue = "") LocalDate today,
Model model) { Model model) {
if (today == null) { if (today == null) {
today = LocalDate.now(); today = LocalDate.now();
} }
@@ -189,28 +234,28 @@ 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);
model.addAttribute("categoryFormList", categoryFormList); model.addAttribute("categoryFormList", categoryFormList);
model.addAttribute("today", today); model.addAttribute("today", today);
return "transaction_update"; return "transaction_update";
} }
@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");
transactionForm.setUserId(loginUser.getUserId()); transactionForm.setUserId(loginUser.getUserId());
transactionForm.setTransactionId(transactionId); transactionForm.setTransactionId(transactionId);
if (transactionService.updateTransaction(TransactionForm.convertTo(transactionForm)) != 0) { if (transactionService.updateTransaction(TransactionForm.convertTo(transactionForm)) != 0) {
return "error"; return "error";
} }
@@ -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);
GoalForm goalForm = GoalForm.convertFrom(goalService.getGoalByUserId(loginUser.getUserId())); model.addAttribute("saving", userForm.getSaving());
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"; model.addAttribute("deadline", goalForm.getDeadline());
return "menu";
} }
} }

View File

@@ -2,6 +2,8 @@ package com.example.demo.app.presentation.form;
import java.time.LocalDate; import java.time.LocalDate;
import org.springframework.format.annotation.DateTimeFormat;
import com.example.demo.app.domain.model.GoalModel; import com.example.demo.app.domain.model.GoalModel;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
@@ -20,7 +22,8 @@ public class GoalForm {
private String goalSubtitle; private String goalSubtitle;
private Integer targetAmount; private Integer targetAmount;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate deadline; private LocalDate deadline;
private Integer goalComplete; private Integer goalComplete;

View File

@@ -1,22 +0,0 @@
package com.example.demo.exception;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public String handleException(
Exception e,
Model model) {
model.addAttribute(
"errorMessage",
"システムエラーが発生しました。"
+ "初めからやり直してください。");
return "error";
}
}

View File

@@ -0,0 +1,9 @@
package com.example.demo.exception;
public class InsufficientPermissionException extends RuntimeException {
public InsufficientPermissionException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,9 @@
package com.example.demo.exception;
public class NoSessionException extends RuntimeException {
public NoSessionException(String message) {
super(message);
}
}

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

@@ -12,7 +12,34 @@
<body class="pb-5 pt-5"> <body class="pb-5 pt-5">
<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})}">