Skip to content

Commit

Permalink
[FEAT]#14 회원 정보 수정 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
LEEJaeHyeok97 committed Aug 29, 2023
1 parent c1aa147 commit 5f9e927
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.example.rcp1.domain.user.domain.repository.UserRepository;
import com.example.rcp1.domain.user.dto.SignInReq;
import com.example.rcp1.domain.user.dto.#Req;
import com.example.rcp1.domain.user.dto.UpdateProfileReq;
import com.example.rcp1.global.CustomAuthenticationException;
import com.example.rcp1.global.config.security.util.JwtUtil;
import io.jsonwebtoken.Jwt;
Expand Down Expand Up @@ -89,4 +90,59 @@ public String deleteUser(String token) {

return "";
}


public User updateProfile(String access_token, UpdateProfileReq updateProfileReq) {

try {
String email = JwtUtil.getUserEmail(access_token, secret_key);

Optional<User> user = userRepository.findByEmail(email);

if (user.isPresent()) {
User userRes = user.get();

if (updateProfileReq.getName() != null) {
userRes.setName(updateProfileReq.getName());
}

if (updateProfileReq.getPhoneNumber() != null) {
userRes.setPhoneNumber(updateProfileReq.getPhoneNumber());
}

if (updateProfileReq.getSpecializedField() != null) {
userRes.setSpecializedField(updateProfileReq.getSpecializedField());
}

if (updateProfileReq.getCareer() != null) {
userRes.setCareer(updateProfileReq.getCareer());
}

if (updateProfileReq.getPosition() != null) {
userRes.setPosition(updateProfileReq.getPosition());
}

if (updateProfileReq.getSchool() != null) {
userRes.setSchool(updateProfileReq.getSchool());
}

if (updateProfileReq.getJob() != null) {
userRes.setJob(updateProfileReq.getJob());
}

userRepository.save(userRes);
return userRes;

} else {
return null;
}
} catch (Exception e) {
throw new CustomAuthenticationException("유저 정보 수정에 실패했습니다.");
}


}



}
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,21 @@ public ResponseEntity<String> writeReview2(@RequestHeader("Authorization") Strin

// 유저 정보 수정
@PatchMapping("/profile")
public ResponseEntity<String> updateProfile(
public ResponseEntity<BaseResponse<?>> updateProfile(
@RequestHeader("Authorization") String Authorization, // 헤더에서 Authorization 값을 받아온다
@Valid @RequestBody UpdateProfileReq updateProfileReq) {
try {
String access_token = Authorization.substring(7); // Bearer 이후 토큰만 파싱
System.out.println("access_token = " + access_token);
System.out.println("test 성공!!!!");

// 토큰에서 이메일 파싱 후 이메일이랑 updateprofilereq 객체랑 같이 서비스에 보낸 후 수정처리 하는 코드
User user = userService.updateProfile(access_token, updateProfileReq);

return ResponseEntity.ok().body("성공");

return ResponseEntity.ok(BaseResponse.success(SuccessCode.UPDATE_PROFILE_SUCCESS, user));

} catch (Exception e) {
return ResponseEntity.ok().body("실패");
// return ResponseEntity.status(HttpStatus.BAD_REQUEST)
// .body(BaseResponse.error(ErrorCode.REQUEST_VALIDATION_EXCEPTION, "유저 정보 수정에 실패했습니다."));
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(BaseResponse.error(ErrorCode.REQUEST_VALIDATION_EXCEPTION, "유저 정보 수정에 실패했습니다."));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.csrf().disable()
.cors().and()
.authorizeHttpRequests()
.requestMatchers("/user/#", "/user/signIn", "/user/delete").permitAll()
.requestMatchers("/user/#", "/user/signIn", "/user/delete", "/user/profile").permitAll()
.requestMatchers(HttpMethod.POST, "/user/**").authenticated()
.and()
.sessionManagement()
Expand Down

0 comments on commit 5f9e927

Please # to comment.