refactor: 为添加用户功能添加返回值
This commit is contained in:
@@ -58,9 +58,8 @@ public class UserController {
|
||||
|
||||
@PostMapping
|
||||
@PreAuthorize("hasAnyAuthority('system:user:write')")
|
||||
public ResponseEntity<Void> addUser(@Validated @RequestBody AddUserRequest request) {
|
||||
userService.addUser(request);
|
||||
return ResponseEntity.ok(null);
|
||||
public UserDetailResponse addUser(@Validated @RequestBody AddUserRequest request) {
|
||||
return userService.addUser(request);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.onixbyte.helix.domain.web.response;
|
||||
|
||||
import com.onixbyte.helix.domain.entity.User;
|
||||
import com.onixbyte.helix.enumeration.UserStatus;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
@@ -175,6 +176,22 @@ public class UserDetailResponse {
|
||||
private UserDetailResponseBuilder() {
|
||||
}
|
||||
|
||||
public UserDetailResponseBuilder user(User user) {
|
||||
this.id = String.valueOf(user.getId());
|
||||
this.username = user.getUsername();
|
||||
this.fullName = user.getFullName();
|
||||
this.email = user.getEmail();
|
||||
this.regionAbbreviation = user.getRegionAbbreviation();
|
||||
this.phoneNumber = user.getPhoneNumber();
|
||||
this.avatarUrl = user.getAvatarUrl();
|
||||
this.status = user.getStatus();
|
||||
this.departmentId = user.getDepartmentId();
|
||||
this.positionId = user.getPositionId();
|
||||
this.createdAt = user.getCreatedAt();
|
||||
this.updatedAt = user.getUpdatedAt();
|
||||
return this;
|
||||
}
|
||||
|
||||
public UserDetailResponseBuilder id(String id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
|
||||
@@ -8,8 +8,6 @@ import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@Component
|
||||
public class DepartmentManager {
|
||||
|
||||
@@ -25,4 +23,8 @@ public class DepartmentManager {
|
||||
public Page<Department> selectAll(Pageable pageable) {
|
||||
return departmentRepository.findAll(pageable);
|
||||
}
|
||||
|
||||
public Department selectById(Long id) {
|
||||
return departmentRepository.findById(id).orElse(null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,4 +23,8 @@ public class PositionManager {
|
||||
public Page<Position> selectAll(Pageable pageable) {
|
||||
return positionRepository.findAll(pageable);
|
||||
}
|
||||
|
||||
public Position selectById(Long id) {
|
||||
return positionRepository.findById(id).orElse(null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,10 +11,7 @@ import com.onixbyte.helix.domain.web.request.QueryUserRequest;
|
||||
import com.onixbyte.helix.domain.web.request.ResetPasswordRequest;
|
||||
import com.onixbyte.helix.domain.web.request.EditUserRequest;
|
||||
import com.onixbyte.helix.domain.web.response.UserDetailResponse;
|
||||
import com.onixbyte.helix.manager.ApplicationManager;
|
||||
import com.onixbyte.helix.manager.RoleManager;
|
||||
import com.onixbyte.helix.manager.UserManager;
|
||||
import com.onixbyte.helix.manager.UserRoleManager;
|
||||
import com.onixbyte.helix.manager.*;
|
||||
import com.onixbyte.identitygenerator.IdentityGenerator;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -36,6 +33,8 @@ public class UserService {
|
||||
private final UserRoleManager userRoleManager;
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
private final ApplicationManager applicationManager;
|
||||
private final DepartmentManager departmentManager;
|
||||
private final PositionManager positionManager;
|
||||
|
||||
@Autowired
|
||||
public UserService(
|
||||
@@ -44,14 +43,16 @@ public class UserService {
|
||||
RoleManager roleManager,
|
||||
UserRoleManager userRoleManager,
|
||||
PasswordEncoder passwordEncoder,
|
||||
ApplicationManager applicationManager
|
||||
) {
|
||||
ApplicationManager applicationManager,
|
||||
DepartmentManager departmentManager, PositionManager positionManager) {
|
||||
this.userManager = userManager;
|
||||
this.userIdentityGenerator = userIdentityGenerator;
|
||||
this.roleManager = roleManager;
|
||||
this.userRoleManager = userRoleManager;
|
||||
this.passwordEncoder = passwordEncoder;
|
||||
this.applicationManager = applicationManager;
|
||||
this.departmentManager = departmentManager;
|
||||
this.positionManager = positionManager;
|
||||
}
|
||||
|
||||
public Page<UserDetailResponse> queryUserDetailsPage(Pageable pageable, QueryUserRequest request) {
|
||||
@@ -71,7 +72,7 @@ public class UserService {
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Throwable.class)
|
||||
public void addUser(AddUserRequest request) {
|
||||
public UserDetailResponse addUser(AddUserRequest request) {
|
||||
var createTime = LocalDateTime.now();
|
||||
|
||||
// validate all roles are existed
|
||||
@@ -131,6 +132,17 @@ public class UserService {
|
||||
|
||||
// Save user and role bindings
|
||||
userRoleManager.saveBatch(userRoleBindings);
|
||||
|
||||
// Get department and position
|
||||
var department = departmentManager.selectById(user.getDepartmentId());
|
||||
var position = positionManager.selectById(user.getPositionId());
|
||||
|
||||
// Build response and return
|
||||
return UserDetailResponse.builder()
|
||||
.user(user)
|
||||
.departmentName(department.getName())
|
||||
.positionName(position.getName())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Throwable.class)
|
||||
|
||||
Reference in New Issue
Block a user