From 5f1cb362353eb17ab09edca64d504721f4169bb1 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sun, 12 Mar 2023 13:29:07 +0800 Subject: [PATCH] =?UTF-8?q?AccountInfo=20=E7=9A=84=E6=9E=84=E5=BB=BA?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=20Builder=20=E6=A8=A1=E5=BC=8F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/AccountManagementService.java | 6 +- .../service/RegisterAccountService.java | 5 +- .../system/domain/model/account/Account.java | 12 ++- .../domain/model/account/AccountInfo.java | 75 ++++++++++++++----- .../system/domain/model/account/Sex.java | 5 ++ .../model/account/AccountRepositoryImpl.java | 10 +-- 6 files changed, 86 insertions(+), 27 deletions(-) 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 0eab48b..36050c3 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 @@ -69,7 +69,11 @@ public class AccountManagementService { command.getPasswordConfirmation(), command.getStatus(), command.getRoleRefs(), - AccountInfo.of(command.getNickname(), command.getAvatar(), command.getSex()), + AccountInfo.builder() + .nickname(command.getNickname()) + .avatar(command.getAvatar()) + .sex(command.getSex()) + .build(), adminAuthLogic.getLoginIdAsLong()); accountRepository.save(account); } diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/RegisterAccountService.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/RegisterAccountService.java index cfc3116..c81a49c 100644 --- a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/RegisterAccountService.java +++ b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/service/RegisterAccountService.java @@ -17,7 +17,6 @@ import xyz.zhouxy.plusone.system.domain.model.account.AccountStatus; import xyz.zhouxy.plusone.system.domain.model.account.Email; import xyz.zhouxy.plusone.system.domain.model.account.MobilePhone; import xyz.zhouxy.plusone.system.domain.model.account.Password; -import xyz.zhouxy.plusone.system.domain.model.account.Sex; import xyz.zhouxy.plusone.system.domain.model.account.Username; /** @@ -82,7 +81,9 @@ public class RegisterAccountService { Password.newPassword(command.getPassword(), command.getReenteredPassword()), AccountStatus.AVAILABLE, Set.of(DEFAULT_ROLE_ID), - AccountInfo.of(command.getNickname(), null, Sex.UNSET)); + AccountInfo.builder() + .nickname(command.getNickname()) + .build()); accountRepository.save(accountToSave); } diff --git a/plusone-system/plusone-system-domain/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/Account.java b/plusone-system/plusone-system-domain/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/Account.java index 895eda3..3150b64 100644 --- a/plusone-system/plusone-system-domain/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/Account.java +++ b/plusone-system/plusone-system-domain/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/Account.java @@ -93,11 +93,19 @@ public class Account extends AggregateRoot implements IWithVersion { } public void setAccountInfo(Nickname nickname, URL avatar, Sex sex) { - this.accountInfo = AccountInfo.of(nickname, avatar, sex); + this.accountInfo = AccountInfo.builder() + .nickname(nickname) + .avatar(avatar) + .sex(sex) + .build(); } public void setAccountInfo(String nickname, String avatar, Sex sex) { - this.accountInfo = AccountInfo.of(nickname, avatar, sex); + this.accountInfo = AccountInfo.builder() + .nickname(nickname) + .avatar(avatar) + .sex(sex) + .build(); } /** diff --git a/plusone-system/plusone-system-domain/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/AccountInfo.java b/plusone-system/plusone-system-domain/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/AccountInfo.java index 6b9aa87..7e750d0 100644 --- a/plusone-system/plusone-system-domain/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/AccountInfo.java +++ b/plusone-system/plusone-system-domain/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/AccountInfo.java @@ -5,7 +5,9 @@ import java.net.URL; import java.util.Objects; import java.util.Optional; -import lombok.Getter; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + import lombok.ToString; import xyz.zhouxy.plusone.domain.IValueObject; @@ -17,30 +19,19 @@ import xyz.zhouxy.plusone.domain.IValueObject; @ToString public class AccountInfo implements IValueObject { + @Nullable private final Nickname nickname; + @Nullable private final URL avatar; - private final @Getter Sex sex; + @Nonnull + private final Sex sex; - private AccountInfo(Nickname nickname, URL avatar, Sex sex) { + private AccountInfo(@Nullable Nickname nickname, @Nullable URL avatar, @Nullable Sex sex) { this.nickname = nickname; this.avatar = avatar; this.sex = Objects.nonNull(sex) ? sex : Sex.UNSET; } - public static AccountInfo of(Nickname nickname, URL avatar, Sex sex) { - return new AccountInfo(nickname, avatar, sex); - } - - public static AccountInfo of(String nickname, String avatar, Sex sex) { - URL avatarURL; - try { - avatarURL = Objects.nonNull(avatar) ? new URL(avatar) : null; - } catch (MalformedURLException e) { - throw new IllegalArgumentException(e); - } - return new AccountInfo(Nickname.ofNullable(nickname), avatarURL, sex); - } - public Optional getNickname() { return Optional.ofNullable(nickname); } @@ -48,4 +39,54 @@ public class AccountInfo implements IValueObject { public Optional getAvatar() { return Optional.ofNullable(avatar); } + + @Nonnull + public Sex getSex() { + return this.sex; + } + + // Builder + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + private Nickname nickname; + private URL avatar; + private Sex sex; + + public Builder nickname(@Nullable Nickname nickname) { + this.nickname = nickname; + return this; + } + + public Builder nickname(@Nullable String nickname) { + return nickname(Nickname.ofNullable(nickname)); + } + + public Builder avatar(@Nullable URL avatar) { + this.avatar = avatar; + return this; + } + + public Builder avatar(@Nullable String avatar) { + URL avatarURL; + try { + avatarURL = Objects.nonNull(avatar) ? new URL(avatar) : null; + } catch (MalformedURLException e) { + throw new IllegalArgumentException(e); + } + return avatar(avatarURL); + } + + public Builder sex(@Nullable Sex sex) { + this.sex = sex; + return this; + } + + public AccountInfo build() { + return new AccountInfo(this.nickname, this.avatar, this.sex); + } + } } diff --git a/plusone-system/plusone-system-domain/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/Sex.java b/plusone-system/plusone-system-domain/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/Sex.java index 3b80c87..e7d5117 100644 --- a/plusone-system/plusone-system-domain/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/Sex.java +++ b/plusone-system/plusone-system-domain/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/Sex.java @@ -1,5 +1,7 @@ package xyz.zhouxy.plusone.system.domain.model.account; +import javax.annotation.Nonnull; + import xyz.zhouxy.plusone.commons.util.Enumeration; import xyz.zhouxy.plusone.domain.IValueObject; @@ -9,8 +11,11 @@ import xyz.zhouxy.plusone.domain.IValueObject; * @author ZhouXY */ public class Sex extends Enumeration implements IValueObject { + @Nonnull public static final Sex UNSET = new Sex(0, "未设置"); + @Nonnull public static final Sex MALE = new Sex(1, "男性"); + @Nonnull public static final Sex FEMALE = new Sex(2, "女性"); private Sex(int value, String name) { 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 a700c63..32ec762 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 @@ -176,10 +176,6 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport @Override protected final Account mapRow(ResultSet rs) throws SQLException { long accountId = rs.getLong("id"); - AccountInfo accountInfo = AccountInfo.of( - rs.getString("nickname"), - rs.getString("avatar"), - Sex.of(rs.getInt("sex"))); return new Account( accountId, rs.getString("username"), @@ -187,7 +183,11 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport rs.getString("mobile_phone"), Password.of(rs.getString("password"), rs.getString("salt")), AccountStatus.of(rs.getInt("status")), - accountInfo, + AccountInfo.builder() + .nickname(rs.getString("nickname")) + .avatar(rs.getString("avatar")) + .sex(Sex.of(rs.getInt("sex"))) + .build(), this.accountRoleDAO.selectRoleIdsByAccountId(accountId), rs.getLong("created_by"), rs.getLong("updated_by"),