From 1d010dc02b964961439d21b1863590f9f2d04284 Mon Sep 17 00:00:00 2001 From: s-morimoto Date: Fri, 18 Sep 2026 16:01:33 +0900 Subject: [PATCH] =?UTF-8?q?[ADD]=E3=83=97=E3=83=AD=E3=83=95=E3=82=A3?= =?UTF-8?q?=E3=83=BC=E3=83=AB=E7=B7=A8=E9=9B=86=E3=81=AE=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/domain/service/ProfileService.java | 2 + .../impl/PasswordResetServiceImpl.java | 42 +++++++++++ .../service/impl/ProfileServiceImpl.java | 19 ++++- .../controller/PasswordResetController.java | 53 ++++++++++++++ .../controller/ProfileController.java | 37 +++++++++- .../presentation/form/ProfileEditForm.java | 16 +++++ src/main/resources/templates/profile.html | 2 +- .../resources/templates/profile_edit.html | 72 +++++++++++++++++++ 8 files changed, 240 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/example/demo/app/domain/service/impl/PasswordResetServiceImpl.java create mode 100644 src/main/java/com/example/demo/app/presentation/controller/PasswordResetController.java create mode 100644 src/main/java/com/example/demo/app/presentation/form/ProfileEditForm.java create mode 100644 src/main/resources/templates/profile_edit.html 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/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/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 3829d49..8b78fac 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,11 +5,13 @@ 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.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; @@ -32,10 +34,43 @@ public class ProfileController { } @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/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