-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDeviceConfigurationService.java
More file actions
104 lines (90 loc) · 3.65 KB
/
Copy pathDeviceConfigurationService.java
File metadata and controls
104 lines (90 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package devices.configuration.management;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
@RequiredArgsConstructor
public class DeviceConfigurationService {
private final DeviceConfigurationRepository repository;
public DeviceConfigurationSnapshot createDevice(String deviceId) {
if (repository.exists(deviceId)) {
throw new DeviceAlreadyExistsException(deviceId);
}
DeviceConfiguration device = DeviceConfiguration.newDevice(deviceId);
repository.save(device, null);
return device.toSnapshot();
}
@Transactional(readOnly = true)
public Optional<DeviceConfigurationSnapshot> getDevice(String deviceId) {
return repository
.findById(deviceId)
.map(VersionedDevice::device)
.map(DeviceConfiguration::toSnapshot);
}
public DeviceConfigurationSnapshot updateOwnership(
String deviceId, Ownership ownership, long version) {
VersionedDevice versioned =
repository.findById(deviceId).orElseThrow(() -> new DeviceNotFoundException(deviceId));
DeviceConfiguration device = versioned.device();
device.changeOwnership(ownership);
repository.save(device, version);
return device.toSnapshot();
}
public DeviceConfigurationSnapshot updateLocation(
String deviceId, Location location, long version) {
VersionedDevice versioned =
repository.findById(deviceId).orElseThrow(() -> new DeviceNotFoundException(deviceId));
DeviceConfiguration device = versioned.device();
device.changeLocation(location);
repository.save(device, version);
return device.toSnapshot();
}
public DeviceConfigurationSnapshot updateOpeningHours(
String deviceId, OpeningHours openingHours, long version) {
VersionedDevice versioned =
repository.findById(deviceId).orElseThrow(() -> new DeviceNotFoundException(deviceId));
DeviceConfiguration device = versioned.device();
device.changeOpeningHours(openingHours);
repository.save(device, version);
return device.toSnapshot();
}
public DeviceConfigurationSnapshot updateSettings(
String deviceId, Settings settings, long version) {
VersionedDevice versioned =
repository.findById(deviceId).orElseThrow(() -> new DeviceNotFoundException(deviceId));
DeviceConfiguration device = versioned.device();
device.changeSettings(settings);
repository.save(device, version);
return device.toSnapshot();
}
public void deleteDevice(String deviceId, long version) {
repository.delete(deviceId, version);
}
@Transactional(readOnly = true)
public Page<DeviceConfigurationSnapshot> listDevices(Pageable pageable) {
return repository
.findAll(pageable)
.map(VersionedDevice::device)
.map(DeviceConfiguration::toSnapshot);
}
public DeviceConfigurationSnapshot patchDevice(
String deviceId, DeviceConfigurationPatch patch, long version) {
VersionedDevice versioned =
repository.findById(deviceId).orElseThrow(() -> new DeviceNotFoundException(deviceId));
DeviceConfiguration device = versioned.device();
DeviceConfigurationSnapshot currentSnapshot = device.toSnapshot();
patch.applyTo(device, currentSnapshot);
repository.save(device, version);
return device.toSnapshot();
}
public long getCurrentVersion(String deviceId) {
return repository
.findById(deviceId)
.map(VersionedDevice::version)
.orElseThrow(() -> new DeviceNotFoundException(deviceId));
}
}