Compare commits
6 Commits
e0a7a3496f
...
d308f14df4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d308f14df4 | ||
|
|
a36705d83f | ||
|
|
ab802cdb25 | ||
|
|
1160af5711 | ||
|
|
d2cf2e6e98 | ||
| 69213bcf9c |
11
src/main/java/com/example/demo/aop/aspect/SessionCheck.java
Normal file
11
src/main/java/com/example/demo/aop/aspect/SessionCheck.java
Normal 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 {
|
||||
}
|
||||
@@ -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("不正なログインです");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
@@ -42,37 +42,82 @@ public class TransactionController {
|
||||
|
||||
// 一覧画面
|
||||
@GetMapping("/transaction/list")
|
||||
public String transactionList(@RequestParam(required = false) LocalDate startDates,
|
||||
@RequestParam(required = false) LocalDate endDates, Model model,
|
||||
public String transactionList(
|
||||
@RequestParam(required = false, defaultValue = "0") String years,
|
||||
@RequestParam(required = false, defaultValue = "0") String months,
|
||||
Model model,
|
||||
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");
|
||||
|
||||
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<>();
|
||||
|
||||
transactionService.findAllByDate(startDate, endDate, loginUser.getUserId()).forEach((date, list) -> {
|
||||
List<TransactionForm> transactionFormList = list.stream().map(transaction -> {
|
||||
System.out.println(transaction.getTransactionDate());
|
||||
TransactionForm transactionForm = TransactionForm.convertFrom(
|
||||
transactionService
|
||||
.findAllByDate(
|
||||
startDate,
|
||||
endDate,
|
||||
loginUser.getUserId())
|
||||
.forEach((date, list) -> {
|
||||
|
||||
List<TransactionForm> transactionFormList = list.stream()
|
||||
.map(transaction -> {
|
||||
|
||||
CategoryForm categoryForm = CategoryForm.convertFrom(
|
||||
categoryService.findOne(
|
||||
transaction.getCategoryId()));
|
||||
|
||||
return TransactionForm.convertFrom(
|
||||
transaction,
|
||||
CategoryForm.convertFrom(categoryService.findOne(transaction.getCategoryId()))
|
||||
.getCategoryName(),
|
||||
transactionService.findTotalExpense(startDate, endDate, loginUser.getUserId()),
|
||||
transactionService.findTotalIncome(startDate, endDate, loginUser.getUserId()),
|
||||
CategoryForm.convertFrom(categoryService.findOne(transaction.getCategoryId()))
|
||||
.getTransactionType());
|
||||
System.out.println("startDate = " + startDate);
|
||||
System.out.println("endDate = " + endDate);
|
||||
return transactionForm;
|
||||
}).collect(Collectors.toList());
|
||||
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);
|
||||
|
||||
return "transaction_list";
|
||||
@@ -189,7 +234,6 @@ public class TransactionController {
|
||||
|
||||
System.out.println("category: " + transactionForm.getCategoryId() + "金額: " + transactionForm.getAmount());
|
||||
|
||||
|
||||
model.addAttribute("transactionId", transactionId);
|
||||
|
||||
model.addAttribute("transactionForm", transactionForm);
|
||||
@@ -203,7 +247,8 @@ public class TransactionController {
|
||||
}
|
||||
|
||||
@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) {
|
||||
|
||||
LoginModel loginUser = (LoginModel) session.getAttribute("loginUser");
|
||||
@@ -255,7 +300,6 @@ public class TransactionController {
|
||||
int nextYear = nextYearMonth.getYear();
|
||||
int nextMonth = nextYearMonth.getMonthValue();
|
||||
|
||||
|
||||
model.addAttribute("year", year);
|
||||
model.addAttribute("month", month);
|
||||
model.addAttribute("day", today.getDayOfMonth());
|
||||
@@ -272,7 +316,6 @@ public class TransactionController {
|
||||
LocalDate startDate = yearMonth.atDay(1);
|
||||
LocalDate endDate = yearMonth.atEndOfMonth();
|
||||
|
||||
|
||||
List<Income> incomeList = transactionService.findTotalIncomeList(startDate, endDate, loginUser.getUserId());
|
||||
|
||||
List<Expense> expenseList = transactionService.findTotalExpenseList(startDate, endDate, loginUser.getUserId());
|
||||
@@ -280,17 +323,13 @@ public class TransactionController {
|
||||
model.addAttribute("incomeList", incomeList);
|
||||
model.addAttribute("expenseList", expenseList);
|
||||
|
||||
|
||||
int totalIncome = transactionService.findTotalIncome(startDate, endDate, loginUser.getUserId());
|
||||
|
||||
int totalExpense = transactionService.findTotalExpense(startDate, endDate, loginUser.getUserId());
|
||||
|
||||
|
||||
model.addAttribute("totalIncome", totalIncome);
|
||||
model.addAttribute("totalExpense", totalExpense);
|
||||
|
||||
|
||||
|
||||
GoalForm goalForm = GoalForm.convertFrom(goalService.getGoalByUserId(loginUser.getUserId()));
|
||||
|
||||
UserForm userForm = UserForm.convertFrom(userService.findByUserId(loginUser.getUserId()));
|
||||
@@ -303,10 +342,7 @@ public class TransactionController {
|
||||
|
||||
model.addAttribute("deadline", goalForm.getDeadline());
|
||||
|
||||
|
||||
|
||||
return "menu";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.example.demo.app.presentation.form;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import com.example.demo.app.domain.model.GoalModel;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
@@ -21,6 +23,7 @@ public class GoalForm {
|
||||
|
||||
private Integer targetAmount;
|
||||
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd")
|
||||
private LocalDate deadline;
|
||||
|
||||
private Integer goalComplete;
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.example.demo.exception;
|
||||
|
||||
public class InsufficientPermissionException extends RuntimeException {
|
||||
|
||||
public InsufficientPermissionException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.example.demo.exception;
|
||||
|
||||
public class NoSessionException extends RuntimeException {
|
||||
|
||||
public NoSessionException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -22,7 +22,7 @@ th {
|
||||
}
|
||||
|
||||
td {
|
||||
width: 14.28%;
|
||||
width: 14%;
|
||||
height: 70px;
|
||||
padding: 5px;
|
||||
text-align: left;
|
||||
|
||||
@@ -129,7 +129,7 @@
|
||||
<div>
|
||||
<span>達成率</span>
|
||||
<span th:text="${achievementRate}"></span>
|
||||
<span>円</span>
|
||||
<span>%</span>
|
||||
</div>
|
||||
<div>
|
||||
<span>達成期限</span>
|
||||
|
||||
@@ -13,6 +13,33 @@
|
||||
|
||||
<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}">
|
||||
|
||||
<a th:href="@{/transaction/detail(todayDates=${transactionForm.key})}">
|
||||
|
||||
Reference in New Issue
Block a user