-
Notifications
You must be signed in to change notification settings - Fork 3
feature : admin 기능 분리 및 UserDomainService 테스트코드 작성 #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
src/main/java/org/ezcode/codetest/application/usermanagement/user/service/AdminService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package org.ezcode.codetest.application.usermanagement.user.service; | ||
|
|
||
| import org.ezcode.codetest.application.usermanagement.user.dto.response.GrantAdminRoleResponse; | ||
| import org.ezcode.codetest.domain.user.exception.AdminException; | ||
| import org.ezcode.codetest.domain.user.exception.code.AdminExceptionCode; | ||
| import org.ezcode.codetest.domain.user.model.entity.AuthUser; | ||
| import org.ezcode.codetest.domain.user.model.entity.User; | ||
| import org.ezcode.codetest.domain.user.model.enums.UserRole; | ||
| import org.ezcode.codetest.domain.user.service.UserDomainService; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class AdminService { | ||
| private final UserDomainService userDomainService; | ||
|
|
||
| @Transactional | ||
| public GrantAdminRoleResponse grantAdminRole(AuthUser authUser, Long userId) { | ||
| if (authUser.getId().equals(userId)) { | ||
| throw new AdminException(AdminExceptionCode.GRANT_ADMIN_SELF); | ||
| } | ||
| User user = userDomainService.getUserById(userId); | ||
| if (user.getRole().equals(UserRole.ADMIN)) { | ||
| throw new AdminException(AdminExceptionCode.ALREADY_ADMIN_USER); | ||
| } | ||
| user.modifyUserRole(UserRole.ADMIN); | ||
|
|
||
| return new GrantAdminRoleResponse("ADMIN 권한을 부여합니다"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/org/ezcode/codetest/domain/user/exception/AdminException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package org.ezcode.codetest.domain.user.exception; | ||
|
|
||
| import org.ezcode.codetest.common.base.exception.ResponseCode; | ||
| import org.ezcode.codetest.domain.user.exception.code.AdminExceptionCode; | ||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public class AdminException extends RuntimeException { | ||
| private final AdminExceptionCode responseCode; | ||
| private final HttpStatus httpStatus; | ||
| private final String message; | ||
|
|
||
| public AdminException(AdminExceptionCode responseCode) { | ||
| this.responseCode = responseCode; | ||
| this.httpStatus = responseCode.getStatus(); | ||
| this.message = responseCode.getMessage(); | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/main/java/org/ezcode/codetest/domain/user/exception/code/AdminExceptionCode.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package org.ezcode.codetest.domain.user.exception.code; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| public enum AdminExceptionCode { | ||
| GRANT_ADMIN_SELF(false, HttpStatus.BAD_REQUEST, "본인에게 ADMIN 권한을 부여할 수 없습니다."), | ||
| ALREADY_ADMIN_USER(false, HttpStatus.BAD_REQUEST, "이미 ADMIN 권한을 가진 유저입니다."); | ||
|
|
||
| private final boolean success; | ||
| private final HttpStatus status; | ||
| private final String message; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/org/ezcode/codetest/presentation/usermanagement/AdminController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package org.ezcode.codetest.presentation.usermanagement; | ||
|
|
||
| import org.ezcode.codetest.application.usermanagement.user.dto.response.GrantAdminRoleResponse; | ||
| import org.ezcode.codetest.application.usermanagement.user.service.AdminService; | ||
| import org.ezcode.codetest.domain.user.model.entity.AuthUser; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api/admin") | ||
| @RequiredArgsConstructor | ||
| @Tag(name = "관리자(Admin) 전용 기능", description = "관리자 권한을 가진 유저만 접근 가능한 기능입니다") | ||
| public class AdminController { | ||
| private final AdminService adminService; | ||
|
|
||
| @Operation(summary = "관리자로 전환", description = "관리자 권한을 가지고 있는 유저는 다른 유저의 권한을 관리자로 수정할 수 있습니다.") | ||
| @PostMapping("/users/{userId}/grant-admin") | ||
| public ResponseEntity<GrantAdminRoleResponse> grantAdminRole( | ||
| @AuthenticationPrincipal AuthUser authUser, | ||
| @PathVariable Long userId | ||
| ){ | ||
| return ResponseEntity.status(HttpStatus.OK).body(adminService.grantAdminRole(authUser, userId)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.