Skip to content

Commit

Permalink
Merge pull request #18 from RecruitUs/feat/#14-userUpdateDelete
Browse files Browse the repository at this point in the history
Feat/#14 user update delete
  • Loading branch information
LEEJaeHyeok97 authored Aug 29, 2023
2 parents 2215354 + 5f9e927 commit 79fb674
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
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;
import lombok.RequiredArgsConstructor;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -72,4 +74,75 @@ public String signIn(SignInReq signInReq) {
}


// 유저 정보 논리 삭제
public String deleteUser(String token) {


String subtractedEmail = JwtUtil.getUserEmail(token, secret_key);

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

User tmpUser = user.get();

tmpUser.setStatusD();

userRepository.save(tmpUser);

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("유저 정보 수정에 실패했습니다.");
}


}



}
5 changes: 5 additions & 0 deletions src/main/java/com/example/rcp1/domain/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,9 @@ public User(Long id, String email, String password, String name, String phoneNum
}


// 논리 삭제 상태 수정
public void setStatusD() {
this.status = "D";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.example.rcp1.domain.user.dto;

import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;

import java.time.LocalDateTime;

@Data
public class UpdateProfileReq {

@Email
private String email;

private String password;

private String name;

private String phoneNumber;


private String specializedField;

private Long career;

private String position;

private String school;

private String job;


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.example.rcp1.domain.user.domain.User;
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.BaseResponse;
import com.example.rcp1.global.CustomAuthenticationException;
import com.example.rcp1.global.ErrorCode;
Expand Down Expand Up @@ -42,7 +43,6 @@ public ResponseEntity<BaseResponse<User>> #(@Valid @RequestBody #Req s
public ResponseEntity<BaseResponse<String>> signIn(@Valid @RequestBody SignInReq signInReq) {
try {
String token = userService.signIn(signInReq);
System.out.println("token = " + token);

if (token != null) {
return ResponseEntity.ok(BaseResponse.success(SuccessCode.SIGNIN_SUCCESS, token));
Expand All @@ -68,8 +68,49 @@ public ResponseEntity<String> writeReview(Authentication authentication) {
return ResponseEntity.ok().body(authentication.getName() + "님의 글작성이 완료되었습니다.");
}

@PostMapping("/write2")
public ResponseEntity<String> writeReview2(@RequestHeader("Authorization") String Authorization) {
return ResponseEntity.ok().body(Authorization + "님의 글작성이 완료되었습니다.");
}


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

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


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

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

}


// 유저 정보 탈퇴(논리 삭제)
@PatchMapping("/delete")
public ResponseEntity<BaseResponse<?>> deleteUser(
@RequestHeader("Authorization") String authorization
) {

try {
String token = authorization.substring(7);
String t = userService.deleteUser(token);

return ResponseEntity.ok(BaseResponse.success(SuccessCode.LOGICAL_DELETE_SUCCESS));
} catch (Exception e) {
return ResponseEntity.ok().body(BaseResponse.error(ErrorCode.EXPIRED_TOKEN));
}
}


}
4 changes: 3 additions & 1 deletion src/main/java/com/example/rcp1/global/SuccessCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public enum SuccessCode {
// CUSTOM_SUCCESS(OK, "~ 조회에 성공했습니다."),
// CUSTOM_CREATED_SUCCESS(CREATED, "~ 생성에 성공했습니다.");
#_SUCCESS(OK, "회원가입에 성공했습니다."),
SIGNIN_SUCCESS(OK, "로그인에 성공했습니다.");
SIGNIN_SUCCESS(OK, "로그인에 성공했습니다."),
UPDATE_PROFILE_SUCCESS(OK, "프로필이 성공적으로 수정되었습니다."),
LOGICAL_DELETE_SUCCESS(OK, "논리적으로 삭제 되었습니다.");

private final HttpStatus httpStatus;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse

// 토큰 만료 여부 확인
if (JwtUtil.isExpired(token, secretKey)) {
log.error("토큰이 만료되었습니다.");
log.error("유효하지 않은 액세스 토큰입니다.");
filterChain.doFilter(request, response);
return;
}
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").permitAll()
.requestMatchers("/user/#", "/user/signIn", "/user/delete", "/user/profile").permitAll()
.requestMatchers(HttpMethod.POST, "/user/**").authenticated()
.and()
.sessionManagement()
Expand Down

0 comments on commit 79fb674

Please # to comment.