10 Commits

30 changed files with 628 additions and 125 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

@@ -25,6 +25,8 @@ public class GoalModel {
private Integer goalComplete; private Integer goalComplete;
private Integer achievementRate; // 達成率 private Integer achievementRate; // 達成率
private Integer currentAmount; // 現在の資産
public static GoalModel convertFrom(GoalEntity goalEntity) { public static GoalModel convertFrom(GoalEntity goalEntity) {
return new GoalModel( return new GoalModel(
@@ -34,7 +36,7 @@ public class GoalModel {
goalEntity.getTargetAmount(), goalEntity.getTargetAmount(),
goalEntity.getDeadline(), goalEntity.getDeadline(),
goalEntity.getGoalComplete(), goalEntity.getGoalComplete(),
null); null,null);
} }
public static GoalEntity convertTo(GoalModel goalModel) { public static GoalEntity convertTo(GoalModel goalModel) {
@@ -56,7 +58,7 @@ public class GoalModel {
goalForm.getTargetAmount(), goalForm.getTargetAmount(),
goalForm.getDeadline(), goalForm.getDeadline(),
goalForm.getGoalComplete() goalForm.getGoalComplete()
,null); ,null,null);
} }
} }

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

@@ -1,8 +1,10 @@
package com.example.demo.app.domain.service; package com.example.demo.app.domain.service;
import com.example.demo.app.domain.model.ProfileModel; import com.example.demo.app.domain.model.ProfileModel;
import com.example.demo.app.presentation.form.ProfileEditForm;
public interface ProfileService { public interface ProfileService {
ProfileModel findOne(Long userId); ProfileModel findOne(Long userId);
void withdraw(Long userId); void withdraw(Long userId);
void update(Long userId, ProfileEditForm profileEditForm);
} }

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

@@ -56,7 +56,8 @@ public class GoalServiceImpl implements GoalService {
goalModel.getTargetAmount(), goalModel.getTargetAmount(),
goalModel.getDeadline(), goalModel.getDeadline(),
goalModel.getGoalComplete(), goalModel.getGoalComplete(),
achievementRate); achievementRate,
currentAmount);
} }
@Override @Override

View File

@@ -0,0 +1,42 @@
package com.example.demo.app.domain.service.impl;
import org.springframework.stereotype.Service;
import com.example.demo.app.domain.service.PasswordResetService;
import com.example.demo.app.infra.entity.UserEntity;
import com.example.demo.app.infra.repository.UserRepository;
import com.example.demo.app.presentation.form.PasswordResetForm;
import lombok.RequiredArgsConstructor;
@Service
@RequiredArgsConstructor
public class PasswordResetServiceImpl implements PasswordResetService {
private final UserRepository userRepository;
@Override
public boolean resetPassword(
Long userId,
PasswordResetForm passwordResetForm) {
if (!passwordResetForm.getPassword()
.equals(passwordResetForm.getPasswordConfirm())) {
return false;
}
UserEntity userEntity =
userRepository.findById(userId).orElse(null);
if (userEntity == null) {
return false;
}
userEntity.setPassword(
passwordResetForm.getPassword());
userRepository.save(userEntity);
return true;
}
}

View File

