diff --git a/src/main/java/de/ph87/home/common/EntityNotFound.java b/src/main/java/de/ph87/home/common/EntityNotFound.java index ae2d190..1f94ab8 100644 --- a/src/main/java/de/ph87/home/common/EntityNotFound.java +++ b/src/main/java/de/ph87/home/common/EntityNotFound.java @@ -8,7 +8,7 @@ import org.springframework.web.bind.annotation.ResponseStatus; public class EntityNotFound extends RuntimeException { public EntityNotFound(@NonNull final String key, @NonNull final Object value) { - super("Device not found: %s=%s".formatted(key, value)); + super("Entity not found: %s=%s".formatted(key, value)); } } diff --git a/src/main/java/de/ph87/home/device/Device.java b/src/main/java/de/ph87/home/device/Device.java index 0782ea6..1c63e54 100644 --- a/src/main/java/de/ph87/home/device/Device.java +++ b/src/main/java/de/ph87/home/device/Device.java @@ -28,12 +28,12 @@ public class Device { @Setter @NonNull @Column(nullable = false) - private String stateProperty; + private String statePropertyId; - public Device(@NonNull final String name, @NonNull final String slug, @NonNull final String stateProperty) { + public Device(@NonNull final String name, @NonNull final String slug, @NonNull final String statePropertyId) { this.name = name; this.slug = slug; - this.stateProperty = stateProperty; + this.statePropertyId = statePropertyId; } } diff --git a/src/main/java/de/ph87/home/device/DeviceController.java b/src/main/java/de/ph87/home/device/DeviceController.java index fbbb4c2..d8a2e56 100644 --- a/src/main/java/de/ph87/home/device/DeviceController.java +++ b/src/main/java/de/ph87/home/device/DeviceController.java @@ -40,14 +40,14 @@ public class DeviceController { @GetMapping("get/{uuidOrSlug}") private DeviceDto get(@PathVariable @NonNull final String uuidOrSlug, @NonNull final HttpServletRequest request) { log.debug("get: path={}", request.getServletPath()); - return deviceService.toDto(uuidOrSlug); + return deviceService.getByUuidOrSlugDto(uuidOrSlug); } @Nullable @GetMapping("getState/{uuidOrSlug}") private Boolean getState(@PathVariable @NonNull final String uuidOrSlug, @NonNull final HttpServletRequest request) throws PropertyTypeMismatch { log.debug("getState: path={}", request.getServletPath()); - return deviceService.toDto(uuidOrSlug).getStateValue(); + return deviceService.getByUuidOrSlugDto(uuidOrSlug).getStateValue(); } @GetMapping("setState/{uuidOrSlug}/{state}") diff --git a/src/main/java/de/ph87/home/device/DeviceDto.java b/src/main/java/de/ph87/home/device/DeviceDto.java index c128c25..c3b79c9 100644 --- a/src/main/java/de/ph87/home/device/DeviceDto.java +++ b/src/main/java/de/ph87/home/device/DeviceDto.java @@ -36,7 +36,7 @@ public class DeviceDto implements IWebSocketMessage { this.uuid = device.getUuid(); this.name = device.getName(); this.slug = device.getSlug(); - this.statePropertyId = device.getStateProperty(); + this.statePropertyId = device.getStatePropertyId(); this.stateProperty = stateProperty; } diff --git a/src/main/java/de/ph87/home/device/DeviceService.java b/src/main/java/de/ph87/home/device/DeviceService.java index 8e1d177..acbb183 100644 --- a/src/main/java/de/ph87/home/device/DeviceService.java +++ b/src/main/java/de/ph87/home/device/DeviceService.java @@ -34,25 +34,25 @@ public class DeviceService { public void setState(@NonNull final String uuidOrSlug, final boolean state) throws PropertyNotFound, PropertyNotWritable, PropertyTypeMismatch { log.debug("setState: uuidOrSlug={}, state={}", uuidOrSlug, state); final Device device = getByUuidOrSlug(uuidOrSlug); - propertyService.write(device.getStateProperty(), state, Boolean.class); + propertyService.write(device.getStatePropertyId(), state, Boolean.class); } @NonNull - public DeviceDto toDto(final @NonNull String uuidOrSlug) { + public DeviceDto getByUuidOrSlugDto(final @NonNull String uuidOrSlug) { return toDto(getByUuidOrSlug(uuidOrSlug)); } - @NonNull - public DeviceDto toDto(@NonNull final Device device) { - final PropertyDto state = propertyService.dtoByIdAndTypeOrNull(device.getStateProperty(), Boolean.class); - return new DeviceDto(device, state); - } - @NonNull private Device getByUuidOrSlug(@NonNull final String uuidOrSlug) { return deviceRepository.findByUuidOrSlug(uuidOrSlug, uuidOrSlug).orElseThrow(() -> new EntityNotFound("uuidOrSlug", uuidOrSlug)); } + @NonNull + public DeviceDto toDto(@NonNull final Device device) { + final PropertyDto state = propertyService.dtoByIdAndTypeOrNull(device.getStatePropertyId(), Boolean.class); + return new DeviceDto(device, state); + } + @NonNull private Device getByUuid(@NonNull final String uuid) { return deviceRepository.findById(uuid).orElseThrow(() -> new EntityNotFound("uuid", uuid));