PAN-60 Created an example UserService
This commit is contained in:
@@ -22,10 +22,30 @@ import java.util.Date;
|
|||||||
public class AccountDto extends BaseDto implements Serializable {
|
public class AccountDto extends BaseDto implements Serializable {
|
||||||
@NotBlank(groups = Insert.class, message = "A username must be specified") @Size(max = 50) private String username;
|
@NotBlank(groups = Insert.class, message = "A username must be specified") @Size(max = 50) private String username;
|
||||||
@NotBlank(groups = Insert.class, message = "A password must be specified") @Size(max = 256) private String password;
|
@NotBlank(groups = Insert.class, message = "A password must be specified") @Size(max = 256) private String password;
|
||||||
@NotNull @JsonProperty("administrator_status") private boolean administratorStatus;
|
@NotNull private boolean administratorStatus;
|
||||||
@Temporal(TemporalType.DATE) @JsonProperty("last_login") private Date lastLogin;
|
@Temporal(TemporalType.DATE) private Date lastLogin;
|
||||||
|
|
||||||
public static final long serialVersionUID = 7095627971593953734L;
|
private static final long serialVersionUID = 7095627971593953734L;
|
||||||
|
|
||||||
|
@JsonProperty("administrator_status")
|
||||||
|
public boolean getAdministratorStatus() {
|
||||||
|
return administratorStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty("administrator_status")
|
||||||
|
public void setAdministratorStatus(final boolean administratorStatus) {
|
||||||
|
this.administratorStatus = administratorStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty("last_login")
|
||||||
|
public Date getLastLogin() {
|
||||||
|
return lastLogin;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty("last_login")
|
||||||
|
public void setLastLogin(final Date lastLogin) {
|
||||||
|
this.lastLogin = lastLogin;
|
||||||
|
}
|
||||||
|
|
||||||
public interface Insert {}
|
public interface Insert {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,5 +36,5 @@ public class BaseDto implements Serializable {
|
|||||||
@Version
|
@Version
|
||||||
private Integer version;
|
private Integer version;
|
||||||
|
|
||||||
public static final long serialVersionUID = -1686252381978213945L;
|
private static final long serialVersionUID = -1686252381978213945L;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,5 +13,5 @@ import java.util.List;
|
|||||||
public class Definition implements Serializable {
|
public class Definition implements Serializable {
|
||||||
private List<String> definitions;
|
private List<String> definitions;
|
||||||
|
|
||||||
public static final long serialVersionUID = -2208496232532214840L;
|
private static final long serialVersionUID = -2208496232532214840L;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ public class DefinitionDto extends BaseDto implements Serializable {
|
|||||||
|
|
||||||
@Type(type = "json") @Column(columnDefinition = "jsonb") private Notation notation;
|
@Type(type = "json") @Column(columnDefinition = "jsonb") private Notation notation;
|
||||||
|
|
||||||
public static final long serialVersionUID = -5314619286352932857L;
|
private static final long serialVersionUID = -5314619286352932857L;
|
||||||
|
|
||||||
public interface Insert {}
|
public interface Insert {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,5 +12,5 @@ import java.util.List;
|
|||||||
@ToString
|
@ToString
|
||||||
public class Notation implements Serializable {
|
public class Notation implements Serializable {
|
||||||
private List<String> notations;
|
private List<String> notations;
|
||||||
public static final long serialVersionUID = 2301438318932336121L;
|
private static final long serialVersionUID = 2301438318932336121L;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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