AccountInfo 的构建使用 Builder 模式。

pull/1/head
ZhouXY108 2023-03-12 13:29:07 +08:00
parent b4bccfe663
commit 5f1cb36235
6 changed files with 86 additions and 27 deletions

View File

@ -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);
}

View File

@ -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);
}

View File

@ -93,11 +93,19 @@ public class Account extends AggregateRoot<Long> 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();
}
/**

View File

@ -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<Nickname> getNickname() {
return Optional.ofNullable(nickname);
}
@ -48,4 +39,54 @@ public class AccountInfo implements IValueObject {
public Optional<URL> 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);
}
}
}

View File

@ -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 <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*/
public class Sex extends Enumeration<Sex> 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) {

View File

@ -176,10 +176,6 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
@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<Account, Long>
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"),