-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
02a9de5
commit ed268d3
Showing
5 changed files
with
144 additions
and
11 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/main/java/com/example/demo/domain/user/controller/UserAdminController.java
This file contains 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,46 @@ | ||
package com.example.demo.domain.user.controller; | ||
|
||
import com.example.demo.domain.user.domain.dto.request.UpdateUserInfoRequest; | ||
import com.example.demo.domain.user.domain.dto.response.UserInfo; | ||
import com.example.demo.domain.user.service.UserAdminService; | ||
import com.example.demo.global.base.dto.ResponseBody; | ||
import com.example.demo.global.base.dto.page.GlobalPageResponse; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.data.web.PageableDefault; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import static com.example.demo.global.base.dto.ResponseUtil.createSuccessResponse; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping("/api/v1/admin/users") | ||
@PreAuthorize("isAuthenticated() and hasRole('ROLE_ADMIN')") | ||
public class UserAdminController { | ||
|
||
private final UserAdminService userAdminService; | ||
|
||
@GetMapping | ||
public ResponseEntity<ResponseBody<GlobalPageResponse<UserInfo>>> getAllUsers ( | ||
@PageableDefault(page=0, size=10,sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable | ||
) { | ||
return ResponseEntity.ok(createSuccessResponse(userAdminService.getAllUsers(pageable))); | ||
} | ||
|
||
@PatchMapping("/{userId}") | ||
public ResponseEntity<ResponseBody<Void>> updateUserInfo (@PathVariable Long userId, @Valid @RequestBody UpdateUserInfoRequest request) { | ||
userAdminService.updateUserInfo(userId, request); | ||
return ResponseEntity.ok(createSuccessResponse()); | ||
} | ||
|
||
@DeleteMapping("/{userId}") | ||
public ResponseEntity<ResponseBody<Void>> deleteUser (@PathVariable Long userId) { | ||
userAdminService.deleteUser(userId); | ||
return ResponseEntity.ok(createSuccessResponse()); | ||
} | ||
} |
This file contains 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
17 changes: 17 additions & 0 deletions
17
src/main/java/com/example/demo/domain/user/domain/dto/request/UpdateUserInfoRequest.java
This file contains 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 com.example.demo.domain.user.domain.dto.request; | ||
|
||
import com.example.demo.domain.user.domain.vo.Role; | ||
import com.example.demo.global.aop.valid.ValidEnum; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.Pattern; | ||
|
||
import static com.example.demo.global.regex.S3UrlRegex.S3_PROFILE_FILE_URL; | ||
import static com.example.demo.global.regex.UserRegex.NICKNAME_REGEXP; | ||
|
||
public record UpdateUserInfoRequest( | ||
@Pattern(regexp = NICKNAME_REGEXP, message = "닉네임 정규식을 맞춰주세요.") String nickname, | ||
@NotBlank(message = "이름은 빈값일 수 없습니다.") String name, | ||
@Pattern(regexp = S3_PROFILE_FILE_URL) String profileImageUrl, | ||
@ValidEnum(enumClass = Role.class) Role role | ||
) { | ||
} |
This file contains 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
52 changes: 52 additions & 0 deletions
52
src/main/java/com/example/demo/domain/user/service/UserAdminService.java
This file contains 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,52 @@ | ||
package com.example.demo.domain.user.service; | ||
|
||
import com.example.demo.domain.user.domain.User; | ||
import com.example.demo.domain.user.domain.dto.request.UpdateUserInfoRequest; | ||
import com.example.demo.domain.user.domain.dto.response.UserInfo; | ||
import com.example.demo.domain.user.repository.UserRepository; | ||
import com.example.demo.global.base.dto.page.GlobalPageResponse; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Optional; | ||
|
||
@Transactional(readOnly = true) | ||
@RequiredArgsConstructor | ||
@Service | ||
public class UserAdminService { | ||
|
||
private final UserRepository userRepository; | ||
private final UserService userService; | ||
|
||
public Boolean isAdmin(Long userId) { | ||
Optional<User> userOptional = userRepository.findById(userId); | ||
|
||
if (userOptional.isPresent()) { | ||
User user = userOptional.get(); | ||
return user.isAdmin(); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public GlobalPageResponse<UserInfo> getAllUsers(Pageable pageable) { | ||
Page<UserInfo> userInfoPage = userRepository.findAll(pageable).map(UserInfo::from); | ||
return GlobalPageResponse.create(userInfoPage); | ||
} | ||
|
||
@Transactional | ||
public void updateUserInfo(Long userId, UpdateUserInfoRequest request) { | ||
User user = userService.validateUser(userId); | ||
userService.checkNicknameDuplicate(request.nickname()); | ||
user.updateUserInfo(request); | ||
} | ||
|
||
@Transactional | ||
public void deleteUser(Long userId) { | ||
userRepository.deleteById(userId); | ||
} | ||
} |