사용자 조회 통합

뚜우웅이·2025년 5월 24일

캡스톤 디자인

목록 보기
27/35

DTO

UserDto

   @Builder
    @Schema(description = "사용자 응답 정보")
    public record UserResponse(
            @Schema(description = "사용자 ID", example = "1")
            Long id,

            @Schema(description = "이메일", example = "user@example.com")
            String email,

            @Schema(description = "이름", example = "홍길동")
            String name,

            @Schema(description = "휴대폰 번호", example = "010-1234-5678")
            String phone,

            @Schema(description = "사용자 역할", example = "일반 사용자")
            String role,

            @Schema(description = "이메일 인증 여부", example = "true")
            boolean emailVerified,

            @Schema(description = "총 판매 중인 상품 수", example = "3")
            int totalSellingCount,

            @Schema(description = "총 판매 완료 수", example = "7")
            int totalSoldCount,

            @Schema(description = "총 구매 수", example = "15")
            int totalPurchaseCount,

            @Schema(description = "가입 날짜", example = "2025-01-01T09:00:00")
            LocalDateTime joinDate
    ) {
        public static UserResponse from(User user, int sellingCount, int soldCount,
                                        int purchaseCount) {
            return UserResponse.builder()
                    .id(user.getId())
                    .email(user.getEmail())
                    .name(user.getName())
                    .phone(user.getPhone())
                    .role(user.getRole().getDisplayName())
                    .emailVerified(user.isEmailVerified())
                    .totalSellingCount(sellingCount)
                    .totalSoldCount(soldCount)
                    .totalPurchaseCount(purchaseCount)
                    .joinDate(user.getCreatedDate())
                    .build();
        }
    }

    @Builder
    @Schema(description = "다른 사용자 응답 정보")
    public record DiffUserResponse(
            @Schema(description = "이름", example = "홍길동")
            String name,

            @Schema(description = "총 판매 중인 상품 수", example = "3")
            int totalSellingCount,

            @Schema(description = "총 판매 완료 수", example = "7")
            int totalSoldCount,

            @Schema(description = "총 구매 수", example = "15")
            int totalPurchaseCount,

            @Schema(description = "가입 날짜", example = "2025-01-01T09:00:00")
            LocalDateTime joinDate
    ) {
        public static DiffUserResponse from(User user, int sellingCount, int soldCount,
                                        int purchaseCount) {
            return DiffUserResponse.builder()
                    .name(user.getName())
                    .totalSellingCount(sellingCount)
                    .totalSoldCount(soldCount)
                    .totalPurchaseCount(purchaseCount)
                    .joinDate(user.getCreatedDate())
                    .build();
        }
    }
  • DTO에서 상세 정보를 받아올 수 있게 수정해준다.
  • 다른 사용자의 정보는 일부분만 조회할 수 있게한다.

Service

UserService

   public UserDto.UserResponse getUserProfile(Long userId) {
        User user = getUser(userId);

        // 통계 정보 조회
        long activeProductCount = productRepository.countBySellerIdAndStatus(userId, ProductStatus.ACTIVE);
        long soldProductCount = productRepository.countBySellerIdAndStatus(userId, ProductStatus.SOLD_OUT);
        long purchaseCount = productRepository.countByBuyerId(userId);


        return UserDto.UserResponse.from(
                user,
                (int) activeProductCount,
                (int) soldProductCount,
                (int) purchaseCount);
    }

    public UserDto.DiffUserResponse getDiffUserProfile(Long userId) {
        User user = getUser(userId);

        // 통계 정보 조회
        long activeProductCount = productRepository.countBySellerIdAndStatus(userId, ProductStatus.ACTIVE);
        long soldProductCount = productRepository.countBySellerIdAndStatus(userId, ProductStatus.SOLD_OUT);
        long purchaseCount = productRepository.countByBuyerId(userId);


        return UserDto.DiffUserResponse.from(
                user,
                (int) activeProductCount,
                (int) soldProductCount,
                (int) purchaseCount);
    }

    @Transactional
    public UserDto.UserResponse updateProfile(Long userId, UserDto.ProfileUpdateRequest request) {
        User user = getUser(userId);
        user.updateProfile(request.name(), request.phone());

        log.info("사용자 프로필 업데이트 완료: {}", userId);

        long activeProductCount = productRepository.countBySellerIdAndStatus(userId, ProductStatus.ACTIVE);
        long soldProductCount = productRepository.countBySellerIdAndStatus(userId, ProductStatus.SOLD_OUT);
        long purchaseCount = productRepository.countByBuyerId(userId);

        return UserDto.UserResponse.from(
                user,
                (int) activeProductCount,
                (int) soldProductCount,
                (int) purchaseCount);
    }

사용자의 상세 정보 조회를 위해 통계 정보를 가져와서 DTO로 반환해준다.

Controller

UserController

    @Operation(summary = "다른 사용자 프로필 조회", description = "다른 사용자의 프로필 정보를 조회합니다. (제한된 정보만 제공)")
    @ApiResponse(responseCode = "200", description = "사용자 프로필 조회 성공")
    @GetMapping("/{userId}")
    public ResponseEntity<ResponseDTO<UserDto.DiffUserResponse>> getUserProfile(
            @Parameter(description = "조회할 사용자 ID", required = true) @PathVariable Long userId) {

        log.info("사용자 프로필 조회: 사용자 ID {}", userId);
        UserDto.DiffUserResponse response = userService.getDiffUserProfile(userId);

        return ResponseEntity.ok(ResponseDTO.success(response));
    }

다른 사용자 프로필 조회 api 엔드포인트를 UserController로 가져와준다.

profile
공부하는 초보 개발자

0개의 댓글