Skip to content

Commit 1ca5a1b

Browse files
committed
Feat: 회원 정보 조회 API 구현
1 parent 2cc684e commit 1ca5a1b

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.back.domain.user.controller;
2+
3+
import com.back.domain.user.dto.UserDetailResponse;
4+
import com.back.domain.user.service.UserService;
5+
import com.back.global.common.dto.RsData;
6+
import com.back.global.security.CustomUserDetails;
7+
import lombok.RequiredArgsConstructor;
8+
import org.springframework.http.ResponseEntity;
9+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
10+
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.RestController;
13+
14+
@RestController
15+
@RequestMapping("/api/users")
16+
@RequiredArgsConstructor
17+
public class UserController {
18+
private final UserService userService;
19+
20+
@GetMapping("/me")
21+
public ResponseEntity<RsData<UserDetailResponse>> getMyInfo (
22+
@AuthenticationPrincipal CustomUserDetails user
23+
) {
24+
UserDetailResponse userDetail = userService.getUserInfo(user.getUserId());
25+
return ResponseEntity
26+
.ok(RsData.success(
27+
"회원 정보를 조회했습니다.",
28+
userDetail
29+
));
30+
}
31+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.back.domain.user.dto;
2+
3+
import com.back.domain.user.entity.Role;
4+
import com.back.domain.user.entity.User;
5+
import com.back.domain.user.entity.UserProfile;
6+
import com.back.domain.user.entity.UserStatus;
7+
8+
import java.time.LocalDateTime;
9+
10+
/**
11+
* 사용자 상세 응답을 나타내는 DTO
12+
*
13+
* @param userId 사용자의 고유 ID
14+
* @param username 사용자의 로그인 id
15+
* @param email 사용자의 이메일 주소
16+
* @param role 사용자의 역할 (예: USER, ADMIN)
17+
* @param status 사용자의 상태 (예: ACTIVE, PENDING)
18+
* @param provider 사용자의 인증 제공자 (예: GOOGLE, FACEBOOK)
19+
* @param providerId 사용자의 인증 제공자 ID
20+
* @param profile 사용자의 프로필 정보
21+
* @param createdAt 사용자 정보가 생성된 날짜 및 시간
22+
* @param updatedAt 사용자 정보가 마지막으로 업데이트된 날짜 및 시간
23+
*/
24+
public record UserDetailResponse(
25+
Long userId,
26+
String username,
27+
String email,
28+
Role role,
29+
UserStatus status,
30+
String provider,
31+
String providerId,
32+
ProfileResponse profile,
33+
LocalDateTime createdAt,
34+
LocalDateTime updatedAt
35+
) {
36+
public static UserDetailResponse from(User user) {
37+
return new UserDetailResponse(
38+
user.getId(),
39+
user.getUsername(),
40+
user.getEmail(),
41+
user.getRole(),
42+
user.getUserStatus(),
43+
user.getProvider(),
44+
user.getProviderId(),
45+
ProfileResponse.from(user.getUserProfile()),
46+
user.getCreatedAt(),
47+
user.getUpdatedAt()
48+
);
49+
}
50+
51+
/**
52+
* 사용자 프로필 정보를 나타내는 DTO
53+
*
54+
* @param nickname 사용자의 별명
55+
* @param profileImageUrl 사용자의 프로필 이미지 URL
56+
* @param bio 사용자의 자기소개
57+
* @param birthDate 사용자의 생년월일
58+
* @param point 사용자의 포인트
59+
*/
60+
public record ProfileResponse(
61+
String nickname,
62+
String profileImageUrl,
63+
String bio,
64+
LocalDateTime birthDate,
65+
int point
66+
) {
67+
public static ProfileResponse from(UserProfile profile) {
68+
if (profile == null) return null;
69+
return new ProfileResponse(
70+
profile.getNickname(),
71+
profile.getProfileImageUrl(),
72+
profile.getBio(),
73+
profile.getBirthDate(),
74+
profile.getPoint()
75+
);
76+
}
77+
}
78+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.back.domain.user.service;
2+
3+
import com.back.domain.user.dto.UserDetailResponse;
4+
import com.back.domain.user.entity.User;
5+
import com.back.domain.user.entity.UserStatus;
6+
import com.back.domain.user.repository.UserRepository;
7+
import com.back.global.exception.CustomException;
8+
import com.back.global.exception.ErrorCode;
9+
import lombok.RequiredArgsConstructor;
10+
import org.springframework.stereotype.Service;
11+
import org.springframework.transaction.annotation.Transactional;
12+
13+
@Service
14+
@RequiredArgsConstructor
15+
@Transactional
16+
public class UserService {
17+
private final UserRepository userRepository;
18+
19+
/**
20+
* 내 정보 조회 서비스
21+
* 1. 사용자 조회
22+
* 2. 상태 검증 (DELETED, SUSPENDED)
23+
* 3. UserDetailResponse 변환 및 반환
24+
*/
25+
public UserDetailResponse getUserInfo(Long userId) {
26+
27+
// userId로 User 조회
28+
User user = userRepository.findById(userId)
29+
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
30+
31+
// UserStatus가 DELETED, SUSPENDED면 예외 처리
32+
if (user.getUserStatus() == UserStatus.DELETED) {
33+
throw new CustomException(ErrorCode.USER_DELETED);
34+
}
35+
if (user.getUserStatus() == UserStatus.SUSPENDED) {
36+
throw new CustomException(ErrorCode.USER_SUSPENDED);
37+
}
38+
39+
// UserDetailResponse로 변환하여 반환
40+
return UserDetailResponse.from(user);
41+
}
42+
}

0 commit comments

Comments
 (0)