From bac7a007e61fd19bcf251985719d630ef8353502 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Thu, 16 Feb 2023 15:42:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=20find=20=E6=96=B9=E6=B3=95=E7=9A=84?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E5=80=BC=E6=94=B9=E4=B8=BA=20Optional?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/DataNotExistException.java | 5 ----- .../xyz/zhouxy/plusone/domain/IRepository.java | 3 ++- .../plusone/jdbc/JdbcEntityDaoSupport.java | 13 +++++++------ .../plusone/jdbc/JdbcRepositorySupport.java | 5 +++-- .../controller/AccountManagementController.java | 2 -- .../service/AccountContextService.java | 17 +++++++++-------- .../service/AccountManagementService.java | 16 ++++++++++++---- .../application/service/AdminLoginService.java | 10 ++++------ .../service/DictManagementService.java | 7 ++++--- .../service/MenuManagementService.java | 11 ++++++----- .../service/RoleManagementService.java | 7 ++++--- .../domain/model/account/AccountRepository.java | 7 ++++--- .../model/account/AccountRepositoryImpl.java | 13 +++++++------ .../domain/model/dict/DictRepositoryImpl.java | 8 +++++--- .../domain/model/menu/MenuRepositoryImpl.java | 8 +++++--- .../domain/model/role/RoleRepositoryImpl.java | 8 +++++--- 16 files changed, 77 insertions(+), 63 deletions(-) diff --git a/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/exception/DataNotExistException.java b/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/exception/DataNotExistException.java index c727bc7..11a386d 100644 --- a/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/exception/DataNotExistException.java +++ b/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/exception/DataNotExistException.java @@ -6,12 +6,7 @@ import org.springframework.web.bind.annotation.ResponseStatus; /** * 需要时,当查询数据不存在时抛出的异常 * - *

- * 暂时先这样,后续完善异常体系时可能会更改。 - *

- * * @author ZhouXY - * @see xyz.zhouxy.plusone.util.AssertResult */ @ResponseStatus(HttpStatus.NOT_FOUND) public class DataNotExistException extends PlusoneException { diff --git a/plusone-basic/plusone-basic-domain/src/main/java/xyz/zhouxy/plusone/domain/IRepository.java b/plusone-basic/plusone-basic-domain/src/main/java/xyz/zhouxy/plusone/domain/IRepository.java index f913a4b..e73ae82 100644 --- a/plusone-basic/plusone-basic-domain/src/main/java/xyz/zhouxy/plusone/domain/IRepository.java +++ b/plusone-basic/plusone-basic-domain/src/main/java/xyz/zhouxy/plusone/domain/IRepository.java @@ -1,6 +1,7 @@ package xyz.zhouxy.plusone.domain; import java.io.Serializable; +import java.util.Optional; /** * Repository 基础接口 @@ -10,7 +11,7 @@ import java.io.Serializable; */ public interface IRepository, ID extends Serializable> { - T find(ID id); + Optional find(ID id); T save(T entity); diff --git a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/jdbc/JdbcEntityDaoSupport.java b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/jdbc/JdbcEntityDaoSupport.java index c779e90..8b0bf89 100644 --- a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/jdbc/JdbcEntityDaoSupport.java +++ b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/jdbc/JdbcEntityDaoSupport.java @@ -4,6 +4,7 @@ import java.io.Serializable; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; +import java.util.Optional; import java.util.stream.Stream; import javax.annotation.Nonnull; @@ -20,23 +21,23 @@ public abstract class JdbcEntityDaoSupport, ID extends Seri extends PlusoneJdbcDaoSupport { protected RowMapper rowMapper; - protected ResultSetExtractor resultSetExtractor; + protected ResultSetExtractor> resultSetExtractor; protected JdbcEntityDaoSupport(@Nonnull NamedParameterJdbcTemplate namedParameterJdbcTemplate) { super(namedParameterJdbcTemplate); this.rowMapper = (ResultSet rs, int rowNum) -> mapRow(rs); - this.resultSetExtractor = (ResultSet rs) -> rs.next() ? mapRow(rs) : null; + this.resultSetExtractor = (ResultSet rs) -> rs.next() ? Optional.of(mapRow(rs)) : Optional.empty(); } - protected final T queryForObject(String sql) { + protected final Optional queryForObject(String sql) { return queryForObject(sql, this.resultSetExtractor); } - protected final T queryForObject(String sql, SqlParameterSource paramSource) { + protected final Optional queryForObject(String sql, SqlParameterSource paramSource) { return queryForObject(sql, paramSource, this.resultSetExtractor); } - protected final T queryForObject(String sql, String paramName, Object value) { + protected final Optional queryForObject(String sql, String paramName, Object value) { return queryForObject(sql, new MapSqlParameterSource(paramName, value), this.resultSetExtractor); } @@ -66,7 +67,7 @@ public abstract class JdbcEntityDaoSupport, ID extends Seri this.rowMapper = rowMapper; } - protected void setResultSetExtractor(@Nonnull ResultSetExtractor resultSetExtractor) { + protected void setResultSetExtractor(@Nonnull ResultSetExtractor> resultSetExtractor) { this.resultSetExtractor = resultSetExtractor; } } diff --git a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/jdbc/JdbcRepositorySupport.java b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/jdbc/JdbcRepositorySupport.java index 3b44b60..4969790 100644 --- a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/jdbc/JdbcRepositorySupport.java +++ b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/jdbc/JdbcRepositorySupport.java @@ -1,6 +1,7 @@ package xyz.zhouxy.plusone.jdbc; import java.io.Serializable; +import java.util.Optional; import javax.annotation.Nonnull; @@ -20,7 +21,7 @@ public abstract class JdbcRepositorySupport, ID exte protected abstract void doDelete(@Nonnull T entity); - protected abstract T doFindById(@Nonnull ID id); + protected abstract Optional doFindById(@Nonnull ID id); protected abstract T doInsert(@Nonnull T entity); @@ -35,7 +36,7 @@ public abstract class JdbcRepositorySupport, ID exte } @Override - public final T find(ID id) { + public final Optional find(ID id) { if (id == null) { throw new IllegalArgumentException("Id cannot be null."); } diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/controller/AccountManagementController.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/controller/AccountManagementController.java index 8699d3d..261e3ee 100644 --- a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/controller/AccountManagementController.java +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/controller/AccountManagementController.java @@ -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); } } diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/AccountContextService.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/AccountContextService.java index ab98386..9e38002 100644 --- a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/AccountContextService.java +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/AccountContextService.java @@ -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 = 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()); } diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/AccountManagementService.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/AccountManagementService.java index e259d98..1d84ceb 100644 --- a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/AccountManagementService.java +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/AccountManagementService.java @@ -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 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); diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/AdminLoginService.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/AdminLoginService.java index 7606615..a898e48 100644 --- a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/AdminLoginService.java +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/AdminLoginService.java @@ -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()); diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/DictManagementService.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/DictManagementService.java index 28fba5b..625ec03 100644 --- a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/DictManagementService.java +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/DictManagementService.java @@ -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 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) diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/MenuManagementService.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/MenuManagementService.java index 4f2a86e..f7e5368 100644 --- a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/MenuManagementService.java +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/MenuManagementService.java @@ -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) diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/RoleManagementService.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/RoleManagementService.java index 23c68f7..9d88826 100644 --- a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/RoleManagementService.java +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/RoleManagementService.java @@ -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) diff --git a/plusone-system/plusone-system-domain/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/AccountRepository.java b/plusone-system/plusone-system-domain/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/AccountRepository.java index 3526f84..b4fc357 100644 --- a/plusone-system/plusone-system-domain/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/AccountRepository.java +++ b/plusone-system/plusone-system-domain/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/AccountRepository.java @@ -1,6 +1,7 @@ package xyz.zhouxy.plusone.system.domain.model.account; import java.util.Collection; +import java.util.Optional; import xyz.zhouxy.plusone.domain.IRepository; @@ -14,11 +15,11 @@ public interface AccountRepository extends IRepository { Collection findByRoleId(Long roleId); - Account findByEmail(Email email); + Optional findByEmail(Email email); - Account findByMobilePhone(MobilePhone mobilePhone); + Optional findByMobilePhone(MobilePhone mobilePhone); - Account findByUsername(Username username); + Optional findByUsername(Username username); boolean existsUsername(Username username); diff --git a/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/AccountRepositoryImpl.java b/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/AccountRepositoryImpl.java index 7156d63..c798296 100644 --- a/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/AccountRepositoryImpl.java +++ b/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/AccountRepositoryImpl.java @@ -5,6 +5,7 @@ import java.sql.SQLException; import java.time.LocalDateTime; import java.util.Collection; import java.util.Objects; +import java.util.Optional; import javax.annotation.Nonnull; @@ -44,7 +45,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport } @Override - protected final Account doFindById(@Nonnull Long id) { + protected final Optional doFindById(@Nonnull Long id) { return queryForObject(""" SELECT id, email, mobile_phone, username, "password", salt, avatar, sex, nickname, status, @@ -56,7 +57,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport } @Override - public Account findByEmail(Email email) { + public Optional findByEmail(Email email) { return queryForObject(""" SELECT id, email, mobile_phone, username, "password", salt, avatar, sex, nickname, status, @@ -68,7 +69,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport } @Override - public Account findByMobilePhone(MobilePhone mobilePhone) { + public Optional findByMobilePhone(MobilePhone mobilePhone) { return queryForObject(""" SELECT id, email, mobile_phone, username, "password", salt, avatar, sex, nickname, status, @@ -80,7 +81,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport } @Override - public Account findByUsername(Username username) { + public Optional findByUsername(Username username) { return queryForObject(""" SELECT id, email, mobile_phone, username, "password", salt, avatar, sex, nickname, status, @@ -143,7 +144,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport generateParamSource(id, entity)); assertUpdateOneRow(i); this.accountRoleDAO.insertAccountRoleRefs(id, entity.getRoleIds()); - return entity; + return find(id).orElseThrow(); } @Override @@ -167,7 +168,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport generateParamSource(entity)); assertUpdateOneRow(i); this.accountRoleDAO.saveAccountRoleRefs(entity); - return entity; + return find(entity.getId().orElseThrow()).orElseThrow(); } @Override diff --git a/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/dict/DictRepositoryImpl.java b/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/dict/DictRepositoryImpl.java index 08a428b..2bd38ee 100644 --- a/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/dict/DictRepositoryImpl.java +++ b/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/dict/DictRepositoryImpl.java @@ -6,6 +6,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.time.LocalDateTime; import java.util.List; +import java.util.Optional; import javax.annotation.Nonnull; @@ -15,6 +16,7 @@ import org.springframework.jdbc.core.namedparam.SqlParameterSource; import org.springframework.stereotype.Repository; import cn.hutool.core.util.IdUtil; +import xyz.zhouxy.plusone.exception.DataNotExistException; import xyz.zhouxy.plusone.jdbc.JdbcRepositorySupport; import xyz.zhouxy.plusone.util.AssertResult; @@ -34,7 +36,7 @@ public class DictRepositoryImpl extends JdbcRepositorySupport implem } @Override - public Dict doFindById(@Nonnull Long id) { + public Optional doFindById(@Nonnull Long id) { return queryForObject("SELECT id, dict_type, dict_label, \"version\" WHERE id = :id AND deleted = 0", "id", id); } @@ -49,7 +51,7 @@ public class DictRepositoryImpl extends JdbcRepositorySupport implem generateParamSource(id, entity)); AssertResult.updateOneRow(i); this.dictValueDAO.insertDictValues(id, entity); - return find(id); + return find(id).orElseThrow(DataNotExistException::new); } @Override @@ -66,7 +68,7 @@ public class DictRepositoryImpl extends JdbcRepositorySupport implem generateParamSource(entity)); AssertResult.updateOneRow(i); this.dictValueDAO.updateDictValues(entity); - return find(entity.getId().orElseThrow()); + return find(entity.getId().orElseThrow()).orElseThrow(DataNotExistException::new); } @Override diff --git a/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/menu/MenuRepositoryImpl.java b/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/menu/MenuRepositoryImpl.java index b2bea61..319b599 100644 --- a/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/menu/MenuRepositoryImpl.java +++ b/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/menu/MenuRepositoryImpl.java @@ -8,6 +8,7 @@ import java.time.LocalDateTime; import java.util.Collection; import java.util.Collections; import java.util.Objects; +import java.util.Optional; import javax.annotation.Nonnull; @@ -17,6 +18,7 @@ import org.springframework.stereotype.Repository; import cn.hutool.core.util.IdUtil; import xyz.zhouxy.plusone.constant.EntityStatus; +import xyz.zhouxy.plusone.exception.DataNotExistException; import xyz.zhouxy.plusone.jdbc.JdbcRepositorySupport; import xyz.zhouxy.plusone.system.domain.model.menu.Menu.MenuType; import xyz.zhouxy.plusone.util.AssertResult; @@ -38,7 +40,7 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport implem } @Override - protected final Menu doFindById(@Nonnull Long id) { + protected final Optional doFindById(@Nonnull Long id) { return queryForObject(""" SELECT id, parent_id, "type", "name", "path", title, icon, hidden, order_number, status, remarks, @@ -64,7 +66,7 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport implem int i = update(sql, paramSource); AssertResult.updateOneRow(i); this.actionDAO.saveActions(id, entity.getActions()); - return entity; + return find(id).orElseThrow(DataNotExistException::new); } @Override @@ -97,7 +99,7 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport implem // 保存权限 Long id = entity.getId().orElseThrow(); this.actionDAO.saveActions(id, entity.getActions()); - return entity; + return find(entity.getId().orElseThrow()).orElseThrow(DataNotExistException::new); } @Override diff --git a/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/role/RoleRepositoryImpl.java b/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/role/RoleRepositoryImpl.java index 8b08f6d..a80206b 100644 --- a/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/role/RoleRepositoryImpl.java +++ b/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/role/RoleRepositoryImpl.java @@ -6,6 +6,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.time.LocalDateTime; import java.util.Collection; +import java.util.Optional; import javax.annotation.Nonnull; @@ -15,6 +16,7 @@ import org.springframework.stereotype.Repository; import cn.hutool.core.util.IdUtil; import xyz.zhouxy.plusone.constant.EntityStatus; +import xyz.zhouxy.plusone.exception.DataNotExistException; import xyz.zhouxy.plusone.jdbc.JdbcRepositorySupport; import xyz.zhouxy.plusone.util.AssertResult; @@ -36,7 +38,7 @@ public class RoleRepositoryImpl extends JdbcRepositorySupport implem } @Override - protected final Role doFindById(@Nonnull Long id) { + protected final Optional doFindById(@Nonnull Long id) { return queryForObject(""" SELECT "id","name","identifier","status","remarks","version" FROM "sys_role" @@ -74,7 +76,7 @@ public class RoleRepositoryImpl extends JdbcRepositorySupport implem AssertResult.updateOneRow(i); this.roleMenuRefDAO.saveRoleMenuRefs(id, entity); this.rolePermissionRefDAO.saveRolePermissionRefs(id, entity); - return entity; + return find(id).orElseThrow(DataNotExistException::new); } @Override @@ -97,7 +99,7 @@ public class RoleRepositoryImpl extends JdbcRepositorySupport implem this.roleMenuRefDAO.saveRoleMenuRefs(id, entity); this.rolePermissionRefDAO.clearRolePermissionRefs(entity); this.rolePermissionRefDAO.saveRolePermissionRefs(id, entity); - return entity; + return find(entity.getId().orElseThrow()).orElseThrow(DataNotExistException::new); } @Override