refactored to make use of the ServiceType enum throughout backend and frontend for typesafety
This commit is contained in:
@@ -2,5 +2,5 @@ package com.vaessl.app.connection;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
public record AuthResponse(String serviceType, Instant expiresAt) {
|
||||
public record AuthResponse(ServiceType serviceType, Instant expiresAt) {
|
||||
}
|
||||
|
||||
@@ -53,19 +53,24 @@ public class ConnectionController {
|
||||
Collections.list(session.getAttributeNames()).stream()
|
||||
.filter(k -> k.endsWith(SessionKeys.CONNECTION_ID_SUFFIX))
|
||||
.forEach(k -> {
|
||||
String serviceType = k.replace(SessionKeys.CONNECTION_ID_SUFFIX, "");
|
||||
Long id = (Long) session.getAttribute(k);
|
||||
ConnectionStatusResponse status =
|
||||
connectionService.getConnectionStatus(serviceType, id);
|
||||
if (status != null)
|
||||
statuses.add(status);
|
||||
String name = k.replace(SessionKeys.CONNECTION_ID_SUFFIX, "");
|
||||
try {
|
||||
ServiceType serviceType = ServiceType.valueOf(name);
|
||||
Long id = (Long) session.getAttribute(k);
|
||||
ConnectionStatusResponse status =
|
||||
connectionService.getConnectionStatus(serviceType, id);
|
||||
if (status != null)
|
||||
statuses.add(status);
|
||||
} catch (IllegalArgumentException _) {
|
||||
// stale session key from an old or removed service type — skip it
|
||||
}
|
||||
});
|
||||
|
||||
return ResponseEntity.ok(statuses);
|
||||
}
|
||||
|
||||
@DeleteMapping("/connections/{serviceType}")
|
||||
public ResponseEntity<Void> logout(@PathVariable("serviceType") String serviceType,
|
||||
public ResponseEntity<Void> logout(@PathVariable("serviceType") ServiceType serviceType,
|
||||
HttpServletRequest httpReq) {
|
||||
|
||||
HttpSession session = httpReq.getSession(false);
|
||||
|
||||
@@ -3,9 +3,10 @@ package com.vaessl.app.connection;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public record ConnectionRequest(@NotBlank(message = "App URL is mandatory") String appUrl,
|
||||
@NotBlank(message = "Service type is mandatory") String serviceType,
|
||||
@NotNull(message = "Service type is mandatory") ServiceType serviceType,
|
||||
String username, String password, String apiKey,
|
||||
@JsonProperty(defaultValue = "false") Boolean stayLoggedIn) {
|
||||
|
||||
@@ -15,7 +16,7 @@ public record ConnectionRequest(@NotBlank(message = "App URL is mandatory") Stri
|
||||
}
|
||||
}
|
||||
|
||||
public ConnectionRequest(String appUrl, String serviceType, String username,
|
||||
public ConnectionRequest(String appUrl, ServiceType serviceType, String username,
|
||||
String password, Boolean stayLoggedIn) {
|
||||
this(appUrl, serviceType, username, password, null, stayLoggedIn);
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ import com.vaessl.app.exception.WrongServiceTypeException;
|
||||
@Service
|
||||
public class ConnectionService {
|
||||
|
||||
private final Map<String, ConnectionProvider> providerRegistry;
|
||||
private final Map<ServiceType, ConnectionProvider> providerRegistry;
|
||||
|
||||
private final ConnectionRepository cRepository;
|
||||
|
||||
@@ -48,7 +48,7 @@ public class ConnectionService {
|
||||
return new LoginResult(saved.getId(), response.expiresAt());
|
||||
}
|
||||
|
||||
public ConnectionStatusResponse getConnectionStatus(String serviceType, Long connectionId) {
|
||||
public ConnectionStatusResponse getConnectionStatus(ServiceType serviceType, Long connectionId) {
|
||||
ConnectionEntity entity = cRepository.findById(connectionId).orElse(null);
|
||||
if (entity == null)
|
||||
return null;
|
||||
@@ -57,7 +57,7 @@ public class ConnectionService {
|
||||
Instant expiresAt = (provider != null) ? provider.getTokenExpiry(entity) : null;
|
||||
boolean connected = expiresAt == null || expiresAt.isAfter(Instant.now());
|
||||
|
||||
return new ConnectionStatusResponse(serviceType, entity.getAppUrl(), entity.getUsername(),
|
||||
return new ConnectionStatusResponse(serviceType.name(), entity.getAppUrl(), entity.getUsername(),
|
||||
expiresAt, connected);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,8 +44,8 @@ public class HomeboxConnectionProvider implements ConnectionProvider {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServiceType() {
|
||||
return "HOMEBOX";
|
||||
public ServiceType getServiceType() {
|
||||
return ServiceType.HOMEBOX;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -6,5 +6,5 @@ public interface ServiceProvider {
|
||||
* Returns the service type key used to look up this provider in a registry, e.g.
|
||||
* {@code "HOMEBOX"}.
|
||||
*/
|
||||
String getServiceType();
|
||||
ServiceType getServiceType();
|
||||
}
|
||||
|
||||
@@ -1,14 +1,5 @@
|
||||
package com.vaessl.app.connection;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public enum ServiceType {
|
||||
HOMEBOX("HOMEBOX");
|
||||
|
||||
private final String value;
|
||||
|
||||
private ServiceType(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
HOMEBOX;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ public final class SessionKeys {
|
||||
|
||||
private SessionKeys() {}
|
||||
|
||||
public static String connectionId(String serviceType) {
|
||||
return serviceType + CONNECTION_ID_SUFFIX;
|
||||
public static String connectionId(ServiceType serviceType) {
|
||||
return serviceType.name() + CONNECTION_ID_SUFFIX;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user