diff --git a/src/main/java/com/example/demo/app/domain/service/AnalysisService.java b/src/main/java/com/example/demo/app/domain/service/AnalysisService.java index 9bcc70c..1f6b433 100644 --- a/src/main/java/com/example/demo/app/domain/service/AnalysisService.java +++ b/src/main/java/com/example/demo/app/domain/service/AnalysisService.java @@ -4,6 +4,9 @@ import com.example.demo.app.domain.model.AnalysisModel; public interface AnalysisService { - AnalysisModel getAnalysis(Long userId); + AnalysisModel getAnalysis( + Long userId, + Integer year, + Integer month); } diff --git a/src/main/java/com/example/demo/app/domain/service/ProfileService.java b/src/main/java/com/example/demo/app/domain/service/ProfileService.java index 19441ab..a913f7a 100644 --- a/src/main/java/com/example/demo/app/domain/service/ProfileService.java +++ b/src/main/java/com/example/demo/app/domain/service/ProfileService.java @@ -1,8 +1,10 @@ package com.example.demo.app.domain.service; import com.example.demo.app.domain.model.ProfileModel; +import com.example.demo.app.presentation.form.ProfileEditForm; public interface ProfileService { ProfileModel findOne(Long userId); void withdraw(Long userId); + void update(Long userId, ProfileEditForm profileEditForm); } diff --git a/src/main/java/com/example/demo/app/domain/service/impl/AnalysisServiceImpl.java b/src/main/java/com/example/demo/app/domain/service/impl/AnalysisServiceImpl.java index 0764d41..b0caf13 100644 --- a/src/main/java/com/example/demo/app/domain/service/impl/AnalysisServiceImpl.java +++ b/src/main/java/com/example/demo/app/domain/service/impl/AnalysisServiceImpl.java @@ -30,7 +30,7 @@ public class AnalysisServiceImpl implements AnalysisService { private final CategoryRepository categoryRepository; @Override - public AnalysisModel getAnalysis(Long userId) { + public AnalysisModel getAnalysis(Long userId, Integer year, Integer month) { // ユーザー情報取得、月別使用可能資金取得 UserEntity userEntity = userRepository.findById(userId).orElseThrow(() -> @@ -44,12 +44,15 @@ public class AnalysisServiceImpl implements AnalysisService { Integer monthlyBudget =userEntity.getMonthlyBudget(); // 今月の期間取得 - LocalDate currentMonthStartDate = LocalDate.now().withDayOfMonth(1); - LocalDate currentMonthEndDate = LocalDate.now().withDayOfMonth(LocalDate.now().lengthOfMonth()); + LocalDate targetDate = LocalDate.of(year, month, 1); + LocalDate currentMonthStartDate = targetDate.withDayOfMonth(1); + LocalDate currentMonthEndDate = targetDate.withDayOfMonth(targetDate.lengthOfMonth()); // 前月の期間取得 - LocalDate previousMonthStartDate = LocalDate.now().minusMonths(1).withDayOfMonth(1); - LocalDate previousMonthEndDate = LocalDate.now().minusMonths(1).withDayOfMonth(LocalDate.now().minusMonths(1).lengthOfMonth()); + LocalDate previousDate = targetDate.minusMonths(1); + + LocalDate previousMonthStartDate = previousDate.withDayOfMonth(1); + LocalDate previousMonthEndDate = previousDate.withDayOfMonth(previousDate.lengthOfMonth()); // 今月の支出取得 Integer currentMonthTotalAmount = transactionRepository.findTotalExpense(currentMonthStartDate, currentMonthEndDate, userId); diff --git a/src/main/java/com/example/demo/app/domain/service/impl/PasswordResetServiceImpl.java b/src/main/java/com/example/demo/app/domain/service/impl/PasswordResetServiceImpl.java new file mode 100644 index 0000000..11c7069 --- /dev/null +++ b/src/main/java/com/example/demo/app/domain/service/impl/PasswordResetServiceImpl.java @@ -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; + } +} \ No newline at end of file diff --git a/src/main/java/com/example/demo/app/domain/service/impl/ProfileServiceImpl.java b/src/main/java/com/example/demo/app/domain/service/impl/ProfileServiceImpl.java index f9fdbd7..c356dc2 100644 --- a/src/main/java/com/example/demo/app/domain/service/impl/ProfileServiceImpl.java +++ b/src/main/java/com/example/demo/app/domain/service/impl/ProfileServiceImpl.java @@ -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.infra.entity.UserEntity; import com.example.demo.app.infra.repository.UserRepository; +import com.example.demo.app.presentation.form.ProfileEditForm; import lombok.RequiredArgsConstructor; @@ -34,7 +35,7 @@ public class ProfileServiceImpl implements ProfileService{ @Override public void withdraw(Long userId) { - UserEntity userEntity =userRepository.findById(userId).orElse(null); + UserEntity userEntity = userRepository.findById(userId).orElse(null); if (userEntity == null) { return; @@ -43,4 +44,20 @@ public class ProfileServiceImpl implements ProfileService{ userEntity.setDeleteFlag(1); 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); + } } diff --git a/src/main/java/com/example/demo/app/presentation/controller/AnalysisController.java b/src/main/java/com/example/demo/app/presentation/controller/AnalysisController.java index 7526181..b64f7ae 100644 --- a/src/main/java/com/example/demo/app/presentation/controller/AnalysisController.java +++ b/src/main/java/com/example/demo/app/presentation/controller/AnalysisController.java @@ -1,10 +1,13 @@ package com.example.demo.app.presentation.controller; +import java.time.LocalDate; + 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.RequestParam; import com.example.demo.aop.aspect.SessionCheck; import com.example.demo.app.domain.model.AnalysisModel; @@ -23,18 +26,45 @@ public class AnalysisController { @SessionCheck @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"); + //初期年月表示 + 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) - AnalysisModel analysisModel = analysisService.getAnalysis(loginUser.getUserId()); + AnalysisModel analysisModel = analysisService.getAnalysis(loginUser.getUserId(), year, month); AnalysisForm analysisForm = AnalysisForm.convertFrom(analysisModel); //model格納 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"; } diff --git a/src/main/java/com/example/demo/app/presentation/controller/PasswordResetController.java b/src/main/java/com/example/demo/app/presentation/controller/PasswordResetController.java new file mode 100644 index 0000000..53d6699 --- /dev/null +++ b/src/main/java/com/example/demo/app/presentation/controller/PasswordResetController.java @@ -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"; + } +} + + diff --git a/src/main/java/com/example/demo/app/presentation/controller/ProfileController.java b/src/main/java/com/example/demo/app/presentation/controller/ProfileController.java index 807cf79..ef10a10 100644 --- a/src/main/java/com/example/demo/app/presentation/controller/ProfileController.java +++ b/src/main/java/com/example/demo/app/presentation/controller/ProfileController.java @@ -5,12 +5,14 @@ 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.ModelAttribute; 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.ProfileModel; 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 lombok.RequiredArgsConstructor; @@ -35,10 +37,43 @@ public class ProfileController { @SessionCheck @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"; } + @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") public String withdraw(HttpSession session) { diff --git a/src/main/java/com/example/demo/app/presentation/form/ProfileEditForm.java b/src/main/java/com/example/demo/app/presentation/form/ProfileEditForm.java new file mode 100644 index 0000000..78d3e81 --- /dev/null +++ b/src/main/java/com/example/demo/app/presentation/form/ProfileEditForm.java @@ -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; +} diff --git a/src/main/resources/templates/analysis.html b/src/main/resources/templates/analysis.html index 82ad4f8..a44b40a 100644 --- a/src/main/resources/templates/analysis.html +++ b/src/main/resources/templates/analysis.html @@ -5,6 +5,7 @@ 分析画面 +
@@ -13,6 +14,23 @@

支出分析

+
+ + + ← + + +
+ + +
+ + + → + + +
+ diff --git a/src/main/resources/templates/profile.html b/src/main/resources/templates/profile.html index 54f12f2..0fe082b 100644 --- a/src/main/resources/templates/profile.html +++ b/src/main/resources/templates/profile.html @@ -1,5 +1,5 @@ - +プロフィール diff --git a/src/main/resources/templates/profile_edit.html b/src/main/resources/templates/profile_edit.html new file mode 100644 index 0000000..d82c709 --- /dev/null +++ b/src/main/resources/templates/profile_edit.html @@ -0,0 +1,72 @@ + + + + +プロフィール編集 + + + +
+
+ +
+

プロフィール編集

+ +
+
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ +
+ +
+ +
+
+ + \ No newline at end of file