Skip to content

Commit

Permalink
added deletion of measurements (made by a given device) when removing…
Browse files Browse the repository at this point in the history
… a device
  • Loading branch information
rafal-nowak authored and ogedo committed Apr 19, 2023
1 parent 12032ef commit 611e638
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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){
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public interface MeasurementRepository {

void remove(String deviceId, ZonedDateTime timestamp);

void removeAllByDeviceId(String deviceId);

Optional<Measurement> findById(String deviceId, ZonedDateTime timestamp);

PageMeasurement findAll(Pageable pageable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.springframework.stereotype.Repository;

import java.time.ZonedDateTime;
import java.util.List;
import java.util.Optional;

@Repository
Expand All @@ -25,4 +24,6 @@ interface DynamoMeasurementRepository extends PagingAndSortingRepository<Measure
MeasurementEntity save(MeasurementEntity entity);

void deleteByMeasurementEntityId(MeasurementEntityId id);

void deleteMeasurementEntitiesByDeviceId(String deviceId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public void remove(String deviceId, ZonedDateTime timestamp) {
measurementRepository.findByMeasurementEntityId(id).ifPresent(deviceEntity -> measurementRepository.deleteByMeasurementEntityId(id));
}

@Override
public void removeAllByDeviceId(final String deviceId) {
measurementRepository.deleteMeasurementEntitiesByDeviceId(deviceId);
}

@Override
public Optional<Measurement> findById(String deviceId, ZonedDateTime timestamp) {
MeasurementEntityId id = new MeasurementEntityId(deviceId, timestamp);
Expand Down

0 comments on commit 611e638

Please sign in to comment.