Compare commits

...

4 Commits

18 changed files with 108 additions and 80 deletions

View File

@ -6,12 +6,7 @@ import org.springframework.web.bind.annotation.ResponseStatus;
/** /**
* *
* *
* <p>
*
* </p>
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a> * @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
* @see xyz.zhouxy.plusone.util.AssertResult
*/ */
@ResponseStatus(HttpStatus.NOT_FOUND) @ResponseStatus(HttpStatus.NOT_FOUND)
public class DataNotExistException extends PlusoneException { public class DataNotExistException extends PlusoneException {

View File

@ -1,6 +1,7 @@
package xyz.zhouxy.plusone.domain; package xyz.zhouxy.plusone.domain;
import java.io.Serializable; import java.io.Serializable;
import java.util.Optional;
/** /**
* Repository * Repository
@ -10,7 +11,7 @@ import java.io.Serializable;
*/ */
public interface IRepository<T extends AggregateRoot<ID>, ID extends Serializable> { public interface IRepository<T extends AggregateRoot<ID>, ID extends Serializable> {
T find(ID id); Optional<T> find(ID id);
T save(T entity); T save(T entity);

View File

@ -4,6 +4,7 @@ import java.io.Serializable;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.stream.Stream; import java.util.stream.Stream;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -20,23 +21,23 @@ public abstract class JdbcEntityDaoSupport<T extends Entity<ID>, ID extends Seri
extends PlusoneJdbcDaoSupport { extends PlusoneJdbcDaoSupport {
protected RowMapper<T> rowMapper; protected RowMapper<T> rowMapper;
protected ResultSetExtractor<T> resultSetExtractor; protected ResultSetExtractor<Optional<T>> resultSetExtractor;
protected JdbcEntityDaoSupport(@Nonnull NamedParameterJdbcTemplate namedParameterJdbcTemplate) { protected JdbcEntityDaoSupport(@Nonnull NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
super(namedParameterJdbcTemplate); super(namedParameterJdbcTemplate);
this.rowMapper = (ResultSet rs, int rowNum) -> mapRow(rs); 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<T> queryForObject(String sql) {
return queryForObject(sql, this.resultSetExtractor); return queryForObject(sql, this.resultSetExtractor);
} }
protected final T queryForObject(String sql, SqlParameterSource paramSource) { protected final Optional<T> queryForObject(String sql, SqlParameterSource paramSource) {
return queryForObject(sql, paramSource, this.resultSetExtractor); return queryForObject(sql, paramSource, this.resultSetExtractor);
} }
protected final T queryForObject(String sql, String paramName, Object value) { protected final Optional<T> queryForObject(String sql, String paramName, Object value) {
return queryForObject(sql, new MapSqlParameterSource(paramName, value), this.resultSetExtractor); return queryForObject(sql, new MapSqlParameterSource(paramName, value), this.resultSetExtractor);
} }
@ -66,7 +67,7 @@ public abstract class JdbcEntityDaoSupport<T extends Entity<ID>, ID extends Seri
this.rowMapper = rowMapper; this.rowMapper = rowMapper;
} }
protected void setResultSetExtractor(@Nonnull ResultSetExtractor<T> resultSetExtractor) { protected void setResultSetExtractor(@Nonnull ResultSetExtractor<Optional<T>> resultSetExtractor) {
this.resultSetExtractor = resultSetExtractor; this.resultSetExtractor = resultSetExtractor;
} }
} }

View File

@ -1,6 +1,7 @@
package xyz.zhouxy.plusone.jdbc; package xyz.zhouxy.plusone.jdbc;
import java.io.Serializable; import java.io.Serializable;
import java.util.Optional;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -20,7 +21,7 @@ public abstract class JdbcRepositorySupport<T extends AggregateRoot<ID>, ID exte
protected abstract void doDelete(@Nonnull T entity); protected abstract void doDelete(@Nonnull T entity);
protected abstract T doFindById(@Nonnull ID id); protected abstract Optional<T> doFindById(@Nonnull ID id);
protected abstract T doInsert(@Nonnull T entity); protected abstract T doInsert(@Nonnull T entity);
@ -35,7 +36,7 @@ public abstract class JdbcRepositorySupport<T extends AggregateRoot<ID>, ID exte
} }
@Override @Override
public final T find(ID id) { public final Optional<T> find(ID id) {
if (id == null) { if (id == null) {
throw new IllegalArgumentException("Id cannot be null."); throw new IllegalArgumentException("Id cannot be null.");
} }

View File

@ -1,6 +1,5 @@
package xyz.zhouxy.plusone.jdbc; package xyz.zhouxy.plusone.jdbc;
import java.sql.ResultSet;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -66,17 +65,16 @@ public abstract class PlusoneJdbcDaoSupport {
} }
protected final <T> Stream<T> queryForStream(String sql, String paramName, Object value, Class<T> elementType) { protected final <T> Stream<T> queryForStream(String sql, String paramName, Object value, Class<T> elementType) {
return this.jdbc.queryForList(sql, new MapSqlParameterSource(paramName, value), elementType).stream(); return queryForStream(sql, new MapSqlParameterSource(paramName, value), elementType);
} }
protected final boolean queryExists(String sql, SqlParameterSource parameterSource) { protected final boolean queryExists(String sql, SqlParameterSource parameterSource) {
Boolean isExists = this.jdbc.query(sql, parameterSource, ResultSet::next); Boolean isExists = this.jdbc.queryForObject(sql, parameterSource, Boolean.TYPE);
return Boolean.TRUE.equals(isExists); return Boolean.TRUE.equals(isExists);
} }
protected final boolean queryExists(String sql, String paramName, Object value) { protected final boolean queryExists(String sql, String paramName, Object value) {
Boolean isExists = this.jdbc.query(sql, new MapSqlParameterSource(paramName, value), ResultSet::next); return queryExists(sql, new MapSqlParameterSource(paramName, value));
return Boolean.TRUE.equals(isExists);
} }
protected final int update(String sql, SqlParameterSource parameterSource) { protected final int update(String sql, SqlParameterSource parameterSource) {
@ -84,7 +82,7 @@ public abstract class PlusoneJdbcDaoSupport {
} }
protected final int update(String sql, String paramName, Object value) { protected final int update(String sql, String paramName, Object value) {
return this.jdbc.update(sql, new MapSqlParameterSource(paramName, value)); return update(sql, new MapSqlParameterSource(paramName, value));
} }
protected final int batchUpdate(String sql, SqlParameterSource[] batchArgs) { protected final int batchUpdate(String sql, SqlParameterSource[] batchArgs) {

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

View File

@ -21,18 +21,34 @@ public class AccountLoginException extends PlusoneException {
} }
public static AccountLoginException accountNotExistException() { public static AccountLoginException accountNotExistException() {
return new AccountLoginException(4030101, "用户账户不存在"); return accountNotExistException("用户账户不存在");
}
public static AccountLoginException accountNotExistException(String msg) {
return new AccountLoginException(4030101, msg);
} }
public static AccountLoginException otpErrorException() { public static AccountLoginException otpErrorException() {
return new AccountLoginException(4030501, "验证码错误"); return otpErrorException("验证码错误");
}
public static AccountLoginException otpErrorException(String msg) {
return new AccountLoginException(4030501, msg);
} }
public static AccountLoginException otpNotExistsException() { public static AccountLoginException otpNotExistsException() {
return new AccountLoginException(4030502, "验证码不存在或已过期"); return otpNotExistsException("验证码不存在或已过期");
}
public static AccountLoginException otpNotExistsException(String msg) {
return new AccountLoginException(4030502, msg);
} }
public static AccountLoginException passwordErrorException() { public static AccountLoginException passwordErrorException() {
return new AccountLoginException(4030200, "用户密码错误"); return passwordErrorException("用户密码错误");
}
public static AccountLoginException passwordErrorException(String msg) {
return new AccountLoginException(4030200, msg);
} }
} }

View File

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

View File

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

View File

@ -44,12 +44,11 @@ public class AdminLoginService {
@ValidateDto @ValidateDto
public LoginInfoViewObject loginByPassword(LoginByPasswordCommand command) { public LoginInfoViewObject loginByPassword(LoginByPasswordCommand command) {
var principal = command.getPrincipal(); var principal = command.getPrincipal();
Account account = switch (command.getPrincipalType()) { Account account = (switch (command.getPrincipalType()) {
case USERNAME -> accountRepository.findByUsername(Username.of(principal)); case USERNAME -> accountRepository.findByUsername(Username.of(principal));
case EMAIL -> accountRepository.findByEmail(Email.of(principal)); case EMAIL -> accountRepository.findByEmail(Email.of(principal));
case MOBILE_PHONE -> accountRepository.findByMobilePhone(MobilePhone.of(principal)); case MOBILE_PHONE -> accountRepository.findByMobilePhone(MobilePhone.of(principal));
}; }).orElseThrow(AccountLoginException::accountNotExistException);
Assert.notNull(account, AccountLoginException::accountNotExistException);
var isPasswordCorrect = account.checkPassword(command.getPassword()); var isPasswordCorrect = account.checkPassword(command.getPassword());
Assert.isTrue(isPasswordCorrect, AccountLoginException::passwordErrorException); Assert.isTrue(isPasswordCorrect, AccountLoginException::passwordErrorException);
@ -61,12 +60,11 @@ public class AdminLoginService {
@ValidateDto @ValidateDto
public LoginInfoViewObject loginByOtp(LoginByOtpCommand command) { public LoginInfoViewObject loginByOtp(LoginByOtpCommand command) {
var principal = command.getPrincipal(); var principal = command.getPrincipal();
Account account = switch (command.getPrincipalType()) { Account account = (switch (command.getPrincipalType()) {
case EMAIL -> accountRepository.findByEmail(Email.of(principal)); case EMAIL -> accountRepository.findByEmail(Email.of(principal));
case MOBILE_PHONE -> accountRepository.findByMobilePhone(MobilePhone.of(principal)); case MOBILE_PHONE -> accountRepository.findByMobilePhone(MobilePhone.of(principal));
default -> throw InvalidInputException.unsupportedPrincipalTypeException("输入邮箱地址或手机号"); default -> throw InvalidInputException.unsupportedPrincipalTypeException("输入邮箱地址或手机号");
}; }).orElseThrow(AccountLoginException::accountNotExistException);
Assert.notNull(account, AccountLoginException::accountNotExistException);
mailAndSmsVerifyService.checkOtp(principal, command.getOtp()); 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.transaction.annotation.Transactional;
import org.springframework.util.Assert; 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.DictQueries;
import xyz.zhouxy.plusone.system.application.query.params.DictQueryParams; import xyz.zhouxy.plusone.system.application.query.params.DictQueryParams;
import xyz.zhouxy.plusone.system.application.query.result.DictOverview; import xyz.zhouxy.plusone.system.application.query.result.DictOverview;
@ -45,21 +46,21 @@ public class DictManagementService {
public void deleteDicts(List<Long> ids) { public void deleteDicts(List<Long> ids) {
Dict dictToDelete; Dict dictToDelete;
for (Long id : ids) { for (Long id : ids) {
dictToDelete = dictRepository.find(id); dictToDelete = dictRepository.find(id).orElseThrow(DataNotExistException::new);
dictRepository.delete(dictToDelete); dictRepository.delete(dictToDelete);
} }
} }
public void updateDict(Long id, @Valid UpdateDictCommand command) { public void updateDict(Long id, @Valid UpdateDictCommand command) {
Assert.isTrue(Objects.equals(id, command.getId()), "id 不匹配"); 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()); dictToUpdate.updateDict(command.getDictType(), command.getDictLabel(), command.getKeyLabelMap());
dictRepository.save(dictToUpdate); dictRepository.save(dictToUpdate);
} }
@Transactional(propagation = Propagation.SUPPORTS) @Transactional(propagation = Propagation.SUPPORTS)
public Dict findDictDetails(Long dictId) { public Dict findDictDetails(Long dictId) {
return dictRepository.find(dictId); return dictRepository.find(dictId).orElseThrow(DataNotExistException::new);
} }
@Transactional(propagation = Propagation.SUPPORTS) @Transactional(propagation = Propagation.SUPPORTS)

View File

@ -14,6 +14,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import xyz.zhouxy.plusone.domain.IWithOrderNumber; 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.exception.UnsupportedMenuTypeException;
import xyz.zhouxy.plusone.system.application.query.result.MenuViewObject; import xyz.zhouxy.plusone.system.application.query.result.MenuViewObject;
import xyz.zhouxy.plusone.system.application.service.command.CreateMenuCommand; 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.MenuConstructor;
import xyz.zhouxy.plusone.system.domain.model.menu.MenuRepository; import xyz.zhouxy.plusone.system.domain.model.menu.MenuRepository;
import xyz.zhouxy.plusone.system.domain.service.MenuService; import xyz.zhouxy.plusone.system.domain.service.MenuService;
import xyz.zhouxy.plusone.util.AssertResult;
/** /**
* *
@ -88,15 +88,16 @@ public class MenuManagementService {
// ==================== delete ==================== // ==================== delete ====================
public void deleteMenu(Long id) { public void deleteMenu(Long id) {
Menu menuToDelete = menuRepository.find(id); Menu menuToDelete = menuRepository.find(id)
AssertResult.nonNull(menuToDelete); .orElseThrow(DataNotExistException::new);
menuRepository.delete(menuToDelete); menuRepository.delete(menuToDelete);
} }
// ==================== update ==================== // ==================== update ====================
public void updateMenu(Long id, @Valid UpdateMenuCommand command) { public void updateMenu(Long id, @Valid UpdateMenuCommand command) {
Assert.isTrue(Objects.equals(id, command.getId()), "id 不匹配"); 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( menuToUpdate.updateMenuInfo(
command.getMenuType(), command.getMenuType(),
command.getParentId(), command.getParentId(),
@ -118,7 +119,7 @@ public class MenuManagementService {
@Transactional(propagation = Propagation.SUPPORTS) @Transactional(propagation = Propagation.SUPPORTS)
public MenuViewObject findById(Long id) { public MenuViewObject findById(Long id) {
var menu = menuRepository.find(id); var menu = menuRepository.find(id);
return MenuViewObject.of(menu); return MenuViewObject.of(menu.orElseThrow(DataNotExistException::new));
} }
@Transactional(propagation = Propagation.SUPPORTS) @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.Propagation;
import org.springframework.transaction.annotation.Transactional; 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.RoleQueries;
import xyz.zhouxy.plusone.system.application.query.params.RoleQueryParams; import xyz.zhouxy.plusone.system.application.query.params.RoleQueryParams;
import xyz.zhouxy.plusone.system.application.query.result.RoleOverview; import xyz.zhouxy.plusone.system.application.query.result.RoleOverview;
@ -62,7 +63,7 @@ public class RoleManagementService {
public void updateRole(@Valid UpdateRoleCommand command) { public void updateRole(@Valid UpdateRoleCommand command) {
Long roleId = command.getId(); Long roleId = command.getId();
Role roleToUpdate = _roleRepository.find(roleId); Role roleToUpdate = _roleRepository.find(roleId).orElseThrow(DataNotExistException::new);
roleToUpdate.update( roleToUpdate.update(
command.getName(), command.getName(),
command.getIdentifier(), command.getIdentifier(),
@ -74,7 +75,7 @@ public class RoleManagementService {
} }
public void delete(Long id) { public void delete(Long id) {
Role role = _roleRepository.find(id); Role role = _roleRepository.find(id).orElseThrow(DataNotExistException::new);
_roleRepository.delete(role); _roleRepository.delete(role);
} }
@ -85,7 +86,7 @@ public class RoleManagementService {
@Transactional(propagation = Propagation.SUPPORTS) @Transactional(propagation = Propagation.SUPPORTS)
public Role findById(Long id) { public Role findById(Long id) {
return _roleRepository.find(id); return _roleRepository.find(id).orElseThrow(DataNotExistException::new);
} }
@Transactional(propagation = Propagation.SUPPORTS) @Transactional(propagation = Propagation.SUPPORTS)

View File

@ -1,6 +1,7 @@
package xyz.zhouxy.plusone.system.domain.model.account; package xyz.zhouxy.plusone.system.domain.model.account;
import java.util.Collection; import java.util.Collection;
import java.util.Optional;
import xyz.zhouxy.plusone.domain.IRepository; import xyz.zhouxy.plusone.domain.IRepository;
@ -14,11 +15,11 @@ public interface AccountRepository extends IRepository<Account, Long> {
Collection<Account> findByRoleId(Long roleId); Collection<Account> findByRoleId(Long roleId);
Account findByEmail(Email email); Optional<Account> findByEmail(Email email);
Account findByMobilePhone(MobilePhone mobilePhone); Optional<Account> findByMobilePhone(MobilePhone mobilePhone);
Account findByUsername(Username username); Optional<Account> findByUsername(Username username);
boolean existsUsername(Username username); boolean existsUsername(Username username);

View File

@ -5,6 +5,7 @@ import java.sql.SQLException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Collection; import java.util.Collection;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -44,7 +45,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
} }
@Override @Override
protected final Account doFindById(@Nonnull Long id) { protected final Optional<Account> doFindById(@Nonnull Long id) {
return queryForObject(""" return queryForObject("""
SELECT SELECT
id, email, mobile_phone, username, "password", salt, avatar, sex, nickname, status, id, email, mobile_phone, username, "password", salt, avatar, sex, nickname, status,
@ -56,7 +57,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
} }
@Override @Override
public Account findByEmail(Email email) { public Optional<Account> findByEmail(Email email) {
return queryForObject(""" return queryForObject("""
SELECT SELECT
id, email, mobile_phone, username, "password", salt, avatar, sex, nickname, status, id, email, mobile_phone, username, "password", salt, avatar, sex, nickname, status,
@ -68,7 +69,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
} }
@Override @Override
public Account findByMobilePhone(MobilePhone mobilePhone) { public Optional<Account> findByMobilePhone(MobilePhone mobilePhone) {
return queryForObject(""" return queryForObject("""
SELECT SELECT
id, email, mobile_phone, username, "password", salt, avatar, sex, nickname, status, id, email, mobile_phone, username, "password", salt, avatar, sex, nickname, status,
@ -80,7 +81,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
} }
@Override @Override
public Account findByUsername(Username username) { public Optional<Account> findByUsername(Username username) {
return queryForObject(""" return queryForObject("""
SELECT SELECT
id, email, mobile_phone, username, "password", salt, avatar, sex, nickname, status, id, email, mobile_phone, username, "password", salt, avatar, sex, nickname, status,
@ -93,25 +94,25 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
@Override @Override
public boolean exists(Long id) { public boolean exists(Long id) {
return queryExists("SELECT 1 FROM sys_account WHERE id = :id AND deleted = 0 LIMIT 1", return queryExists("SELECT EXISTS (SELECT 1 FROM sys_account WHERE id = :id AND deleted = 0 LIMIT 1)",
"id", id); "id", id);
} }
@Override @Override
public boolean existsUsername(Username username) { public boolean existsUsername(Username username) {
return queryExists("SELECT 1 FROM sys_account WHERE username = :username AND deleted = 0 LIMIT 1", return queryExists("SELECT EXISTS (SELECT 1 FROM sys_account WHERE username = :username AND deleted = 0 LIMIT 1)",
"username", username.value()); "username", username.value());
} }
@Override @Override
public boolean existsEmail(Email email) { public boolean existsEmail(Email email) {
return queryExists("SELECT 1 FROM sys_account WHERE email = :email AND deleted = 0 LIMIT 1", return queryExists("SELECT EXISTS (SELECT 1 FROM sys_account WHERE email = :email AND deleted = 0 LIMIT 1)",
"email", email.value()); "email", email.value());
} }
@Override @Override
public boolean existsMobilePhone(MobilePhone mobilePhone) { public boolean existsMobilePhone(MobilePhone mobilePhone) {
return queryExists("SELECT 1 FROM sys_account WHERE mobile_phone = :mobile_phone AND deleted = 0 LIMIT 1", return queryExists("SELECT EXISTS (SELECT 1 FROM sys_account WHERE mobile_phone = :mobile_phone AND deleted = 0 LIMIT 1)",
"mobile_phone", mobilePhone.value()); "mobile_phone", mobilePhone.value());
} }
@ -143,7 +144,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
generateParamSource(id, entity)); generateParamSource(id, entity));
assertUpdateOneRow(i); assertUpdateOneRow(i);
this.accountRoleDAO.insertAccountRoleRefs(id, entity.getRoleIds()); this.accountRoleDAO.insertAccountRoleRefs(id, entity.getRoleIds());
return entity; return find(id).orElseThrow();
} }
@Override @Override
@ -167,7 +168,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
generateParamSource(entity)); generateParamSource(entity));
assertUpdateOneRow(i); assertUpdateOneRow(i);
this.accountRoleDAO.saveAccountRoleRefs(entity); this.accountRoleDAO.saveAccountRoleRefs(entity);
return entity; return find(entity.getId().orElseThrow()).orElseThrow();
} }
@Override @Override

View File

@ -6,6 +6,7 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.List; import java.util.List;
import java.util.Optional;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -15,6 +16,7 @@ import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.IdUtil;
import xyz.zhouxy.plusone.exception.DataNotExistException;
import xyz.zhouxy.plusone.jdbc.JdbcRepositorySupport; import xyz.zhouxy.plusone.jdbc.JdbcRepositorySupport;
import xyz.zhouxy.plusone.util.AssertResult; import xyz.zhouxy.plusone.util.AssertResult;
@ -34,7 +36,7 @@ public class DictRepositoryImpl extends JdbcRepositorySupport<Dict, Long> implem
} }
@Override @Override
public Dict doFindById(@Nonnull Long id) { public Optional<Dict> doFindById(@Nonnull Long id) {
return queryForObject("SELECT id, dict_type, dict_label, \"version\" WHERE id = :id AND deleted = 0", return queryForObject("SELECT id, dict_type, dict_label, \"version\" WHERE id = :id AND deleted = 0",
"id", id); "id", id);
} }
@ -49,7 +51,7 @@ public class DictRepositoryImpl extends JdbcRepositorySupport<Dict, Long> implem
generateParamSource(id, entity)); generateParamSource(id, entity));
AssertResult.updateOneRow(i); AssertResult.updateOneRow(i);
this.dictValueDAO.insertDictValues(id, entity); this.dictValueDAO.insertDictValues(id, entity);
return find(id); return find(id).orElseThrow(DataNotExistException::new);
} }
@Override @Override
@ -66,7 +68,7 @@ public class DictRepositoryImpl extends JdbcRepositorySupport<Dict, Long> implem
generateParamSource(entity)); generateParamSource(entity));
AssertResult.updateOneRow(i); AssertResult.updateOneRow(i);
this.dictValueDAO.updateDictValues(entity); this.dictValueDAO.updateDictValues(entity);
return find(entity.getId().orElseThrow()); return find(entity.getId().orElseThrow()).orElseThrow(DataNotExistException::new);
} }
@Override @Override
@ -81,7 +83,7 @@ public class DictRepositoryImpl extends JdbcRepositorySupport<Dict, Long> implem
@Override @Override
public boolean exists(Long id) { public boolean exists(Long id) {
return queryExists("SELECT 1 FROM sys_dict_type WHERE id = :id AND deleted = 0 LIMIT 1", return queryExists("SELECT EXISTS (SELECT 1 FROM sys_dict_type WHERE id = :id AND deleted = 0 LIMIT 1)",
new MapSqlParameterSource("id", id)); new MapSqlParameterSource("id", id));
} }

View File

@ -8,6 +8,7 @@ import java.time.LocalDateTime;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -17,6 +18,7 @@ import org.springframework.stereotype.Repository;
import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.IdUtil;
import xyz.zhouxy.plusone.constant.EntityStatus; import xyz.zhouxy.plusone.constant.EntityStatus;
import xyz.zhouxy.plusone.exception.DataNotExistException;
import xyz.zhouxy.plusone.jdbc.JdbcRepositorySupport; import xyz.zhouxy.plusone.jdbc.JdbcRepositorySupport;
import xyz.zhouxy.plusone.system.domain.model.menu.Menu.MenuType; import xyz.zhouxy.plusone.system.domain.model.menu.Menu.MenuType;
import xyz.zhouxy.plusone.util.AssertResult; import xyz.zhouxy.plusone.util.AssertResult;
@ -38,7 +40,7 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport<Menu, Long> implem
} }
@Override @Override
protected final Menu doFindById(@Nonnull Long id) { protected final Optional<Menu> doFindById(@Nonnull Long id) {
return queryForObject(""" return queryForObject("""
SELECT SELECT
id, parent_id, "type", "name", "path", title, icon, hidden, order_number, status, remarks, id, parent_id, "type", "name", "path", title, icon, hidden, order_number, status, remarks,
@ -64,7 +66,7 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport<Menu, Long> implem
int i = update(sql, paramSource); int i = update(sql, paramSource);
AssertResult.updateOneRow(i); AssertResult.updateOneRow(i);
this.actionDAO.saveActions(id, entity.getActions()); this.actionDAO.saveActions(id, entity.getActions());
return entity; return find(id).orElseThrow(DataNotExistException::new);
} }
@Override @Override
@ -97,7 +99,7 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport<Menu, Long> implem
// 保存权限 // 保存权限
Long id = entity.getId().orElseThrow(); Long id = entity.getId().orElseThrow();
this.actionDAO.saveActions(id, entity.getActions()); this.actionDAO.saveActions(id, entity.getActions());
return entity; return find(entity.getId().orElseThrow()).orElseThrow(DataNotExistException::new);
} }
@Override @Override
@ -113,7 +115,7 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport<Menu, Long> implem
@Override @Override
public boolean exists(Long id) { public boolean exists(Long id) {
return queryExists("SELECT 1 FROM sys_menu WHERE id = :id AND deleted = 0 LIMIT 1", return queryExists("SELECT EXISTS (SELECT 1 FROM sys_menu WHERE id = :id AND deleted = 0 LIMIT 1)",
"id", id); "id", id);
} }

View File

@ -6,6 +6,7 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Collection; import java.util.Collection;
import java.util.Optional;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -15,6 +16,7 @@ import org.springframework.stereotype.Repository;
import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.IdUtil;
import xyz.zhouxy.plusone.constant.EntityStatus; import xyz.zhouxy.plusone.constant.EntityStatus;
import xyz.zhouxy.plusone.exception.DataNotExistException;
import xyz.zhouxy.plusone.jdbc.JdbcRepositorySupport; import xyz.zhouxy.plusone.jdbc.JdbcRepositorySupport;
import xyz.zhouxy.plusone.util.AssertResult; import xyz.zhouxy.plusone.util.AssertResult;
@ -36,7 +38,7 @@ public class RoleRepositoryImpl extends JdbcRepositorySupport<Role, Long> implem
} }
@Override @Override
protected final Role doFindById(@Nonnull Long id) { protected final Optional<Role> doFindById(@Nonnull Long id) {
return queryForObject(""" return queryForObject("""
SELECT "id","name","identifier","status","remarks","version" SELECT "id","name","identifier","status","remarks","version"
FROM "sys_role" FROM "sys_role"
@ -58,7 +60,7 @@ public class RoleRepositoryImpl extends JdbcRepositorySupport<Role, Long> implem
@Override @Override
public boolean exists(Long id) { public boolean exists(Long id) {
return queryExists("SELECT 1 FROM sys_role WHERE id = :id AND deleted = 0 LIMIT 1", return queryExists("SELECT EXISTS (SELECT 1 FROM sys_role WHERE id = :id AND deleted = 0 LIMIT 1)",
"id", id); "id", id);
} }
@ -74,7 +76,7 @@ public class RoleRepositoryImpl extends JdbcRepositorySupport<Role, Long> implem
AssertResult.updateOneRow(i); AssertResult.updateOneRow(i);
this.roleMenuRefDAO.saveRoleMenuRefs(id, entity); this.roleMenuRefDAO.saveRoleMenuRefs(id, entity);
this.rolePermissionRefDAO.saveRolePermissionRefs(id, entity); this.rolePermissionRefDAO.saveRolePermissionRefs(id, entity);
return entity; return find(id).orElseThrow(DataNotExistException::new);
} }
@Override @Override
@ -97,7 +99,7 @@ public class RoleRepositoryImpl extends JdbcRepositorySupport<Role, Long> implem
this.roleMenuRefDAO.saveRoleMenuRefs(id, entity); this.roleMenuRefDAO.saveRoleMenuRefs(id, entity);
this.rolePermissionRefDAO.clearRolePermissionRefs(entity); this.rolePermissionRefDAO.clearRolePermissionRefs(entity);
this.rolePermissionRefDAO.saveRolePermissionRefs(id, entity); this.rolePermissionRefDAO.saveRolePermissionRefs(id, entity);
return entity; return find(entity.getId().orElseThrow()).orElseThrow(DataNotExistException::new);
} }
@Override @Override