|
|
@@ -5,8 +5,7 @@ import cn.seecoder.seecoderportalcommon.vo.UserVO;
|
|
|
import cn.seecoder.seecoderportalserver.domain.User;
|
|
|
import cn.seecoder.seecoderportalserver.mapper.UserMapper;
|
|
|
import cn.seecoder.seecoderportalserver.repository.UserRepository;
|
|
|
-import cn.seecoder.seecoderportalserver.web.rest.errors.PhoneAlreadyUsedException;
|
|
|
-import cn.seecoder.seecoderportalserver.web.rest.errors.PhoneVerificationNotMatchException;
|
|
|
+import cn.seecoder.seecoderportalserver.web.rest.errors.CustomErrorException;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
|
@@ -42,24 +41,52 @@ public class UserService {
|
|
|
return userRepository.findByRole(role);
|
|
|
}
|
|
|
|
|
|
- public User updateUserValid(Long userId, Boolean isValid) {
|
|
|
+ @Transactional(rollbackFor = RuntimeException.class)
|
|
|
+ public User updateUserName (Long userId, String name) {
|
|
|
return userRepository.findById(userId)
|
|
|
+ .map(user -> {
|
|
|
+ user.setName(name);
|
|
|
+ return userRepository.save(user);
|
|
|
+ }).orElseThrow(CustomErrorException::userIdNotFound);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = RuntimeException.class)
|
|
|
+ public User updateUserEmail (Long userId, String email) {
|
|
|
+ return userRepository.findById(userId)
|
|
|
+ .map(user -> {
|
|
|
+ user.setEmail(email);
|
|
|
+ return userRepository.save(user);
|
|
|
+ }).orElseThrow(CustomErrorException::userIdNotFound);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = RuntimeException.class)
|
|
|
+ public User updateUserSignature (Long userId, String signature) {
|
|
|
+ return userRepository.findById(userId)
|
|
|
+ .map(user -> {
|
|
|
+ user.setSignature(signature);
|
|
|
+ return userRepository.save(user);
|
|
|
+ }).orElseThrow(CustomErrorException::userIdNotFound);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = RuntimeException.class)
|
|
|
+ public void updateUserValid(Long userId, Boolean isValid) {
|
|
|
+ userRepository.findById(userId)
|
|
|
.map(user -> {
|
|
|
user.setValid(isValid);
|
|
|
return userRepository.save(user);
|
|
|
- }).orElseThrow(()->new UsernameNotFoundException("找不到用户"));
|
|
|
+ }).orElseThrow(CustomErrorException::userIdNotFound);
|
|
|
}
|
|
|
|
|
|
@Transactional(rollbackFor = RuntimeException.class)
|
|
|
- public User registerUser(UserVO userDTO, String password, String phoneCode) {
|
|
|
- checkPhoneMessageOrThrowError(userDTO.getPhone(), phoneCode);
|
|
|
+ public User registerUser(UserVO userVO, String password, String phoneCode) {
|
|
|
+ checkPhoneMessageOrThrowError(userVO.getPhone(), phoneCode);
|
|
|
|
|
|
userRepository
|
|
|
- .findByPhone(userDTO.getPhone())
|
|
|
+ .findByPhone(userVO.getPhone())
|
|
|
.ifPresent(user -> {
|
|
|
- throw new PhoneAlreadyUsedException();
|
|
|
+ throw CustomErrorException.phoneAlreadyExist();
|
|
|
});
|
|
|
- User user = userMapper.userVOToUser(userDTO);
|
|
|
+ User user = userMapper.userVOToUser(userVO);
|
|
|
String encodedPassword = passwordEncoder.encode(password);
|
|
|
user.setPassword(encodedPassword);
|
|
|
User savedUser = userRepository.save(user);
|
|
|
@@ -67,13 +94,14 @@ public class UserService {
|
|
|
return savedUser;
|
|
|
}
|
|
|
|
|
|
- @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Transactional(rollbackFor = RuntimeException.class)
|
|
|
public User resetPassword(String phone, String phoneCode, String password) {
|
|
|
checkPhoneMessageOrThrowError(phone, phoneCode);
|
|
|
|
|
|
User user = userRepository
|
|
|
.findByPhone(phone)
|
|
|
.<UsernameNotFoundException>orElseThrow(() -> {
|
|
|
+ // TODO: use customException
|
|
|
throw new UsernameNotFoundException("找不到手机用户[" + phone + "]");
|
|
|
});
|
|
|
user.setPassword(passwordEncoder.encode(password));
|
|
|
@@ -84,7 +112,13 @@ public class UserService {
|
|
|
|
|
|
private void checkPhoneMessageOrThrowError(String phone, String phoneCode) {
|
|
|
if (!aliService.checkPhoneMessage(phone, phoneCode)) {
|
|
|
- throw new PhoneVerificationNotMatchException();
|
|
|
+ throw CustomErrorException.phoneVerificationNotMatch();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public Long findIdByPhone(String phone) {
|
|
|
+ return userRepository.findByPhone(phone)
|
|
|
+ .map(user -> user.getId())
|
|
|
+ .orElseThrow(CustomErrorException::userIdNotFound);
|
|
|
+ }
|
|
|
}
|