Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.back.domain.profile.controller;

import com.back.domain.profile.dto.ProfileResponseDto;
import com.back.domain.profile.service.ProfileService;
import com.back.domain.user.service.UserService;
import com.back.global.rsData.RsData;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/me/profile")
@RequiredArgsConstructor
public class ProfileController {

private final UserService userService;
private final ProfileService profileService;

@GetMapping
public RsData<ProfileResponseDto> getProfile(@AuthenticationPrincipal(expression = "id") Long userId) {
ProfileResponseDto body = profileService.getProfile(userId);
return RsData.successOf(body); // code=200, message="success"
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/back/domain/profile/dto/ProfileResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.back.domain.profile.dto;

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class ProfileResponseDto {
private Long id;
private String nickname;
private String email;

// 서버에 저장된 실제 값(0~100)
private Double abvDegree; // 83.2
// 표현용(서버에서 계산)
private Integer abvLevel; // 1~6
private String abvLabel; // "83.2%"
}
34 changes: 34 additions & 0 deletions src/main/java/com/back/domain/profile/service/ProfileService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.back.domain.profile.service;

import com.back.domain.profile.dto.ProfileResponseDto;
import com.back.domain.user.entity.User;
import com.back.domain.user.repository.UserRepository;
import com.back.domain.user.support.AbvView;
import com.back.global.exception.ServiceException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class ProfileService {

private final UserRepository userRepository;

@Transactional(readOnly = true)
public ProfileResponseDto getProfile(Long id) {
User user = userRepository.findById(id).orElseThrow(() -> new ServiceException(404, "사용자를 찾을 수 없습니다."));

Double percent = user.getAbvDegree();
int level = AbvView.levelOf(percent);
String label = AbvView.percentLabel(percent);

return ProfileResponseDto.builder()
.id(user.getId())
.nickname(user.getNickname())
.abvDegree(percent)
.abvLevel(level)
.abvLabel(label)
.build();
}
}
8 changes: 6 additions & 2 deletions src/main/java/com/back/domain/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import lombok.*;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;

import java.time.LocalDateTime;
import java.util.ArrayList;
Expand Down Expand Up @@ -33,16 +35,18 @@ public class User {

private Double abvDegree; // 알콜도수(회원 등급)

@CreatedDate // JPA Auditing 적용
@Column(nullable = false, updatable = false)
private LocalDateTime createdAt; // 생성 날짜

@LastModifiedDate // JPA Auditing 적용
@Column(nullable = false)
private LocalDateTime updatedAt; // 수정 날짜

@Builder.Default
@Column(nullable = false, length = 20)
private String role = "USER";

private String profileImgUrl;

public boolean isAdmin() {
return "ADMIN".equalsIgnoreCase(role);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
boolean existsByNicknameAndIdNot(String nickname, Long id);
}

28 changes: 28 additions & 0 deletions src/main/java/com/back/domain/user/support/AbvView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.back.domain.user.support;

public final class AbvView {

private AbvView(){}

// 0~100%를 6단계로 매핑
public static int levelOf(Double percent) {
if (percent == null) return 1;

double x = Math.max(0, Math.min(100, percent));
int p = (int) x;

if (p <= 10) return 1; // 0~10
if (p <= 25) return 2; // 11~25
if (p <= 45) return 3; // 26~45
if (p <= 65) return 4; // 46~65
if (p <= 85) return 5; // 66~85
return 6; // 86~100
}

// 화면용 "23.5%" 라벨
public static String percentLabel(Double percent) {
if (percent == null) return "0%";
double x = Math.max(0.0, Math.min(100.0, percent));
return (x % 1.0 == 0.0) ? String.format("%.0f%%", x) : String.format("%.1f%%", x);
}
}