refactor(web): 移除用户和数据文件创建时的冗余信息
- 移除了 DataFileService 中设置创建和修改用户的代码- 更新了 UserService 中的当前登录用户获取方式,增加了 Optional 类型的方法 - 移除了 EntityHelper 中填充创建者和修改者的代码
This commit is contained in:
@@ -36,9 +36,6 @@ public class DataFileService extends SimpleServiceSupport<DataFile> {
|
||||
public Long initialDataFile(String filename) {
|
||||
DataFile dataFile = new DataFile();
|
||||
dataFile.setFilename(filename);
|
||||
User loginUser = userService.currentLoginUser();
|
||||
dataFile.setCreatedUser(loginUser);
|
||||
dataFile.setModifiedUser(loginUser);
|
||||
return dataFileRepository.save(dataFile).getId();
|
||||
}
|
||||
|
||||
|
||||
@@ -3,13 +3,12 @@ package com.eshore.gringotts.web.domain.user.service;
|
||||
import cn.dev33.satoken.exception.NotLoginException;
|
||||
import cn.dev33.satoken.stp.SaTokenInfo;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import com.eshore.gringotts.web.domain.user.entity.User;
|
||||
import com.eshore.gringotts.web.domain.user.repository.UserRepository;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import lombok.Data;
|
||||
import org.eclipse.collections.api.factory.Lists;
|
||||
import org.eclipse.collections.api.list.ImmutableList;
|
||||
@@ -73,7 +72,15 @@ public class UserService {
|
||||
}
|
||||
|
||||
public User currentLoginUser() {
|
||||
return userRepository.findById(StpUtil.getLoginIdAsLong()).orElseThrow(LoginNotFoundException::new);
|
||||
return currentLoginUserOptional().orElseThrow(LoginNotFoundException::new);
|
||||
}
|
||||
|
||||
public Optional<User> currentLoginUserOptional() {
|
||||
try {
|
||||
return userRepository.findById(StpUtil.getLoginIdAsLong());
|
||||
} catch (Throwable throwable) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
private User findUserByUsername(String username) {
|
||||
@@ -129,7 +136,6 @@ public class UserService {
|
||||
*/
|
||||
public void registerFromAdministrator(String username, String password, User.Role role) {
|
||||
User loginUser = currentLoginUser();
|
||||
|
||||
User user = new User();
|
||||
user.setUsername(username);
|
||||
user.setPassword(encryptPassword(password));
|
||||
@@ -137,8 +143,6 @@ public class UserService {
|
||||
user.setState(User.State.NORMAL);
|
||||
user.setCheckedUser(loginUser);
|
||||
user.setCheckedTime(LocalDateTime.now());
|
||||
user.setCreatedUser(loginUser);
|
||||
user.setModifiedUser(loginUser);
|
||||
userRepository.save(user);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
package com.eshore.gringotts.web.helper;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.eshore.gringotts.web.domain.base.entity.SimpleEntity;
|
||||
import com.eshore.gringotts.web.domain.user.entity.User;
|
||||
import com.eshore.gringotts.web.domain.user.service.UserService;
|
||||
import java.util.function.Supplier;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
@@ -20,13 +17,4 @@ public class EntityHelper {
|
||||
repository.delete(old);
|
||||
}
|
||||
}
|
||||
|
||||
public static <E extends SimpleEntity> E fillCreatorAndModifier(E entity, UserService service) {
|
||||
User user = service.currentLoginUser();
|
||||
if (ObjectUtil.isNull(entity.getCreatedUser())) {
|
||||
entity.setCreatedUser(user);
|
||||
}
|
||||
entity.setModifiedUser(user);
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user