diff --git a/backend/src/main/java/com/edufocus/edufocus/user/controller/UserController.java b/backend/src/main/java/com/edufocus/edufocus/user/controller/UserController.java index 5b36459..7ca0bf4 100644 --- a/backend/src/main/java/com/edufocus/edufocus/user/controller/UserController.java +++ b/backend/src/main/java/com/edufocus/edufocus/user/controller/UserController.java @@ -1,5 +1,7 @@ package com.edufocus.edufocus.user.controller; +import com.edufocus.edufocus.user.model.entity.InfoDto; +import com.edufocus.edufocus.user.model.entity.PasswordDto; import com.edufocus.edufocus.user.model.entity.User; import com.edufocus.edufocus.user.model.service.UserService; import com.edufocus.edufocus.user.util.JWTUtil; @@ -42,7 +44,30 @@ public class UserController { } + @PutMapping("/updateinfo/{id}") + public ResponseEntity updateUserInfo( + @PathVariable("id") Long id, + @RequestBody InfoDto infoDto) { + try { + userService.changeuInfo(infoDto, id); + return ResponseEntity.ok("User info updated successfully"); + } catch (Exception e) { + return ResponseEntity.badRequest().body(e.getMessage()); + } + } + // 비밀번호 변경 + @PutMapping("/updatepassword/{id}") + public ResponseEntity updatePassword( + @PathVariable("id") Long id, + @RequestBody PasswordDto passwordDto) { + try { + userService.changePassword(passwordDto, id); + return ResponseEntity.ok("Password changed successfully"); + } catch (Exception e) { + return ResponseEntity.badRequest().body(e.getMessage()); + } + } @Operation(summary = "로그인", description = "아이디와 비밀번호를 이용하여 로그인 처리.") @PostMapping("/login") public ResponseEntity> login( diff --git a/backend/src/main/java/com/edufocus/edufocus/user/model/entity/InfoDto.java b/backend/src/main/java/com/edufocus/edufocus/user/model/entity/InfoDto.java index ed96b9c..52b1f08 100644 --- a/backend/src/main/java/com/edufocus/edufocus/user/model/entity/InfoDto.java +++ b/backend/src/main/java/com/edufocus/edufocus/user/model/entity/InfoDto.java @@ -1,8 +1,15 @@ package com.edufocus.edufocus.user.model.entity; +import lombok.Getter; +import lombok.Setter; + +@Setter +@Getter + public class InfoDto { String name; String email; + } diff --git a/backend/src/main/java/com/edufocus/edufocus/user/model/entity/PasswordDto.java b/backend/src/main/java/com/edufocus/edufocus/user/model/entity/PasswordDto.java new file mode 100644 index 0000000..a77f73e --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/user/model/entity/PasswordDto.java @@ -0,0 +1,14 @@ +package com.edufocus.edufocus.user.model.entity; + +import lombok.Generated; +import lombok.Getter; +import lombok.Setter; + +@Setter +@Getter +public class PasswordDto { + + String currentPassword; + String newPassword; + String newPasswordCheck; +} diff --git a/backend/src/main/java/com/edufocus/edufocus/user/model/service/UserService.java b/backend/src/main/java/com/edufocus/edufocus/user/model/service/UserService.java index 2f63135..5e1ba53 100644 --- a/backend/src/main/java/com/edufocus/edufocus/user/model/service/UserService.java +++ b/backend/src/main/java/com/edufocus/edufocus/user/model/service/UserService.java @@ -1,5 +1,7 @@ package com.edufocus.edufocus.user.model.service; +import com.edufocus.edufocus.user.model.entity.InfoDto; +import com.edufocus.edufocus.user.model.entity.PasswordDto; import com.edufocus.edufocus.user.model.entity.User; public interface UserService { @@ -12,7 +14,7 @@ public interface UserService { void sendEamail(User user) throws Exception; void userCheck(Long id) throws Exception; String getUserName(Long id) throws Exception; - void changeuInfo(Long id) throws Exception; - void changePassword(Long id) throws Exception; + void changeuInfo(InfoDto infoDto,Long id) throws Exception; + void changePassword(PasswordDto passwordDto,Long id) throws Exception; } diff --git a/backend/src/main/java/com/edufocus/edufocus/user/model/service/UserServiceImpl.java b/backend/src/main/java/com/edufocus/edufocus/user/model/service/UserServiceImpl.java index 66bb873..ed0f07d 100644 --- a/backend/src/main/java/com/edufocus/edufocus/user/model/service/UserServiceImpl.java +++ b/backend/src/main/java/com/edufocus/edufocus/user/model/service/UserServiceImpl.java @@ -1,9 +1,7 @@ package com.edufocus.edufocus.user.model.service; -import com.edufocus.edufocus.user.model.entity.MailDto; -import com.edufocus.edufocus.user.model.entity.MemberChangeDto; -import com.edufocus.edufocus.user.model.entity.User; +import com.edufocus.edufocus.user.model.entity.*; import com.edufocus.edufocus.user.model.exception.UserException; import com.edufocus.edufocus.user.model.repository.UserRepository; import jakarta.transaction.Transactional; @@ -21,46 +19,38 @@ import java.util.Optional; @Service @Transactional @RequiredArgsConstructor -public class UserServiceImpl implements UserService{ +public class UserServiceImpl implements UserService { private final UserRepository userRepository; private final JavaMailSender mailSender; - - public void join(User user) - { + public void join(User user) { userRepository.save(user); } - public User login(User user) throws SQLException - { + public User login(User user) throws SQLException { Optional findUser = userRepository.findByUserId(user.getUserId()); - if(findUser.isEmpty()) - { + if (findUser.isEmpty()) { throw new UserException("없는 유저"); } - if(findUser.isPresent()) - { + if (findUser.isPresent()) { User find = findUser.get(); - if(find.getPassword().equals(user.getPassword())) - { + if (find.getPassword().equals(user.getPassword())) { return find; - } - else{ + } else { throw new UserException("비밀번호 틀림"); } - } - else{ + } else { throw new UserException("없는 유저"); @@ -69,13 +59,10 @@ public class UserServiceImpl implements UserService{ } @Override - public User userInfo(Long id) - { - try{ + public User userInfo(Long id) { + try { return userRepository.findById(id).get(); - } - catch (Exception e) - { + } catch (Exception e) { throw new UserException(e.getMessage()); } @@ -89,12 +76,11 @@ public class UserServiceImpl implements UserService{ SimpleMailMessage message = new SimpleMailMessage(); - message.setTo(mailDto.getAddress()); message.setFrom("passfinder111@gmail.com"); message.setSubject(mailDto.getTitle()); message.setText(mailDto.getMessage()); - System.out.println("!!!!!!!!!!!!!!!!!!"+ message); + System.out.println("!!!!!!!!!!!!!!!!!!" + message); mailSender.send(message); @@ -105,15 +91,15 @@ public class UserServiceImpl implements UserService{ String str = getTempPassword(); MailDto dto = new MailDto(); dto.setAddress(user.getEmail()); - dto.setTitle(user.getUserId()+"님의 임시비밀번호 안내 이메일 입니다."); - dto.setMessage("안녕하세요. EduFoucs 입니다. "+ "\n"+ "임시비밀번호 안내 관련 메일 입니다." + "\n[" + user.getName() + "]" + "님의 임시 비밀번호는 " + dto.setTitle(user.getUserId() + "님의 임시비밀번호 안내 이메일 입니다."); + dto.setMessage("안녕하세요. EduFoucs 입니다. " + "\n" + "임시비밀번호 안내 관련 메일 입니다." + "\n[" + user.getName() + "]" + "님의 임시 비밀번호는 " + str + " 입니다."); System.out.println(dto); - MemberChangeDto memberChangeDto = new MemberChangeDto(user.getId(),str); + MemberChangeDto memberChangeDto = new MemberChangeDto(user.getId(), str); System.out.println(memberChangeDto); - userRepository.updatePassword(memberChangeDto.getId(),memberChangeDto.getPassword()); + userRepository.updatePassword(memberChangeDto.getId(), memberChangeDto.getPassword()); System.out.println(); return dto; @@ -125,13 +111,11 @@ public class UserServiceImpl implements UserService{ User user = userRepository.findById(id).orElse(null); - if(user == null) - { + if (user == null) { System.out.println("불가"); throw new RuntimeException("유효하지 않은 아이디입니다. 다시 입력하세요"); - } - else { + } else { sendEamail(user); } @@ -143,16 +127,48 @@ public class UserServiceImpl implements UserService{ return userRepository.findById(id).get().getName(); } - @Override - public void changeuInfo(Long id) throws Exception { - - } @Override - public void changePassword(Long id) throws Exception { + public void changeuInfo(InfoDto infoDto, Long id) throws Exception { + User user = userRepository.findById(id).orElse(null); + + if (user == null) { + throw new Exception("User not found"); + } + + if (infoDto.getName() != null) + { + user.setName(infoDto.getName()); + } + + if(infoDto.getEmail()!=null) + { + user.setEmail(infoDto.getEmail()); + } + userRepository.save(user); +} + + + @Override + public void changePassword(PasswordDto passwordDto, Long id) throws Exception { + User user = userRepository.findById(id).orElse(null); + + if (user == null) { + throw new Exception("User not found"); + } + + if (!user.getPassword().equals(passwordDto.getCurrentPassword())) { + throw new Exception("Current password is incorrect"); + } else { + if (!passwordDto.getNewPassword().equals(passwordDto.getNewPasswordCheck())) { + throw new Exception("New password confirmation does not match"); + } + } + + user.setPassword(passwordDto.getNewPassword()); + userRepository.save(user); } - public String getTempPassword() { char[] charSet = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};