将 find 方法的返回值改为 Optional<T>。

This commit is contained in:
2023-02-16 15:42:30 +08:00
parent 02d918d0a4
commit bac7a007e6
16 changed files with 77 additions and 63 deletions

View File

@@ -20,7 +20,6 @@ import xyz.zhouxy.plusone.system.application.query.params.AccountQueryParams;
import xyz.zhouxy.plusone.system.application.service.AccountManagementService;
import xyz.zhouxy.plusone.system.application.service.command.CreateAccountCommand;
import xyz.zhouxy.plusone.system.application.service.command.UpdateAccountCommand;
import xyz.zhouxy.plusone.util.AssertResult;
import xyz.zhouxy.plusone.util.RestfulResult;
/**
@@ -77,7 +76,6 @@ public class AccountManagementController {
adminAuthLogic.checkLogin();
adminAuthLogic.checkPermission("sys-account-details");
var accountDetails = service.queryAccountDetails(accountId);
AssertResult.nonNull(accountDetails);
return success("查询成功", accountDetails);
}
}

View File

@@ -1,8 +1,7 @@
package xyz.zhouxy.plusone.system.application.service;
import xyz.zhouxy.plusone.system.constant.AuthLogic;
import java.util.List;
import java.util.Optional;
import javax.annotation.Resource;
@@ -10,7 +9,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.dev33.satoken.stp.StpLogic;
import cn.hutool.core.lang.Assert;
import xyz.zhouxy.plusone.system.application.common.util.PrincipalUtil;
import xyz.zhouxy.plusone.system.application.exception.AccountLoginException;
import xyz.zhouxy.plusone.system.application.query.AccountQueries;
@@ -19,6 +17,7 @@ import xyz.zhouxy.plusone.system.application.query.result.MenuViewObject;
import xyz.zhouxy.plusone.system.application.service.command.ChangePasswordByOtpCommand;
import xyz.zhouxy.plusone.system.application.service.command.ChangePasswordCommand;
import xyz.zhouxy.plusone.system.application.service.command.ChangePasswordWithoutLoginCommand;
import xyz.zhouxy.plusone.system.constant.AuthLogic;
import xyz.zhouxy.plusone.system.domain.model.account.Account;
import xyz.zhouxy.plusone.system.domain.model.account.AccountRepository;
import xyz.zhouxy.plusone.system.domain.model.account.Email;
@@ -65,7 +64,8 @@ public class AccountContextService {
@Transactional
public void changePassword(ChangePasswordCommand command) {
adminAuthLogic.checkLogin();
Account account = accountRepository.find(adminAuthLogic.getLoginIdAsLong());
Account account = accountRepository.find(adminAuthLogic.getLoginIdAsLong())
.orElseThrow(() -> AccountLoginException.accountNotExistException("当前所登录的账号不存在"));
account.checkPassword(command.getPassword());
account.changePassword(command.getNewPassword(), command.getPasswordConfirmation());
accountRepository.save(account);
@@ -77,9 +77,10 @@ public class AccountContextService {
String principal = command.getAccount();
Principal emailOrMobilePhone = PrincipalUtil.getEmailOrMobilePhone(principal);
Account account = emailOrMobilePhone instanceof Email
Account account = (emailOrMobilePhone instanceof Email
? accountRepository.findByEmail((Email) emailOrMobilePhone)
: accountRepository.findByMobilePhone((MobilePhone) emailOrMobilePhone);
: accountRepository.findByMobilePhone((MobilePhone) emailOrMobilePhone))
.orElseThrow(() -> AccountLoginException.accountNotExistException("当前所登录的账号不存在"));
account.checkPassword(command.getOldPassword());
account.changePassword(command.getNewPassword(), command.getPasswordConfirmation());
accountRepository.save(account);
@@ -90,12 +91,12 @@ public class AccountContextService {
public void changePasswordByOtp(ChangePasswordByOtpCommand command) {
var principal = command.getAccount();
Account account = switch (command.getPrincipalType()) {
Optional<Account> account = switch (command.getPrincipalType()) {
case EMAIL -> accountRepository.findByEmail(Email.of(principal));
case MOBILE_PHONE -> accountRepository.findByMobilePhone(MobilePhone.of(principal));
default -> throw InvalidInputException.unsupportedPrincipalTypeException("输入邮箱地址或手机号");
};
Assert.notNull(account, AccountLoginException::accountNotExistException);
account.orElseThrow(AccountLoginException::accountNotExistException);
mailAndSmsVerifyService.checkOtp(principal, command.getOtp());
}

View File

@@ -14,6 +14,7 @@ import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import xyz.zhouxy.plusone.exception.DataNotExistException;
import xyz.zhouxy.plusone.system.application.exception.AccountRegisterException;
import xyz.zhouxy.plusone.system.application.query.AccountQueries;
import xyz.zhouxy.plusone.system.application.query.params.AccountQueryParams;
@@ -76,16 +77,16 @@ public class AccountManagementService {
public void deleteAccounts(List<Long> ids) {
Account accountToDelete;
for (var id : ids) {
accountToDelete = accountRepository.find(id);
AssertResult.nonNull(accountToDelete);
accountToDelete = accountRepository.find(id)
.orElseThrow(() -> new DataNotExistException("该账号不存在"));
accountRepository.delete(accountToDelete);
}
}
public void updateAccountInfo(Long id, @Valid UpdateAccountCommand command) {
Assert.isTrue(Objects.equals(id, command.getId()), "参数错误: id 不匹配");
Account account = accountRepository.find(id);
AssertResult.nonNull(account, "该账号不存在");
Account account = accountRepository.find(id)
.orElseThrow(() -> new DataNotExistException("该账号不存在"));
account.setAccountInfo(command.getNickname(), command.getAvatar(), command.getSex());
account.setUpdatedBy(adminAuthLogic.getLoginIdAsLong());
accountRepository.save(account);
@@ -96,6 +97,13 @@ public class AccountManagementService {
return accountQueries.queryAccountOverviewPage(queryParams);
}
/**
* 查询账号详细信息,如果查不到将抛出 {@link DataNotExistException}。
*
* @param accountId 账号 id
* @return 账号信息
* @throws DataNotExistException 查询不到数据时抛出异常
*/
@Transactional(propagation = Propagation.SUPPORTS)
public AccountDetails queryAccountDetails(@PathVariable("accountId") Long accountId) {
var accountDetails = accountQueries.queryAccountDetails(accountId);

View File

@@ -44,12 +44,11 @@ public class AdminLoginService {
@ValidateDto
public LoginInfoViewObject loginByPassword(LoginByPasswordCommand command) {
var principal = command.getPrincipal();
Account account = switch (command.getPrincipalType()) {
Account account = (switch (command.getPrincipalType()) {
case USERNAME -> accountRepository.findByUsername(Username.of(principal));
case EMAIL -> accountRepository.findByEmail(Email.of(principal));
case MOBILE_PHONE -> accountRepository.findByMobilePhone(MobilePhone.of(principal));
};
Assert.notNull(account, AccountLoginException::accountNotExistException);
}).orElseThrow(AccountLoginException::accountNotExistException);
var isPasswordCorrect = account.checkPassword(command.getPassword());
Assert.isTrue(isPasswordCorrect, AccountLoginException::passwordErrorException);
@@ -61,12 +60,11 @@ public class AdminLoginService {
@ValidateDto
public LoginInfoViewObject loginByOtp(LoginByOtpCommand command) {
var principal = command.getPrincipal();
Account account = switch (command.getPrincipalType()) {
Account account = (switch (command.getPrincipalType()) {
case EMAIL -> accountRepository.findByEmail(Email.of(principal));
case MOBILE_PHONE -> accountRepository.findByMobilePhone(MobilePhone.of(principal));
default -> throw InvalidInputException.unsupportedPrincipalTypeException("输入邮箱地址或手机号");
};
Assert.notNull(account, AccountLoginException::accountNotExistException);
}).orElseThrow(AccountLoginException::accountNotExistException);
mailAndSmsVerifyService.checkOtp(principal, command.getOtp());

View File

@@ -10,6 +10,7 @@ import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import xyz.zhouxy.plusone.exception.DataNotExistException;
import xyz.zhouxy.plusone.system.application.query.DictQueries;
import xyz.zhouxy.plusone.system.application.query.params.DictQueryParams;
import xyz.zhouxy.plusone.system.application.query.result.DictOverview;
@@ -45,21 +46,21 @@ public class DictManagementService {
public void deleteDicts(List<Long> ids) {
Dict dictToDelete;
for (Long id : ids) {
dictToDelete = dictRepository.find(id);
dictToDelete = dictRepository.find(id).orElseThrow(DataNotExistException::new);
dictRepository.delete(dictToDelete);
}
}
public void updateDict(Long id, @Valid UpdateDictCommand command) {
Assert.isTrue(Objects.equals(id, command.getId()), "id 不匹配");
Dict dictToUpdate = dictRepository.find(command.getId());
Dict dictToUpdate = dictRepository.find(command.getId()).orElseThrow(DataNotExistException::new);
dictToUpdate.updateDict(command.getDictType(), command.getDictLabel(), command.getKeyLabelMap());
dictRepository.save(dictToUpdate);
}
@Transactional(propagation = Propagation.SUPPORTS)
public Dict findDictDetails(Long dictId) {
return dictRepository.find(dictId);
return dictRepository.find(dictId).orElseThrow(DataNotExistException::new);
}
@Transactional(propagation = Propagation.SUPPORTS)

View File

@@ -14,6 +14,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import xyz.zhouxy.plusone.domain.IWithOrderNumber;
import xyz.zhouxy.plusone.exception.DataNotExistException;
import xyz.zhouxy.plusone.system.application.exception.UnsupportedMenuTypeException;
import xyz.zhouxy.plusone.system.application.query.result.MenuViewObject;
import xyz.zhouxy.plusone.system.application.service.command.CreateMenuCommand;
@@ -22,7 +23,6 @@ import xyz.zhouxy.plusone.system.domain.model.menu.Menu;
import xyz.zhouxy.plusone.system.domain.model.menu.MenuConstructor;
import xyz.zhouxy.plusone.system.domain.model.menu.MenuRepository;
import xyz.zhouxy.plusone.system.domain.service.MenuService;
import xyz.zhouxy.plusone.util.AssertResult;
/**
* 菜单管理
@@ -88,15 +88,16 @@ public class MenuManagementService {
// ==================== delete ====================
public void deleteMenu(Long id) {
Menu menuToDelete = menuRepository.find(id);
AssertResult.nonNull(menuToDelete);
Menu menuToDelete = menuRepository.find(id)
.orElseThrow(DataNotExistException::new);
menuRepository.delete(menuToDelete);
}
// ==================== update ====================
public void updateMenu(Long id, @Valid UpdateMenuCommand command) {
Assert.isTrue(Objects.equals(id, command.getId()), "id 不匹配");
Menu menuToUpdate = menuRepository.find(command.getId());
Menu menuToUpdate = menuRepository.find(command.getId())
.orElseThrow(DataNotExistException::new);
menuToUpdate.updateMenuInfo(
command.getMenuType(),
command.getParentId(),
@@ -118,7 +119,7 @@ public class MenuManagementService {
@Transactional(propagation = Propagation.SUPPORTS)
public MenuViewObject findById(Long id) {
var menu = menuRepository.find(id);
return MenuViewObject.of(menu);
return MenuViewObject.of(menu.orElseThrow(DataNotExistException::new));
}
@Transactional(propagation = Propagation.SUPPORTS)

View File

@@ -10,6 +10,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouxy.plusone.exception.DataNotExistException;
import xyz.zhouxy.plusone.system.application.query.RoleQueries;
import xyz.zhouxy.plusone.system.application.query.params.RoleQueryParams;
import xyz.zhouxy.plusone.system.application.query.result.RoleOverview;
@@ -62,7 +63,7 @@ public class RoleManagementService {
public void updateRole(@Valid UpdateRoleCommand command) {
Long roleId = command.getId();
Role roleToUpdate = _roleRepository.find(roleId);
Role roleToUpdate = _roleRepository.find(roleId).orElseThrow(DataNotExistException::new);
roleToUpdate.update(
command.getName(),
command.getIdentifier(),
@@ -74,7 +75,7 @@ public class RoleManagementService {
}
public void delete(Long id) {
Role role = _roleRepository.find(id);
Role role = _roleRepository.find(id).orElseThrow(DataNotExistException::new);
_roleRepository.delete(role);
}
@@ -85,7 +86,7 @@ public class RoleManagementService {
@Transactional(propagation = Propagation.SUPPORTS)
public Role findById(Long id) {
return _roleRepository.find(id);
return _roleRepository.find(id).orElseThrow(DataNotExistException::new);
}
@Transactional(propagation = Propagation.SUPPORTS)