AccountsUpdate#update: Return Optional<AccountState> instead of AccountState

This makes it more explicit that callers must handle the case where the
returned AccountState is absent.

Change-Id: Id301678f97d2a8827c3a23d85cbc5f6da97abc7c
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2018-01-12 10:52:25 +01:00
parent 5c37073931
commit d40d81482d
8 changed files with 103 additions and 112 deletions

View File

@@ -82,10 +82,8 @@ public class PutName implements RestModifyView<AccountResource, NameInput> {
AccountState accountState =
accountsUpdate
.create()
.update("Set Full Name via API", user.getAccountId(), u -> u.setFullName(newName));
if (accountState == null) {
throw new ResourceNotFoundException("account not found");
}
.update("Set Full Name via API", user.getAccountId(), u -> u.setFullName(newName))
.orElseThrow(() -> new ResourceNotFoundException("account not found"));
return Strings.isNullOrEmpty(accountState.getAccount().getFullName())
? Response.none()
: Response.ok(accountState.getAccount().getFullName());

View File

@@ -22,7 +22,6 @@ import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.account.AccountResource;
import com.google.gerrit.server.account.AccountState;
import com.google.gerrit.server.account.AccountsUpdate;
import com.google.gerrit.server.permissions.GlobalPermission;
import com.google.gerrit.server.permissions.PermissionBackend;
@@ -65,22 +64,19 @@ public class PutPreferred implements RestModifyView<AccountResource.Email, Input
public Response<String> apply(IdentifiedUser user, String email)
throws ResourceNotFoundException, IOException, ConfigInvalidException, OrmException {
AtomicBoolean alreadyPreferred = new AtomicBoolean(false);
AccountState accountState =
accountsUpdate
.create()
.update(
"Set Preferred Email via API",
user.getAccountId(),
(a, u) -> {
if (email.equals(a.getAccount().getPreferredEmail())) {
alreadyPreferred.set(true);
} else {
u.setPreferredEmail(email);
}
});
if (accountState == null) {
throw new ResourceNotFoundException("account not found");
}
accountsUpdate
.create()
.update(
"Set Preferred Email via API",
user.getAccountId(),
(a, u) -> {
if (email.equals(a.getAccount().getPreferredEmail())) {
alreadyPreferred.set(true);
} else {
u.setPreferredEmail(email);
}
})
.orElseThrow(() -> new ResourceNotFoundException("account not found"));
return alreadyPreferred.get() ? Response.ok("") : Response.created("");
}
}

View File

@@ -71,10 +71,8 @@ public class PutStatus implements RestModifyView<AccountResource, StatusInput> {
AccountState accountState =
accountsUpdate
.create()
.update("Set Status via API", user.getAccountId(), u -> u.setStatus(newStatus));
if (accountState == null) {
throw new ResourceNotFoundException("account not found");
}
.update("Set Status via API", user.getAccountId(), u -> u.setStatus(newStatus))
.orElseThrow(() -> new ResourceNotFoundException("account not found"));
return Strings.isNullOrEmpty(accountState.getAccount().getStatus())
? Response.none()
: Response.ok(accountState.getAccount().getStatus());