PAN-15 fix the request changes

This commit is contained in:
dantanxiaotian
2019-03-27 12:18:12 -06:00
parent eedc7a3eee
commit b1d4ed9719
4 changed files with 212 additions and 36 deletions
@@ -26,7 +26,99 @@ public class UserService {
this.restService = restService;
}
public Optional<Account> createNewAccount(final Account account) {
public Optional<Account> getListOfAccount(){
final String auth = "";
final Instant start = Instant.now();
try {
final TypeToken<Account> typeToken = new TypeToken<Account>() {};
final Optional<Account> persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/",
typeToken,
connectionTimeoutMilliseconds,
socketTimeoutMilliseconds,
auth);
if (persistenceApiResponse.isPresent()) {
LOG.info("Returning {}", persistenceApiResponse.get());
} else {
LOG.info("Unable to get the list of accounts");
}
return persistenceApiResponse;
} catch (final Exception e) {
LOG.error("Error getting the list of accounts {}", e);
return Optional.empty();
} finally {
LOG.info("Getting the list of accounts request took {} ms", Duration.between(start, Instant.now()).toMillis());
}
}
public Optional<Account> getAccountById(final int id) {
if (id == 0) {
LOG.error("No user ID specified! Returning {}");
return Optional.empty();
}
final String auth = "";
final Instant start = Instant.now();
try {
final TypeToken<Account> typeToken = new TypeToken<Account>() {};
final Optional<Account> persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/id?id="+id,
typeToken,
connectionTimeoutMilliseconds,
socketTimeoutMilliseconds,
auth);
if (persistenceApiResponse.isPresent()) {
LOG.info("Returning {}", persistenceApiResponse.get());
} else {
LOG.info("Unable to find account with id {}", id);
}
return persistenceApiResponse;
} catch (final Exception e) {
LOG.error("Error getting account by id {}", e);
return Optional.empty();
} finally {
LOG.info("Getting account by ID request took {} ms", Duration.between(start, Instant.now()).toMillis());
}
}
public Optional<Account> getAccountByUsername(final String username) {
if (username == null) {
LOG.error("No username specified! Returning {}");
return Optional.empty();
}
final String auth = "";
final Instant start = Instant.now();
try {
final TypeToken<Account> typeToken = new TypeToken<Account>() {};
final Optional<Account> persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/username?username="+username,
typeToken,
connectionTimeoutMilliseconds,
socketTimeoutMilliseconds,
auth);
if (persistenceApiResponse.isPresent()) {
LOG.info("Returning {}", persistenceApiResponse.get());
} else {
LOG.info("Unable to GET account with username{}", username);
}
return persistenceApiResponse;
} catch (final Exception e) {
LOG.error("Error getting account by username {}", e);
return Optional.empty();
} finally {
LOG.info("Getting account by username request took {} ms", Duration.between(start, Instant.now()).toMillis());
}
}
public Optional<Account> createAccount(final Account account) {
if (account == null) {
LOG.error("Given null account, returning {}");
return Optional.empty();
@@ -58,7 +150,7 @@ public class UserService {
public Optional<Account> updateAccount(final Account account) {
if (account == null) {
LOG.error("User does not exist, returning {}");
LOG.error("Specified account is null; returning {}");
return Optional.empty();
}
@@ -81,7 +173,7 @@ public class UserService {
if (persistenceApiResponse.isPresent()) {
LOG.info("Returning {}", persistenceApiResponse.get());
} else {
LOG.info("Unable to update user {} with id", account.getId());
LOG.info("Unable to update user with id {}", account.getId());
}
return persistenceApiResponse;
@@ -95,8 +187,8 @@ public class UserService {
public boolean deleteAccount(final Account account) {
if(account == null){
LOG.error("Username does not exist, returning {}");
if (account == null){
LOG.error("Specified account is null; returning {}");
return false;
}
@@ -107,13 +199,13 @@ public class UserService {
final int id = account.getId();
final Instant start = Instant.now();
try{
try {
final boolean persistenceApiResponse = restService.delete(persistenceApiBaseUrl + "/accounts/" + id,
final boolean persistenceApiResponse = restService.delete(persistenceApiBaseUrl + "accounts/" + id,
connectionTimeoutMilliseconds,
socketTimeoutMilliseconds, HttpStatus.NO_CONTENT );
if(persistenceApiResponse){
LOG.info("return {}", persistenceApiResponse);
socketTimeoutMilliseconds, HttpStatus.NO_CONTENT);
if (persistenceApiResponse){
LOG.info("Returning {}", persistenceApiResponse);
}
else {
LOG.info("Unable to delete user {}", account.toString());
@@ -124,7 +216,7 @@ public class UserService {
LOG.error("Error deleting user {}", e);
return false;
} finally {
LOG.info("delete user request took {} ms", Duration.between(start, Instant.now()).toMillis());
LOG.info("Delete user request took {} ms", Duration.between(start, Instant.now()).toMillis());
}
}
}