add user api
This commit is contained in:
parent
b501cd3cb2
commit
eb9767c2d1
|
@ -0,0 +1,14 @@
|
|||
package io.metersphere.commons;
|
||||
|
||||
public class MSException extends RuntimeException{
|
||||
|
||||
private MSException(String message) { super(message); }
|
||||
|
||||
private MSException(Throwable t) { super(t); }
|
||||
|
||||
public static void throwException(String message) { throw new MSException(message); }
|
||||
|
||||
public static MSException getException(String message) { throw new MSException(message); }
|
||||
|
||||
public static void throwException(Throwable t) { throw new MSException(t); }
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package io.metersphere.controller;
|
||||
|
||||
import io.metersphere.base.domain.User;
|
||||
import io.metersphere.dto.UserDTO;
|
||||
import io.metersphere.service.UserService;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@RequestMapping("user")
|
||||
@RestController
|
||||
public class UserController {
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
@PostMapping("/add")
|
||||
public UserDTO insertUser(@RequestBody User user) {
|
||||
return userService.insert(user);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
package io.metersphere.dto;
|
||||
|
||||
import io.metersphere.base.domain.Role;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class UserDTO {
|
||||
private String id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String email;
|
||||
|
||||
private String phone;
|
||||
|
||||
private String status;
|
||||
|
||||
private Long createTime;
|
||||
|
||||
private Long updateTime;
|
||||
|
||||
private List<Role> roles = new ArrayList<>();
|
||||
|
||||
public List<Role> getRoles() {
|
||||
return roles;
|
||||
}
|
||||
|
||||
public void setRoles(List<Role> roles) {
|
||||
this.roles = roles;
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id == null ? null : id.trim();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name == null ? null : name.trim();
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email == null ? null : email.trim();
|
||||
}
|
||||
|
||||
public String getPhone() {
|
||||
return phone;
|
||||
}
|
||||
|
||||
public void setPhone(String phone) {
|
||||
this.phone = phone == null ? null : phone.trim();
|
||||
}
|
||||
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(String status) {
|
||||
this.status = status == null ? null : status.trim();
|
||||
}
|
||||
|
||||
public Long getCreateTime() {
|
||||
return createTime;
|
||||
}
|
||||
|
||||
public void setCreateTime(Long createTime) {
|
||||
this.createTime = createTime;
|
||||
}
|
||||
|
||||
public Long getUpdateTime() {
|
||||
return updateTime;
|
||||
}
|
||||
|
||||
public void setUpdateTime(Long updateTime) {
|
||||
this.updateTime = updateTime;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package io.metersphere.dto;
|
||||
|
||||
import io.metersphere.base.domain.Role;
|
||||
import io.metersphere.base.domain.User;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class UserOperateDTO extends User {
|
||||
|
||||
private List<Role> roleList;
|
||||
|
||||
public List<Role> getRoleList() {
|
||||
return roleList;
|
||||
}
|
||||
|
||||
public void setRoleList(List<Role> roleList) {
|
||||
this.roleList = roleList;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
package io.metersphere.service;
|
||||
|
||||
import io.metersphere.base.domain.*;
|
||||
import io.metersphere.base.mapper.RoleMapper;
|
||||
import io.metersphere.base.mapper.UserMapper;
|
||||
import io.metersphere.base.mapper.UserRoleMapper;
|
||||
import io.metersphere.commons.MSException;
|
||||
import io.metersphere.dto.UserDTO;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
|
||||
@Resource
|
||||
private UserMapper userMapper;
|
||||
@Resource
|
||||
private RoleMapper roleMapper;
|
||||
@Resource
|
||||
private UserRoleMapper userRoleMapper;
|
||||
|
||||
public UserDTO insert(User user) {
|
||||
checkUserParam(user);
|
||||
createUser(user);
|
||||
return getUserDTO(user.getId());
|
||||
}
|
||||
|
||||
private void checkUserParam(User user) {
|
||||
if (StringUtils.isBlank(user.getName())) {
|
||||
MSException.throwException("user_name_empty");
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(user.getEmail())) {
|
||||
MSException.throwException("user_email_empty");
|
||||
}
|
||||
// password
|
||||
}
|
||||
|
||||
private void createUser(User userRequest) {
|
||||
User user = new User();
|
||||
BeanUtils.copyProperties(userRequest, user);
|
||||
user.setCreateTime(System.currentTimeMillis());
|
||||
user.setUpdateTime(System.currentTimeMillis());
|
||||
user.setStatus("0");
|
||||
|
||||
UserExample userExample = new UserExample();
|
||||
UserExample.Criteria criteria = userExample.createCriteria();
|
||||
criteria.andEmailEqualTo(user.getEmail());
|
||||
List<User> userList = userMapper.selectByExample(userExample);
|
||||
if (!CollectionUtils.isEmpty(userList)) {
|
||||
MSException.throwException("user_email_is_exist");
|
||||
}
|
||||
userMapper.insertSelective(user);
|
||||
}
|
||||
|
||||
public UserDTO getUserDTO(String userId) {
|
||||
|
||||
User user = userMapper.selectByPrimaryKey(userId);
|
||||
if (user == null) {
|
||||
return null;
|
||||
}
|
||||
UserDTO userDTO = new UserDTO();
|
||||
BeanUtils.copyProperties(user, userDTO);
|
||||
//
|
||||
UserRoleExample userRoleExample = new UserRoleExample();
|
||||
userRoleExample.createCriteria().andUserIdEqualTo(userId);
|
||||
List<UserRole> userRoleList = userRoleMapper.selectByExample(userRoleExample);
|
||||
|
||||
if (CollectionUtils.isEmpty(userRoleList)) {
|
||||
return userDTO;
|
||||
}
|
||||
|
||||
List<String> roleIds = userRoleList.stream().map(UserRole::getRoleId).collect(Collectors.toList());
|
||||
|
||||
RoleExample roleExample = new RoleExample();
|
||||
roleExample.createCriteria().andIdIn(roleIds);
|
||||
|
||||
List<Role> roleList = roleMapper.selectByExample(roleExample);
|
||||
userDTO.setRoles(roleList);
|
||||
|
||||
return userDTO;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue