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;
}
final Boolean value = dto.getStateValue();
if (stateTrue != null && (dto.getStateProperty() == null || stateTrue != value)) {
if (stateTrue != null && (value == null || stateTrue != value)) {
return false;
}
if (stateFalse != null && (dto.getStateProperty() == null || stateFalse == value)) {
if (stateFalse != null && (value == null || stateFalse == value)) {
return false;
}
return search(dto.getName());

View File

@ -1,5 +1,6 @@
package de.ph87.home.device;
import de.ph87.home.common.crud.CrudAction;
import de.ph87.home.common.crud.EntityNotFound;
import de.ph87.home.property.*;
import jakarta.annotation.Nullable;
@ -28,7 +29,7 @@ public class DeviceService {
@NonNull
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 {
@ -75,13 +76,15 @@ public class DeviceService {
@EventListener(PropertyDto.class)
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) {
final DeviceDto deviceDto = toDto(device);
log.info("Device updated: {}", deviceDto);
applicationEventPublisher.publishEvent(deviceDto);
@NonNull
private DeviceDto publish(@NonNull final Device device, @NonNull final CrudAction action) {
final DeviceDto dto = toDto(device);
log.info("Device {}: {}", action, dto);
applicationEventPublisher.publishEvent(dto);
return dto;
}
@NonNull