[ADD]プロフィール編集の作成
This commit is contained in:
@@ -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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -5,11 +5,13 @@ 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.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;
|
||||||
@@ -32,10 +34,43 @@ public class ProfileController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@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) {
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -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>
|
||||||
|
|||||||
72
src/main/resources/templates/profile_edit.html
Normal file
72
src/main/resources/templates/profile_edit.html
Normal 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>
|
||||||
Reference in New Issue
Block a user