Device publish on create
This commit is contained in:
parent
aa90d8125d
commit
fdf64ebd17
5
src/main/java/de/ph87/home/common/crud/CrudAction.java
Normal file
5
src/main/java/de/ph87/home/common/crud/CrudAction.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package de.ph87.home.common.crud;
|
||||||
|
|
||||||
|
public enum CrudAction {
|
||||||
|
CREATED, UPDATED, DELETED
|
||||||
|
}
|
||||||
@ -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());
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user