From 611e638f526fb16aca4bd78d68c8caa4c73458dc Mon Sep 17 00:00:00 2001 From: rafal-nowak Date: Wed, 19 Apr 2023 07:20:34 +0200 Subject: [PATCH] added deletion of measurements (made by a given device) when removing a device --- .../devicemanagement/config/DomainConfiguration.java | 4 ++-- .../devicemanagement/domain/device/DeviceService.java | 3 +++ .../domain/measurement/MeasurementRepository.java | 2 ++ .../domain/measurement/MeasurementService.java | 4 ++++ .../storage/measurement/DynamoMeasurementRepository.java | 3 ++- .../storage/measurement/MeasurementStorageAdapter.java | 5 +++++ 6 files changed, 18 insertions(+), 3 deletions(-) diff --git a/backend/device-management-main/src/main/java/com/amigoscode/devicemanagement/config/DomainConfiguration.java b/backend/device-management-main/src/main/java/com/amigoscode/devicemanagement/config/DomainConfiguration.java index bb1bdc4..44fe25a 100644 --- a/backend/device-management-main/src/main/java/com/amigoscode/devicemanagement/config/DomainConfiguration.java +++ b/backend/device-management-main/src/main/java/com/amigoscode/devicemanagement/config/DomainConfiguration.java @@ -34,8 +34,8 @@ public UserService userService(UserRepository userRepository, EncodingService en } @Bean - public DeviceService deviceService(DeviceRepository deviceRepository, DeviceSettingService deviceSettingService, Clock clock) { - return new DeviceService(deviceRepository, deviceSettingService, clock); + public DeviceService deviceService(DeviceRepository deviceRepository, DeviceSettingService deviceSettingService, MeasurementService measurementService, Clock clock) { + return new DeviceService(deviceRepository, deviceSettingService,measurementService, clock); } @Bean diff --git a/backend/device-management-main/src/main/java/com/amigoscode/devicemanagement/domain/device/DeviceService.java b/backend/device-management-main/src/main/java/com/amigoscode/devicemanagement/domain/device/DeviceService.java index db9c4c7..41e77b8 100644 --- a/backend/device-management-main/src/main/java/com/amigoscode/devicemanagement/domain/device/DeviceService.java +++ b/backend/device-management-main/src/main/java/com/amigoscode/devicemanagement/domain/device/DeviceService.java @@ -4,6 +4,7 @@ import com.amigoscode.devicemanagement.domain.device.model.PageDevice; import com.amigoscode.devicemanagement.domain.devicesetting.DeviceSettingService; import com.amigoscode.devicemanagement.domain.devicesetting.model.DeviceSetting; +import com.amigoscode.devicemanagement.domain.measurement.MeasurementService; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; @@ -15,6 +16,7 @@ public class DeviceService { private final DeviceRepository deviceRepository; private final DeviceSettingService deviceSettingService; + private final MeasurementService measurementService; private final Clock clock; public boolean isDeviceRegistered(String deviceId){ @@ -51,6 +53,7 @@ public void update(Device device, String updaterId){ } public void removeById(String id){ + measurementService.removeAllByDeviceId(id); DeviceSetting deviceSetting = deviceSettingService.findByDeviceId(id); deviceSettingService.removeById(deviceSetting.getId()); deviceRepository.remove(id); diff --git a/backend/device-management-main/src/main/java/com/amigoscode/devicemanagement/domain/measurement/MeasurementRepository.java b/backend/device-management-main/src/main/java/com/amigoscode/devicemanagement/domain/measurement/MeasurementRepository.java index 9c4820e..6c5a125 100644 --- a/backend/device-management-main/src/main/java/com/amigoscode/devicemanagement/domain/measurement/MeasurementRepository.java +++ b/backend/device-management-main/src/main/java/com/amigoscode/devicemanagement/domain/measurement/MeasurementRepository.java @@ -13,6 +13,8 @@ public interface MeasurementRepository { void remove(String deviceId, ZonedDateTime timestamp); + void removeAllByDeviceId(String deviceId); + Optional findById(String deviceId, ZonedDateTime timestamp); PageMeasurement findAll(Pageable pageable); diff --git a/backend/device-management-main/src/main/java/com/amigoscode/devicemanagement/domain/measurement/MeasurementService.java b/backend/device-management-main/src/main/java/com/amigoscode/devicemanagement/domain/measurement/MeasurementService.java index ae30fc9..27f58e4 100644 --- a/backend/device-management-main/src/main/java/com/amigoscode/devicemanagement/domain/measurement/MeasurementService.java +++ b/backend/device-management-main/src/main/java/com/amigoscode/devicemanagement/domain/measurement/MeasurementService.java @@ -20,6 +20,10 @@ public void removeById(String deviceId, ZonedDateTime timestamp){ measurementRepository.remove(deviceId, timestamp); } + public void removeAllByDeviceId(String deviceId){ + measurementRepository.removeAllByDeviceId(deviceId); + } + public Measurement findById(String deviceId, ZonedDateTime timestamp){ return measurementRepository.findById(deviceId, timestamp) .orElseThrow(MeasurementNotFoundException::new); diff --git a/backend/device-management-main/src/main/java/com/amigoscode/devicemanagement/external/storage/measurement/DynamoMeasurementRepository.java b/backend/device-management-main/src/main/java/com/amigoscode/devicemanagement/external/storage/measurement/DynamoMeasurementRepository.java index 82e8691..7bad88a 100644 --- a/backend/device-management-main/src/main/java/com/amigoscode/devicemanagement/external/storage/measurement/DynamoMeasurementRepository.java +++ b/backend/device-management-main/src/main/java/com/amigoscode/devicemanagement/external/storage/measurement/DynamoMeasurementRepository.java @@ -8,7 +8,6 @@ import org.springframework.stereotype.Repository; import java.time.ZonedDateTime; -import java.util.List; import java.util.Optional; @Repository @@ -25,4 +24,6 @@ interface DynamoMeasurementRepository extends PagingAndSortingRepository measurementRepository.deleteByMeasurementEntityId(id)); } + @Override + public void removeAllByDeviceId(final String deviceId) { + measurementRepository.deleteMeasurementEntitiesByDeviceId(deviceId); + } + @Override public Optional findById(String deviceId, ZonedDateTime timestamp) { MeasurementEntityId id = new MeasurementEntityId(deviceId, timestamp);