PAN-60 Created an example UserService
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package edu.msudenver.tsp.services;
|
||||
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import edu.msudenver.tsp.services.dto.Account;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Optional;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class UserService {
|
||||
private final RestService restService;
|
||||
@Value("${persistence.api.connection.timeout.milliseconds}") private int connectionTimeoutMilliseconds;
|
||||
@Value("${persistence.api.socket.timeout.milliseconds}") private int socketTimeoutMilliseconds;
|
||||
@Value("${persistence.api.base.url}") private String persistenceApiBaseUrl;
|
||||
|
||||
@Autowired
|
||||
public UserService(final RestService restService) {
|
||||
this.restService = restService;
|
||||
}
|
||||
|
||||
public Optional<Account> createNewAccount(final Account account) {
|
||||
if (account == null) {
|
||||
LOG.error("Given null account, returning {}");
|
||||
return Optional.empty();
|
||||
}
|
||||
final Instant start = Instant.now();
|
||||
|
||||
try {
|
||||
final TypeToken<Account> typeToken = new TypeToken<Account>() {};
|
||||
final Optional<Account> persistenceApiResponse = restService.post(persistenceApiBaseUrl + "/accounts/",
|
||||
new GsonBuilder().create().toJson(account),
|
||||
typeToken,
|
||||
connectionTimeoutMilliseconds,
|
||||
socketTimeoutMilliseconds);
|
||||
|
||||
if (persistenceApiResponse.isPresent()) {
|
||||
LOG.info("Returning {}", persistenceApiResponse.get());
|
||||
} else {
|
||||
LOG.info("Unable to create new account {}", account.toString());
|
||||
}
|
||||
|
||||
return persistenceApiResponse;
|
||||
} catch (final Exception e) {
|
||||
LOG.error("Error creating new account {}", e);
|
||||
return Optional.empty();
|
||||
} finally {
|
||||
LOG.info("Create new account request took {} ms", Duration.between(start, Instant.now()).toMillis());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package edu.msudenver.tsp.services.dto;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import edu.msudenver.tsp.persistence.dto.AccountDto;
|
||||
|
||||
import javax.persistence.Temporal;
|
||||
import javax.persistence.TemporalType;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
public class Account extends BaseDto implements Serializable {
|
||||
@NotBlank(groups = AccountDto.Insert.class, message = "A username must be specified") @Size(max = 50) private String username;
|
||||
@NotBlank(groups = AccountDto.Insert.class, message = "A password must be specified") @Size(max = 256) private String password;
|
||||
@NotNull @SerializedName("administrator_status") private boolean administratorStatus;
|
||||
@Temporal(TemporalType.DATE) @SerializedName("last_login") private Date lastLogin;
|
||||
|
||||
private static final long serialVersionUID = 7095627971593953734L;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package edu.msudenver.tsp.services.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@Data
|
||||
public class BaseDto implements Serializable {
|
||||
private int id;
|
||||
private Integer version;
|
||||
|
||||
private static final long serialVersionUID = 5343705942114910963L;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
persistence.api.connection.timeout.milliseconds = 5000
|
||||
persistence.api.socket.timeout.milliseconds = 10000
|
||||
persistence.api.base.url = http://localhost:8090/
|
||||
Reference in New Issue
Block a user