Device publish on create

This commit is contained in:
Patrick Haßel 2024-11-21 15:31:34 +01:00
parent aa90d8125d
commit fdf64ebd17
3 changed files with 16 additions and 8 deletions

View File

@ -0,0 +1,5 @@
package de.ph87.home.common.crud;
public enum CrudAction {
CREATED, UPDATED, DELETED
}

View File

@ -29,10 +29,10 @@ public class DeviceFilter extends AbstractSearchFilter {
return false; return false;
} }
final Boolean value = dto.getStateValue(); final Boolean value = dto.getStateValue();
if (stateTrue != null && (dto.getStateProperty() == null || stateTrue != value)) { if (stateTrue != null && (value == null || stateTrue != value)) {
return false; return false;
} }
if (stateFalse != null && (dto.getStateProperty() == null || stateFalse == value)) { if (stateFalse != null && (value == null || stateFalse == value)) {
return false; return false;
} }
return search(dto.getName()); return search(dto.getName());

View File

@ -1,5 +1,6 @@
package de.ph87.home.device; package de.ph87.home.device;
import de.ph87.home.common.crud.CrudAction;
import de.ph87.home.common.crud.EntityNotFound; import de.ph87.home.common.crud.EntityNotFound;
import de.ph87.home.property.*; import de.ph87.home.property.*;
import jakarta.annotation.Nullable; import jakarta.annotation.Nullable;
@ -28,7 +29,7 @@ public class DeviceService {
@NonNull @NonNull
public DeviceDto create(@NonNull final String name, @NonNull final String slug, @NonNull final String stateProperty) { public DeviceDto create(@NonNull final String name, @NonNull final String slug, @NonNull final String stateProperty) {
return toDto(deviceRepository.save(new Device(name, slug, stateProperty))); return publish(deviceRepository.save(new Device(name, slug, stateProperty)), CrudAction.UPDATED);
} }
public void setState(@NonNull final String uuidOrSlug, final boolean state) throws PropertyNotFound, PropertyNotWritable, PropertyTypeMismatch { public void setState(@NonNull final String uuidOrSlug, final boolean state) throws PropertyNotFound, PropertyNotWritable, PropertyTypeMismatch {
@ -75,13 +76,15 @@ public class DeviceService {
@EventListener(PropertyDto.class) @EventListener(PropertyDto.class)
public void onPropertyChange(@NonNull final PropertyDto<?> dto) { public void onPropertyChange(@NonNull final PropertyDto<?> dto) {
deviceRepository.findAllByStatePropertyId(dto.getId()).forEach(this::publish); deviceRepository.findAllByStatePropertyId(dto.getId()).forEach(device -> publish(device, CrudAction.CREATED));
} }
private void publish(@NonNull final Device device) { @NonNull
final DeviceDto deviceDto = toDto(device); private DeviceDto publish(@NonNull final Device device, @NonNull final CrudAction action) {
log.info("Device updated: {}", deviceDto); final DeviceDto dto = toDto(device);
applicationEventPublisher.publishEvent(deviceDto); log.info("Device {}: {}", action, dto);
applicationEventPublisher.publishEvent(dto);
return dto;
} }
@NonNull @NonNull