Skip to content

Commit

Permalink
feat: 로그인 회원 id 정보 가져오는 애노테이션 적용 (#41) (KAN-96)
Browse files Browse the repository at this point in the history
  • Loading branch information
ywonchae1 committed Oct 30, 2024
1 parent 74bd848 commit 824c41b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.ioteatime.meonghanyangserver.common.api.Api;
import org.ioteatime.meonghanyangserver.common.utils.LoginMember;
import org.ioteatime.meonghanyangserver.user.dto.request.ChangePasswordRequest;
import org.ioteatime.meonghanyangserver.user.dto.response.UserDetailResponse;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

Expand All @@ -17,9 +17,9 @@ public interface UserApi {
Api<UserDetailResponse> getUserDetail(@PathVariable("userId") Long userId);

@Operation(summary = "회원 정보를 삭제합니다.")
Api<Object> deleteUser(@PathVariable("userId") Long userId);
Api<Object> deleteUser(@LoginMember Long userId);

@Operation(summary = "회원의 비밀번호를 변경합니다.")
Api<Object> changeUserPassword(
Authentication authentication, @RequestBody @Valid ChangePasswordRequest request);
@LoginMember Long userId, @RequestBody @Valid ChangePasswordRequest request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.ioteatime.meonghanyangserver.common.api.Api;
import org.ioteatime.meonghanyangserver.common.utils.LoginMember;
import org.ioteatime.meonghanyangserver.user.dto.request.ChangePasswordRequest;
import org.ioteatime.meonghanyangserver.user.dto.response.UserDetailResponse;
import org.ioteatime.meonghanyangserver.user.service.UserService;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

@RestController
Expand All @@ -16,23 +16,21 @@ public class UserController implements UserApi {
private final UserService userService;

@GetMapping("/{userId}")
public Api<UserDetailResponse> getUserDetail(Long userId) {
public Api<UserDetailResponse> getUserDetail(@PathVariable("userId") Long userId) {
UserDetailResponse userDto = userService.getUserDetail(userId);
return Api.OK(userDto);
}

@DeleteMapping("/{userId}")
public Api<Object> deleteUser(Long userId) {
@DeleteMapping
public Api<Object> deleteUser(@LoginMember Long userId) {
userService.deleteUser(userId);
return Api.OK();
}

@PutMapping("/password")
public Api<Object> changeUserPassword(
Authentication authentication, @RequestBody @Valid ChangePasswordRequest request) {

userService.changeUserPassword(authentication, request);

@LoginMember Long userId, @RequestBody @Valid ChangePasswordRequest request) {
userService.changeUserPassword(userId, request);
return Api.OK();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
import org.ioteatime.meonghanyangserver.common.error.ErrorTypeCode;
import org.ioteatime.meonghanyangserver.common.exception.ApiException;
import org.ioteatime.meonghanyangserver.user.domain.UserEntity;
import org.ioteatime.meonghanyangserver.user.dto.CustomUserDetail;
import org.ioteatime.meonghanyangserver.user.dto.request.ChangePasswordRequest;
import org.ioteatime.meonghanyangserver.user.dto.response.UserDetailResponse;
import org.ioteatime.meonghanyangserver.user.repository.UserRepository;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -34,11 +32,9 @@ public void deleteUser(Long userId) {
}

@Transactional
public void changeUserPassword(Authentication authentication, ChangePasswordRequest request) {
public void changeUserPassword(Long userId, ChangePasswordRequest request) {
String currentPassword = request.getCurrentPassword();
String newPassword = request.getNewPassword();
CustomUserDetail userDetails = (CustomUserDetail) authentication.getPrincipal();
Long userId = userDetails.getUserEntity().getId();

UserEntity userEntity =
userRepository
Expand Down

0 comments on commit 824c41b

Please sign in to comment.