-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feat/#2 inquiry profile
- Loading branch information
Showing
10 changed files
with
228 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/main/java/com/sparta/oneandzerobest/profile/controller/ProfileController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.sparta.oneandzerobest.profile.controller; | ||
|
||
import com.sparta.oneandzerobest.profile.dto.ProfileRequestDto; | ||
import com.sparta.oneandzerobest.profile.dto.ProfileResponseDto; | ||
import com.sparta.oneandzerobest.profile.service.ProfileService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequestMapping("/profile") | ||
@RequiredArgsConstructor | ||
public class ProfileController { | ||
|
||
private final ProfileService profileService; | ||
|
||
/** | ||
* 선택한 프로필을 조회 | ||
* @param id : userId | ||
* @return : body로 유저의 정보 전달 | ||
*/ | ||
@GetMapping("/{id}") | ||
public ResponseEntity<ProfileResponseDto> inquiryProfile(@PathVariable Long id) { | ||
return ResponseEntity.status(200).body(profileService.inquiryProfile(id)); | ||
} | ||
|
||
/** | ||
* 프로필 수정 | ||
* @param id : userId | ||
* @param requestDto : 수정하고자하는 정보를 body로 받아옴 | ||
* @return : body로 유저의 정보 전달 | ||
*/ | ||
@PutMapping("/{id}") | ||
public ResponseEntity<ProfileResponseDto> editProfile(@PathVariable Long id, @RequestBody ProfileRequestDto requestDto) { | ||
return ResponseEntity.status(200).body(profileService.editProfile(id, requestDto)); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/sparta/oneandzerobest/profile/dto/ProfileRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.sparta.oneandzerobest.profile.dto; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class ProfileRequestDto { | ||
private String name; | ||
private String email; | ||
private String introduction; | ||
private String password; | ||
private String newPassword; | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/sparta/oneandzerobest/profile/dto/ProfileResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.sparta.oneandzerobest.profile.dto; | ||
|
||
import com.sparta.oneandzerobest.auth.entity.User; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class ProfileResponseDto { | ||
private String username; | ||
private String name; | ||
private String introduction; | ||
private String email; | ||
|
||
public ProfileResponseDto(User user) { | ||
this.username = user.getUsername(); | ||
this.name = user.getName(); | ||
this.introduction = user.getIntroduction(); | ||
this.email = user.getEmail(); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/com/sparta/oneandzerobest/profile/exception/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.sparta.oneandzerobest.profile.exception; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
|
||
@ControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
/** | ||
* 해당하는 유저를 찾을 수 없을 때, 클라이언트로 에러 코드와 메시지 반환 | ||
* @return | ||
*/ | ||
@ExceptionHandler(NotFoundUserException.class) | ||
public ResponseEntity<String> notFoundUserHandler() { | ||
return ResponseEntity.status(400).body("해당 유저를 찾을 수 없습니다."); | ||
} | ||
|
||
/** | ||
* 비밀번호가 맞지 않을 때, 클라이언트로 에러 코드와 메시지 반환 | ||
* @return | ||
*/ | ||
@ExceptionHandler(IncorrectPasswordException.class) | ||
public ResponseEntity<String> incorrectPasswordHandler() { | ||
return ResponseEntity.status(400).body("비밀번호가 맞지 않습니다."); | ||
} | ||
|
||
/** | ||
* 비밀번호가 형식이 맞지 않을 때, 클라이언트로 에러 코드와 메시지 반환 | ||
* @return | ||
*/ | ||
@ExceptionHandler(PasswordPatternException.class) | ||
public ResponseEntity<String> passwordPatternHandler() { | ||
return ResponseEntity.status(400).body("비밀번호는 최소 10자 이상이며 알파벳 대소문자, 숫자, 특수문자를 포함해야 합니다."); | ||
} | ||
|
||
/** | ||
* 비밀번호가 변경이 불가능 할 때, 클라이언트로 에러 코드와 메시지 반환 | ||
* @return | ||
*/ | ||
@ExceptionHandler(UnacceptablePasswordException.class) | ||
public ResponseEntity<String> unacceptablePasswordHandler() { | ||
return ResponseEntity.status(400).body("해당 비밀번호로 변경이 불가능합니다. (이전과 동일한 비밀번호로 변경할 수 없습니다.)"); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/sparta/oneandzerobest/profile/exception/IncorrectPasswordException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.sparta.oneandzerobest.profile.exception; | ||
|
||
public class IncorrectPasswordException extends RuntimeException { | ||
public IncorrectPasswordException() { | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/sparta/oneandzerobest/profile/exception/NotFoundUserException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.sparta.oneandzerobest.profile.exception; | ||
|
||
public class NotFoundUserException extends RuntimeException { | ||
public NotFoundUserException() { | ||
} | ||
|
||
public NotFoundUserException(String message) { | ||
super(message); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/sparta/oneandzerobest/profile/exception/PasswordPatternException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.sparta.oneandzerobest.profile.exception; | ||
|
||
public class PasswordPatternException extends RuntimeException { | ||
public PasswordPatternException() { | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/sparta/oneandzerobest/profile/exception/UnacceptablePasswordException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.sparta.oneandzerobest.profile.exception; | ||
|
||
public class UnacceptablePasswordException extends RuntimeException { | ||
public UnacceptablePasswordException() { | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/main/java/com/sparta/oneandzerobest/profile/service/ProfileService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.sparta.oneandzerobest.profile.service; | ||
|
||
import com.sparta.oneandzerobest.auth.entity.User; | ||
import com.sparta.oneandzerobest.auth.repository.UserRepository; | ||
import com.sparta.oneandzerobest.profile.dto.ProfileRequestDto; | ||
import com.sparta.oneandzerobest.profile.dto.ProfileResponseDto; | ||
import com.sparta.oneandzerobest.profile.exception.IncorrectPasswordException; | ||
import com.sparta.oneandzerobest.profile.exception.NotFoundUserException; | ||
import com.sparta.oneandzerobest.profile.exception.PasswordPatternException; | ||
import com.sparta.oneandzerobest.profile.exception.UnacceptablePasswordException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ProfileService { | ||
|
||
private final UserRepository userRepository; | ||
private final PasswordEncoder passwordEncoder; | ||
|
||
/** | ||
* 해당 id를 가진 user의 프로필을 조회해주는 메서드 | ||
* | ||
* @param id : userId | ||
* @return : ProfileResponseDto | ||
*/ | ||
public ProfileResponseDto inquiryProfile(Long id) { | ||
User user = userRepository.findById(id).orElseThrow( | ||
() -> new NotFoundUserException() | ||
); | ||
|
||
return new ProfileResponseDto(user); | ||
} | ||
|
||
/** | ||
* 프로필을 수정해주는 메서드 | ||
* | ||
* @param id : userId | ||
* @param requestDto : 수정 정보가 담긴 requestDto | ||
* @return : ProfileResponseDto | ||
*/ | ||
public ProfileResponseDto editProfile(Long id, ProfileRequestDto requestDto) { | ||
User user = userRepository.findById(id).orElseThrow( | ||
() -> new NotFoundUserException() | ||
); | ||
|
||
user.update(requestDto); // 이메일, 이름, 한줄소개 수정 | ||
|
||
if (requestDto.getPassword() != null && !requestDto.getPassword().isEmpty()) { // 비밀번호가 입력되어 있을때만 비밀번호 검사 시작 | ||
if (passwordEncoder.matches(requestDto.getPassword(), user.getPassword())) { // DB에 저장된 비밀번호와 일치하지 않는지 검사 | ||
validationPassword(requestDto, user.getPassword()); // 유효한 비밀번호로 변경하려는지 검사 | ||
user.updatePassword(passwordEncoder.encode(requestDto.getNewPassword())); // 비밀번호 수정 | ||
} else { | ||
throw new IncorrectPasswordException(); | ||
} | ||
} | ||
|
||
return new ProfileResponseDto(userRepository.save(user)); | ||
} | ||
|
||
/** | ||
* 유효한 비밀번호로 변경하려는지 검사하는 메서드 | ||
* | ||
* @param requestDto : 유저가 입력한 입력한 기존 비밀번호와 새 비밀번호 정보가 담겨있다. | ||
* @param userPassword : 원래 DB에 저장되어 있던 유저의 비밀번호 | ||
*/ | ||
private void validationPassword(ProfileRequestDto requestDto, String userPassword) { | ||
if (!requestDto.getNewPassword().matches("^(?=.*[a-zA-Z])(?=.*\\d)(?=.*[!@#$%^&*])[a-zA-Z\\d!@#$%^&*]{10,}$")) { // 비밀번호를 올바른 형식으로 바꾸려는 검사 | ||
if (passwordEncoder.matches(requestDto.getNewPassword(), userPassword)) { // 이전과 같은 비밀번호로 수정하는지 검사 | ||
throw new UnacceptablePasswordException(); | ||
} | ||
throw new PasswordPatternException(); | ||
} | ||
} | ||
} |