Skip to content

Commit

Permalink
[refactor] User의 Role 변경 API 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
Leewonchan14 committed Feb 19, 2024
1 parent 1c01293 commit ff8878d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/main/java/qp/official/qp/converter/UserConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static UserResponseDTO.GetUserInfoDTO toUserGetInfoDTO(User user) {
public static UserResponseDTO.UpdateUserInfoDTO toUserUpdateDTO(User user) {
return UserResponseDTO.UpdateUserInfoDTO.builder()
.userId(user.getUserId())
.role(user.getRole())
.nickname(user.getNickname())
.profileImage(user.getProfileImage())
.updatedAt(user.getUpdatedAt())
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/qp/official/qp/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,8 @@ public void updateStatus(UserStatus status) {
public void updatePoint(Long point) {
this.point += point;
}

public void updateRole(Role role) {
this.role = role;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.json.simple.parser.ParseException;
import qp.official.qp.domain.User;
import qp.official.qp.domain.enums.Role;
import qp.official.qp.web.dto.UserRequestDTO;
import java.io.IOException;
public interface UserService {
Expand All @@ -13,4 +14,6 @@ public interface UserService {
User autoSignIn(Long userId);
User deleteUser(Long userId);
User updateUserPoint(Long userId, UserRequestDTO.UpdateUserPointRequestDTO requestDTO);

User changeUserRole(Long userId, Role role);
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,12 @@ public User updateUserPoint(Long userId, UserRequestDTO.UpdateUserPointRequestDT

return user;
}

@Override
@Transactional
public User changeUserRole(Long userId, Role role) {
User user = userRepository.findById(userId).get();
user.updateRole(role);
return user;
}
}
19 changes: 17 additions & 2 deletions src/main/java/qp/official/qp/web/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import qp.official.qp.apiPayload.code.status.SuccessStatus;
import qp.official.qp.converter.UserConverter;
import qp.official.qp.domain.User;
import qp.official.qp.domain.enums.Role;
import qp.official.qp.service.TokenService.TokenService;
import qp.official.qp.service.UserService.UserService;
import qp.official.qp.validation.annotation.ExistUser;
Expand Down Expand Up @@ -125,7 +126,7 @@ public ApiResponse<UserResponseDTO.deleteUserDTO> delete(
);
}

@Operation(summary = "테스트 유저 생성", description =
@Operation(summary = "(개발용) 테스트 유저 생성", description =
"# Test User를 생성합니다. 다른 기능을 테스트 할때 이용 하세요 \n" +
"새로운 유저의 token, refreshToken도 같이 반환합니다."
, security = {@SecurityRequirement(name = "accessToken"), @SecurityRequirement(name = "refreshToken")}
Expand All @@ -142,11 +143,25 @@ public ApiResponse<UserResponseDTO.UserSignUpResultDTO> createTestUser() {
);
}

@Operation(summary = "(개발용) 계정 Role 변경", description = "특정 유저의 Role을 전환합니다.")
@PostMapping("/{userId}")
public ApiResponse<UserResponseDTO.UpdateUserInfoDTO> changeUserRole(
@PathVariable @ExistUser Long userId,
@RequestParam Role role
) {
return ApiResponse.onSuccess(
SuccessStatus.User_OK,
UserConverter.toUserUpdateDTO(
userService.changeUserRole(userId, role)
)
);
}

@PatchMapping("/point/{userId}")
@Operation(
summary = "유저 point 충전 또는 차감 API"
, description = "Header에 accessToken 필요. path variable로 userId를 입력하고, Request body에 충전 또는 차감할 point를 입력하세요. \n" +
"Response의 point는 회원이 보유한 총 point입니다."
"Response의 point는 회원이 보유한 총 point입니다."
, security = @SecurityRequirement(name = "accessToken")
)
public ApiResponse<UserResponseDTO.UpdateUserPointDTO> UpdateUserPoint(
Expand Down
1 change: 1 addition & 0 deletions src/main/java/qp/official/qp/web/dto/UserResponseDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public static class GetUserInfoDTO {
@AllArgsConstructor
public static class UpdateUserInfoDTO {
Long userId;
Role role;
String nickname;
String profileImage;
LocalDateTime updatedAt;
Expand Down

0 comments on commit ff8878d

Please sign in to comment.