Skip to content

Commit

Permalink
#82 [refactor] : 유저 DTO를 레코드로 리팩토링한다
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbang105 committed Oct 21, 2024
1 parent 2a7df7f commit 30da7ed
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 89 deletions.
17 changes: 10 additions & 7 deletions src/main/java/side/onetime/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import side.onetime.dto.UserDto;
import side.onetime.dto.user.request.OnboardUserRequest;
import side.onetime.dto.user.request.UpdateUserProfileRequest;
import side.onetime.dto.user.response.GetUserProfileResponse;
import side.onetime.dto.user.response.OnboardUserResponse;
import side.onetime.global.common.ApiResponse;
import side.onetime.global.common.status.SuccessStatus;
import side.onetime.service.UserService;
Expand All @@ -16,27 +19,27 @@ public class UserController {

// 유저 온보딩 API
@PostMapping("/onboarding")
public ResponseEntity<ApiResponse<UserDto.OnboardUserResponse>> onboardUser(
@RequestBody UserDto.OnboardUserRequest onboardUserRequest) {
public ResponseEntity<ApiResponse<OnboardUserResponse>> onboardUser(
@RequestBody OnboardUserRequest onboardUserRequest) {

UserDto.OnboardUserResponse onboardUserResponse = userService.onboardUser(onboardUserRequest);
OnboardUserResponse onboardUserResponse = userService.onboardUser(onboardUserRequest);
return ApiResponse.onSuccess(SuccessStatus._ONBOARD_USER, onboardUserResponse);
}

// 유저 정보 조회 API
@GetMapping("/profile")
public ResponseEntity<ApiResponse<UserDto.GetUserProfileResponse>> getUserProfile(
public ResponseEntity<ApiResponse<GetUserProfileResponse>> getUserProfile(
@RequestHeader("Authorization") String authorizationHeader) {

UserDto.GetUserProfileResponse getUserProfileResponse = userService.getUserProfile(authorizationHeader);
GetUserProfileResponse getUserProfileResponse = userService.getUserProfile(authorizationHeader);
return ApiResponse.onSuccess(SuccessStatus._GET_USER_PROFILE, getUserProfileResponse);
}

// 유저 정보 수정 API
@PatchMapping("/profile/action-update")
public ResponseEntity<ApiResponse<SuccessStatus>> updateUserProfile(
@RequestHeader("Authorization") String authorizationHeader,
@RequestBody UserDto.UpdateUserProfileRequest updateUserProfileRequest) {
@RequestBody UpdateUserProfileRequest updateUserProfileRequest) {

userService.updateUserProfile(authorizationHeader, updateUserProfileRequest);
return ApiResponse.onSuccess(SuccessStatus._UPDATE_USER_PROFILE);
Expand Down
69 changes: 0 additions & 69 deletions src/main/java/side/onetime/dto/UserDto.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package side.onetime.dto.user.request;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import jakarta.validation.constraints.NotBlank;

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public record OnboardUserRequest(
@NotBlank(message = "Register Token은 필수 값입니다.") String registerToken,
@NotBlank(message = "Nickname은 필수 값입니다.") String nickname
) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package side.onetime.dto.user.request;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import jakarta.validation.constraints.NotBlank;

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public record UpdateUserProfileRequest(
@NotBlank(message = "Nickname은 필수 값입니다.") String nickname
) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package side.onetime.dto.user.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import side.onetime.domain.User;

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public record GetUserProfileResponse(
String nickname,
String email
) {
public static GetUserProfileResponse of(User user) {
return new GetUserProfileResponse(user.getNickname(), user.getEmail());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package side.onetime.dto.user.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public record OnboardUserResponse(
String accessToken,
String refreshToken
) {
public static OnboardUserResponse of(String accessToken, String refreshToken) {
return new OnboardUserResponse(accessToken, refreshToken);
}
}
26 changes: 13 additions & 13 deletions src/main/java/side/onetime/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import org.springframework.stereotype.Service;
import side.onetime.domain.RefreshToken;
import side.onetime.domain.User;
import side.onetime.dto.UserDto;
import side.onetime.dto.user.request.OnboardUserRequest;
import side.onetime.dto.user.request.UpdateUserProfileRequest;
import side.onetime.dto.user.response.GetUserProfileResponse;
import side.onetime.dto.user.response.OnboardUserResponse;
import side.onetime.exception.UserErrorResult;
import side.onetime.exception.UserException;
import side.onetime.repository.RefreshTokenRepository;
Expand All @@ -31,23 +34,23 @@ public class UserService {

// 유저 온보딩 메서드
@Transactional
public UserDto.OnboardUserResponse onboardUser(UserDto.OnboardUserRequest onboardUserRequest) {
public OnboardUserResponse onboardUser(OnboardUserRequest onboardUserRequest) {
// 레지스터 토큰을 이용하여 사용자 정보 추출
String registerToken = onboardUserRequest.getRegisterToken();
String registerToken = onboardUserRequest.registerToken();
jwtUtil.validateTokenExpiration(registerToken);
String provider = jwtUtil.getProviderFromToken(registerToken);
String providerId = jwtUtil.getProviderIdFromToken(registerToken);
String name = jwtUtil.getNameFromToken(registerToken);
String email = jwtUtil.getEmailFromToken(registerToken);

if (onboardUserRequest.getNickname().length() > NICKNAME_LENGTH_LIMIT) {
if (onboardUserRequest.nickname().length() > NICKNAME_LENGTH_LIMIT) {
throw new UserException(UserErrorResult._NICKNAME_TOO_LONG);
}

User user = User.builder()
.name(name)
.email(email)
.nickname(onboardUserRequest.getNickname())
.nickname(onboardUserRequest.nickname())
.provider(provider)
.providerId(providerId)
.build();
Expand All @@ -63,26 +66,23 @@ public UserDto.OnboardUserResponse onboardUser(UserDto.OnboardUserRequest onboar
refreshTokenRepository.save(newRefreshToken);

// 액세스 토큰 반환
return UserDto.OnboardUserResponse.of(accessToken, refreshToken);
return OnboardUserResponse.of(accessToken, refreshToken);
}

// 유저 정보 조회 메서드
@Transactional(readOnly = true)
public UserDto.GetUserProfileResponse getUserProfile(String authorizationHeader) {
public GetUserProfileResponse getUserProfile(String authorizationHeader) {
User user = jwtUtil.getUserFromHeader(authorizationHeader);

return UserDto.GetUserProfileResponse.of(user);
return GetUserProfileResponse.of(user);
}

// 유저 정보 수정 메서드
@Transactional
public void updateUserProfile(String authorizationHeader, UserDto.UpdateUserProfileRequest updateUserProfileRequest) {
public void updateUserProfile(String authorizationHeader, UpdateUserProfileRequest updateUserProfileRequest) {
User user = jwtUtil.getUserFromHeader(authorizationHeader);
String nickname = updateUserProfileRequest.getNickname();
String nickname = updateUserProfileRequest.nickname();

if (nickname == null) {
throw new UserException(UserErrorResult._NOT_FOUND_REQUEST_NICKNAME);
}
if (nickname.length() > NICKNAME_LENGTH_LIMIT) {
throw new UserException(UserErrorResult._NICKNAME_TOO_LONG);
}
Expand Down

0 comments on commit 30da7ed

Please sign in to comment.