Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dependencies {

implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-oauth2-client")
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0")
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.7.0")
implementation("io.jsonwebtoken:jjwt-api:0.12.3")
runtimeOnly("io.jsonwebtoken:jjwt-impl:0.12.3")
runtimeOnly("io.jsonwebtoken:jjwt-jackson:0.12.3")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.back.domain.profile.controller;

import com.back.domain.profile.dto.ProfileResponseDto;
import com.back.domain.profile.dto.ProfileUpdateRequestDto;
import com.back.domain.profile.service.ProfileService;
import com.back.domain.user.service.UserService;
import com.back.global.rsData.RsData;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

@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"
}

@PutMapping
public RsData<ProfileResponseDto> updateProfile(@AuthenticationPrincipal(expression = "id") Long userId, @Valid @RequestBody ProfileUpdateRequestDto profileUpdateRequestDto) {
ProfileResponseDto body = profileService.updateProfile(userId, profileUpdateRequestDto);
return RsData.successOf(body); // code=200
}
}
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%"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.back.domain.profile.dto;

import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.Size;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class ProfileUpdateRequestDto {

@Size(min = 1, max = 10, message = "닉네임은 1~10자")
private String nickname;

@Email(message = "이메일 형식이 아닙니다")
private String email;
}
62 changes: 62 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,62 @@
package com.back.domain.profile.service;

import com.back.domain.profile.dto.ProfileResponseDto;
import com.back.domain.profile.dto.ProfileUpdateRequestDto;
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();
}

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

if (profileUpdateRequestDto.getNickname() != null) {
String nickname = profileUpdateRequestDto.getNickname().trim();
if (nickname.isEmpty() || nickname.length() > 10) {
throw new ServiceException(400, "닉네임은 1~10자");
}

if (userRepository.existsByNicknameAndIdNot(nickname, id)) {
throw new ServiceException(409, "이미 사용중인 닉네임");
}

user.setNickname(nickname);
}

if (profileUpdateRequestDto.getEmail() != null) {
String email = profileUpdateRequestDto.getEmail().trim();
user.setEmail(email.isEmpty() ? null : email);
}

userRepository.save(user);

return getProfile(id);
}
}
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);
}
}