@@ -6,6 +6,7 @@ import com.example.demo.app.domain.model.ProfileModel;
import com.example.demo.app.domain.service.ProfileService; import com.example.demo.app.domain.service.ProfileService;
import com.example.demo.app.infra.entity.UserEntity; import com.example.demo.app.infra.entity.UserEntity;
import com.example.demo.app.infra.repository.UserRepository; import com.example.demo.app.infra.repository.UserRepository;
import com.example.demo.app.presentation.form.ProfileEditForm;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@@ -34,7 +35,7 @@ public class ProfileServiceImpl implements ProfileService{
@Override @Override
public void withdraw(Long userId) { public void withdraw(Long userId) {
UserEntity userEntity =userRepository.findById(userId).orElse(null); UserEntity userEntity = userRepository.findById(userId).orElse(null);
if (userEntity == null) { if (userEntity == null) {
return; return;
@@ -43,4 +44,20 @@ public class ProfileServiceImpl implements ProfileService{
userEntity.setDeleteFlag(1); userEntity.setDeleteFlag(1);
userRepository.save(userEntity); userRepository.save(userEntity);
} }
@Override
public void update(Long userId, ProfileEditForm profileEditForm) {
UserEntity userEntity = userRepository.findById(userId).orElse(null);
if (userEntity == null) {
return;
}
userEntity.setUserName(profileEditForm.getUserName());
userEntity.setMailAddress(profileEditForm.getMailAddress());
userEntity.setSaving(profileEditForm.getSaving());
userEntity.setAnnualIncome(profileEditForm.getAnnualIncome());
userEntity.setMonthlyBudget(profileEditForm.getMonthlyBudget());
userRepository.save(userEntity);
}
} }

View File

@@ -1,11 +1,15 @@
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.aop.aspect.SessionCheck;
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;
import com.example.demo.app.domain.service.AnalysisService; import com.example.demo.app.domain.service.AnalysisService;
@@ -20,19 +24,47 @@ public class AnalysisController {
private final AnalysisService analysisService; private final AnalysisService analysisService;
@SessionCheck
@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

@@ -7,6 +7,7 @@ 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.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import com.example.demo.aop.aspect.SessionCheck;
import com.example.demo.app.domain.model.AuthenticationModel; import com.example.demo.app.domain.model.AuthenticationModel;
import com.example.demo.app.domain.service.AuthenticationService; import com.example.demo.app.domain.service.AuthenticationService;
import com.example.demo.app.infra.repository.QuestionRepository; import com.example.demo.app.infra.repository.QuestionRepository;
@@ -21,6 +22,7 @@ public class AuthenticationController {
private final AuthenticationService authenticationService; private final AuthenticationService authenticationService;
private final QuestionRepository questionRepository; private final QuestionRepository questionRepository;
@SessionCheck
@GetMapping("/authentication") @GetMapping("/authentication")
public String showAuthentication(Model model) { public String showAuthentication(Model model) {
@@ -29,7 +31,8 @@ public class AuthenticationController {
return "authentication"; return "authentication";
} }
@SessionCheck
@PostMapping("/authentication") @PostMapping("/authentication")
public String authenticate( public String authenticate(
AuthenticationForm authenticationForm, AuthenticationForm authenticationForm,

View File

@@ -12,6 +12,7 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import com.example.demo.aop.aspect.SessionCheck;
import com.example.demo.app.domain.model.LoginModel; import com.example.demo.app.domain.model.LoginModel;
import com.example.demo.app.domain.model.UserModel; import com.example.demo.app.domain.model.UserModel;
import com.example.demo.app.domain.service.CourseInfoService; import com.example.demo.app.domain.service.CourseInfoService;
@@ -38,7 +39,7 @@ public class CourseController {
private final UserService userService; private final UserService userService;
@SessionCheck
@PostMapping("/select/register") @PostMapping("/select/register")
public String showSelectCourse(@ModelAttribute UserModel userModel, Model model) { public String showSelectCourse(@ModelAttribute UserModel userModel, Model model) {
@@ -63,6 +64,7 @@ public class CourseController {
} }
} }
@SessionCheck
@PostMapping("/select/change") @PostMapping("/select/change")
public String showChangeCourse(Model model) { public String showChangeCourse(Model model) {
@@ -87,6 +89,7 @@ public class CourseController {
} }
} }
@SessionCheck
@PostMapping("/change") @PostMapping("/change")
public String changeCourse(@RequestParam Long courseId) { public String changeCourse(@RequestParam Long courseId) {

View File

@@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import com.example.demo.aop.aspect.SessionCheck;
import com.example.demo.app.domain.model.GoalModel; import com.example.demo.app.domain.model.GoalModel;
import com.example.demo.app.domain.model.LoginModel; import com.example.demo.app.domain.model.LoginModel;
import com.example.demo.app.domain.service.GoalService; import com.example.demo.app.domain.service.GoalService;
@@ -23,6 +24,7 @@ public class GoalController {
private final GoalService goalService; private final GoalService goalService;
@SessionCheck
@GetMapping("") @GetMapping("")
public String getGoal(Model model, HttpSession session) { public String getGoal(Model model, HttpSession session) {
@@ -37,6 +39,7 @@ public class GoalController {
return "goal_view"; return "goal_view";
} }
@SessionCheck
@GetMapping("/edit") @GetMapping("/edit")
public String editGoal(Model model, HttpSession session) { public String editGoal(Model model, HttpSession session) {
@@ -50,6 +53,7 @@ public class GoalController {
return "goal_edit"; return "goal_edit";
} }
@SessionCheck
@PostMapping("/update") @PostMapping("/update")
public String updateGoal(@ModelAttribute GoalForm goalForm, HttpSession session) { public String updateGoal(@ModelAttribute GoalForm goalForm, HttpSession session) {

View File

@@ -0,0 +1,53 @@
package com.example.demo.app.presentation.controller;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.example.demo.app.domain.service.PasswordResetService;
import com.example.demo.app.presentation.form.PasswordResetForm;
import lombok.RequiredArgsConstructor;
@Controller
@RequiredArgsConstructor
public class PasswordResetController {
private final PasswordResetService passwordResetService;
@GetMapping("/password/reset")
public String showPasswordReset(Model model) {
model.addAttribute("passwordResetForm", new PasswordResetForm());
return "password_reset";
}
@PostMapping("/password/reset")
public String resetPassword(
PasswordResetForm passwordResetForm,
Model model,
HttpSession session){
Long userId =
(Long) session.getAttribute("passwordResetUserId");
if(userId == null) {
return "redirect:/login";
}
boolean isReset =
passwordResetService.resetPassword(userId, passwordResetForm);
if(!isReset) {
model.addAttribute("Message", "パスワードが一致しません。");
return "password_reset";
}
session.removeAttribute("passwordResetUserId");
return "redirect:/login";
}
}

View File

@@ -5,11 +5,14 @@ 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.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import com.example.demo.aop.aspect.SessionCheck;
import com.example.demo.app.domain.model.LoginModel; import com.example.demo.app.domain.model.LoginModel;
import com.example.demo.app.domain.model.ProfileModel; import com.example.demo.app.domain.model.ProfileModel;
import com.example.demo.app.domain.service.ProfileService; import com.example.demo.app.domain.service.ProfileService;
import com.example.demo.app.presentation.form.ProfileEditForm;
import com.example.demo.app.presentation.form.ProfileForm; import com.example.demo.app.presentation.form.ProfileForm;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@@ -19,6 +22,7 @@ import lombok.RequiredArgsConstructor;
public class ProfileController { public class ProfileController {
private final ProfileService profileService; private final ProfileService profileService;
@SessionCheck
@GetMapping("/profile") @GetMapping("/profile")
public String profile(Model model, HttpSession session) { public String profile(Model model, HttpSession session) {
LoginModel loginUser = (LoginModel) session.getAttribute("loginUser"); LoginModel loginUser = (LoginModel) session.getAttribute("loginUser");
@@ -31,11 +35,45 @@ public class ProfileController {
return "profile"; return "profile";
} }
@SessionCheck
@GetMapping("/profile/edit") @GetMapping("/profile/edit")
public String showEditProfile() { public String showEditProfile(
Model model,
HttpSession session) {
LoginModel loginUser =
(LoginModel) session.getAttribute("loginUser");
ProfileModel profileModel =
profileService.findOne(loginUser.getUserId());
ProfileEditForm profileEditForm =
new ProfileEditForm(
profileModel.getUserName(),
profileModel.getMailAddress(),
profileModel.getSaving(),
profileModel.getAnnualIncome(),
profileModel.getMonthlyBudget());
model.addAttribute("profileEditForm", profileEditForm);
return "profile_edit"; return "profile_edit";
} }
@PostMapping("/profile/edit")
public String updateProfile(
@ModelAttribute ProfileEditForm profileEditForm,
HttpSession session) {
LoginModel loginUser =
(LoginModel) session.getAttribute("loginUser");
profileService.update(
loginUser.getUserId(),
profileEditForm);
return "redirect:/profile";
}
@PostMapping("/withdraw") @PostMapping("/withdraw")
public String withdraw(HttpSession session) { public String withdraw(HttpSession session) {
@@ -49,6 +87,7 @@ public class ProfileController {
return "redirect:/login"; return "redirect:/login";
} }
@PostMapping("/logout") @PostMapping("/logout")
public String logout(HttpSession session) { public String logout(HttpSession session) {
session.invalidate(); session.invalidate();

View File

@@ -17,6 +17,7 @@ import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import com.example.demo.aop.aspect.SessionCheck;
import com.example.demo.app.domain.model.LoginModel; import com.example.demo.app.domain.model.LoginModel;
import com.example.demo.app.domain.service.CategoryService; import com.example.demo.app.domain.service.CategoryService;
import com.example.demo.app.domain.service.GoalService; import com.example.demo.app.domain.service.GoalService;
@@ -41,37 +42,83 @@ public class TransactionController {
private final UserService userService; private final UserService userService;
// 一覧画面 // 一覧画面
@SessionCheck
@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);
@@ -79,6 +126,7 @@ public class TransactionController {
} }
// 詳細画面 // 詳細画面
@SessionCheck
@GetMapping("/transaction/detail") @GetMapping("/transaction/detail")
public String findOne(@RequestParam(required = false) LocalDate todayDates, Model model, HttpSession session) { public String findOne(@RequestParam(required = false) LocalDate todayDates, Model model, HttpSession session) {
@@ -131,10 +179,11 @@ public class TransactionController {
return "transaction_detail"; return "transaction_detail";
} }
@SessionCheck
@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();
} }
@@ -155,6 +204,7 @@ public class TransactionController {
return "transaction_create"; return "transaction_create";
} }
@SessionCheck
@PostMapping("/transaction/create") @PostMapping("/transaction/create")
public String createOnePost(@ModelAttribute TransactionForm transactionForm, @RequestParam LocalDate today, public String createOnePost(@ModelAttribute TransactionForm transactionForm, @RequestParam LocalDate today,
Model model, Model model,
@@ -170,6 +220,7 @@ public class TransactionController {
} }
@SessionCheck
@GetMapping("/transaction/update") @GetMapping("/transaction/update")
public String updateOneGet(@RequestParam Long transactionId, @RequestParam(defaultValue = "") LocalDate today, public String updateOneGet(@RequestParam Long transactionId, @RequestParam(defaultValue = "") LocalDate today,
Model model) { Model model) {
@@ -189,28 +240,29 @@ 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";
} }
@SessionCheck
@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";
} }
@@ -219,6 +271,7 @@ public class TransactionController {
} }
@SessionCheck
@PostMapping("/transaction/delete") @PostMapping("/transaction/delete")
public String deleteOne(@RequestParam Long transactionId, @RequestParam LocalDate today) { public String deleteOne(@RequestParam Long transactionId, @RequestParam LocalDate today) {
@@ -226,87 +279,81 @@ public class TransactionController {
return "redirect:/transaction/detail?todayDates=" + today; return "redirect:/transaction/detail?todayDates=" + today;
} }
@SessionCheck
@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("currentAmount", goalForm.getCurrentAmount());
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

@@ -12,6 +12,7 @@ import com.example.demo.app.presentation.form.UserForm;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@Controller @Controller
@RequiredArgsConstructor @RequiredArgsConstructor
public class UserController { public class UserController {

View File

@@ -29,6 +29,8 @@ public class GoalForm {
private Integer goalComplete; private Integer goalComplete;
private Integer achievementRate; // 達成率 private Integer achievementRate; // 達成率
private Integer currentAmount; // 現在の資産
public static GoalForm convertFrom(GoalModel goalModel) { public static GoalForm convertFrom(GoalModel goalModel) {
return new GoalForm( return new GoalForm(
@@ -38,7 +40,8 @@ public class GoalForm {
goalModel.getTargetAmount(), goalModel.getTargetAmount(),
goalModel.getDeadline(), goalModel.getDeadline(),
goalModel.getGoalComplete(), goalModel.getGoalComplete(),
goalModel.getAchievementRate()); goalModel.getAchievementRate(),
goalModel.getCurrentAmount());
} }
// public static List<GoalForm> convertFrom(List<GoalModel> goalModelList) { // public static List<GoalForm> convertFrom(List<GoalModel> goalModelList) {

View File

@@ -0,0 +1,16 @@
package com.example.demo.app.presentation.form;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ProfileEditForm {
private String userName;
private String mailAddress;
private Integer saving;
private Integer annualIncome;
private Integer monthlyBudget;
}

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

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

View File

@@ -14,7 +14,6 @@
<h1>目標詳細</h1> <h1>目標詳細</h1>
<table> <table>
<tr> <tr>
<th>目標名</th> <th>目標名</th>
<td th:text="${goalForm.goalTitle}"></td> <td th:text="${goalForm.goalTitle}"></td>

View File

@@ -118,6 +118,13 @@
<span th:text="${targetAmount}"></span> <span th:text="${targetAmount}"></span>
<span>円</span> <span>円</span>
</div> </div>
<div>
<span>現在高</span>
<span th:text="${currentAmount}"></span>
<span>円</span>
</div>
<div> <div>
<span>貯金額</span> <span>貯金額</span>
<span th:text="${saving}"></span> <span th:text="${saving}"></span>
@@ -129,7 +136,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

@@ -1,5 +1,5 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html xmlns:th="http://www.thymeleaf.org">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>プロフィール</title> <title>プロフィール</title>

View File

@@ -0,0 +1,72 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>プロフィール編集</title>
</head>
<body class="pb-5 pt-5">
<div th:replace="~{header :: header}">
</div>
<main>
<h1>プロフィール編集</h1>
<form th:action="@{/profile/edit}"th:object="${profileEditForm}"method="post">
<div>
<label>氏名</label>
<input type="text"
th:field="*{userName}"
maxlength="100"
required>
</div>
<div>
<label>メールアドレス</label>
<input type="email"
th:field="*{mailAddress}"
maxlength="255"
required>
</div>
<div>
<label>パスワード</label>
<input type="password"
value="********"
disabled>
</div>
<div>
<label>貯蓄金</label>
<input type="number"
th:field="*{saving}"
min="0"
required>
</div>
<div>
<label>年収</label>
<input type="number"
th:field="*{annualIncome}"
min="0"
required>
</div>
<div>
<label>月別使用可能額</label>
<input type="number"
th:field="*{monthlyBudget}"
min="0"
required>
</div>
<div>
<button type="submit">確認</button>
</div>
</form>
</main>
<div th:replace="~{footer :: footer}">
</div>
</body>
</html>

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})}">