From b9c2407c1c448e5a2170cc103f730819e4b8ec1b Mon Sep 17 00:00:00 2001 From: kanguk Date: Thu, 31 Oct 2024 16:16:23 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=8B=89=EB=84=A4=EC=9E=84=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EC=9C=A0=EC=A0=80=20=EC=A1=B0=ED=9A=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../splanet/splanet/user/controller/UserApi.java | 11 +++++++++++ .../splanet/user/controller/UserController.java | 7 +++++++ .../splanet/splanet/user/service/UserService.java | 14 +++++++++++--- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/splanet/splanet/user/controller/UserApi.java b/src/main/java/com/splanet/splanet/user/controller/UserApi.java index cfcb47f9..b2699adb 100644 --- a/src/main/java/com/splanet/splanet/user/controller/UserApi.java +++ b/src/main/java/com/splanet/splanet/user/controller/UserApi.java @@ -63,4 +63,15 @@ ResponseEntity createUser( @Parameter(description = "새로운 유저의 닉네임", required = true) @RequestParam String nickname, @Parameter(description = "새로운 유저의 프로필 이미지", required = false) @RequestParam(required = false) String profileImage, @Parameter(description = "새로운 유저의 프리미엄 여부", required = false) @RequestParam(required = false) Boolean isPremium); + + @GetMapping("/nickname/{user_nickname}") + @Operation(summary = "닉네임으로 유저 조회", description = "특정 사용자의 닉네임으로 유저 정보를 조회합니다.") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "유저 정보가 성공적으로 반환되었습니다.", + content = @Content(schema = @Schema(implementation = UserResponseDto.class))), + @ApiResponse(responseCode = "404", description = "존재하지 않는 유저입니다.", content = @Content), + @ApiResponse(responseCode = "500", description = "서버 오류.", content = @Content) + }) + ResponseEntity getUserByNickname( + @Parameter(description = "검색할 유저의 닉네임", required = true) @PathVariable String user_nickname); } diff --git a/src/main/java/com/splanet/splanet/user/controller/UserController.java b/src/main/java/com/splanet/splanet/user/controller/UserController.java index bfcd49c9..124e60c0 100644 --- a/src/main/java/com/splanet/splanet/user/controller/UserController.java +++ b/src/main/java/com/splanet/splanet/user/controller/UserController.java @@ -6,6 +6,7 @@ import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController @@ -37,4 +38,10 @@ public ResponseEntity createUser(String nickname, String profil UserResponseDto newUser = userService.createUser(nickname, profileImage, isPremium); return ResponseEntity.status(201).body(newUser); } + + @Override + public ResponseEntity getUserByNickname(@PathVariable("user_nickname") String userNickname) { + UserResponseDto userResponse = userService.getUserByNickname(userNickname); + return ResponseEntity.ok(userResponse); + } } diff --git a/src/main/java/com/splanet/splanet/user/service/UserService.java b/src/main/java/com/splanet/splanet/user/service/UserService.java index b7f709e0..e115bb6d 100644 --- a/src/main/java/com/splanet/splanet/user/service/UserService.java +++ b/src/main/java/com/splanet/splanet/user/service/UserService.java @@ -11,6 +11,8 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import static com.splanet.splanet.core.exception.ErrorCode.USER_NOT_FOUND; + @Service @RequiredArgsConstructor public class UserService { @@ -19,14 +21,14 @@ public class UserService { public UserResponseDto getUserById(Long userId) { User user = userRepository.findById(userId) - .orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND)); + .orElseThrow(() -> new BusinessException(USER_NOT_FOUND)); return toUserResponseDto(user); } @Transactional public UserResponseDto updateUserInfo(Long userId, UserUpdateRequestDto requestDto) { User user = userRepository.findById(userId) - .orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND)); + .orElseThrow(() -> new BusinessException(USER_NOT_FOUND)); User updatedUser = user.toBuilder() .nickname(requestDto.getNickname()) @@ -42,7 +44,7 @@ public UserResponseDto updateUserInfo(Long userId, UserUpdateRequestDto requestD @Transactional public void deleteUser(Long userId) { User user = userRepository.findById(userId) - .orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND)); + .orElseThrow(() -> new BusinessException(USER_NOT_FOUND)); userRepository.delete(user); } @@ -77,4 +79,10 @@ private UserResponseDto toUserResponseDto(User user) { .updatedAt(user.getUpdatedAt()) .build(); } + + public UserResponseDto getUserByNickname(String nickname) { + User user = userRepository.findByNickname(nickname) + .orElseThrow(() -> new BusinessException(USER_NOT_FOUND)); + return toUserResponseDto(user); + } }