Skip to content

Commit

Permalink
feat: 닉네임으로 유저 조회
Browse files Browse the repository at this point in the history
  • Loading branch information
kanguk01 committed Oct 31, 2024
1 parent c00000d commit b9c2407
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/main/java/com/splanet/splanet/user/controller/UserApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,15 @@ ResponseEntity<UserResponseDto> 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<UserResponseDto> getUserByNickname(
@Parameter(description = "검색할 유저의 닉네임", required = true) @PathVariable String user_nickname);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -37,4 +38,10 @@ public ResponseEntity<UserResponseDto> createUser(String nickname, String profil
UserResponseDto newUser = userService.createUser(nickname, profileImage, isPremium);
return ResponseEntity.status(201).body(newUser);
}

@Override
public ResponseEntity<UserResponseDto> getUserByNickname(@PathVariable("user_nickname") String userNickname) {
UserResponseDto userResponse = userService.getUserByNickname(userNickname);
return ResponseEntity.ok(userResponse);
}
}
14 changes: 11 additions & 3 deletions src/main/java/com/splanet/splanet/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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())
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
}

0 comments on commit b9c2407

Please sign in to comment.