-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDeviceConfigurationController.java
More file actions
44 lines (37 loc) · 1.56 KB
/
Copy pathDeviceConfigurationController.java
File metadata and controls
44 lines (37 loc) · 1.56 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
package devices.configuration.management;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
@RestController
@RequiredArgsConstructor
class DeviceConfigurationController {
private final DeviceConfigurationService service;
@GetMapping(path = "/devices/{deviceId}", produces = APPLICATION_JSON_VALUE)
DeviceConfigurationSnapshot getDevice(@PathVariable String deviceId) {
return service
.getDevice(deviceId)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
}
@PatchMapping(
path = "/devices/{deviceId}",
consumes = APPLICATION_JSON_VALUE,
produces = APPLICATION_JSON_VALUE)
DeviceConfigurationSnapshot patchDevice(
@PathVariable String deviceId,
@RequestHeader(value = "If-Match", required = false) Long version,
@RequestBody DeviceConfigurationPatch patch) {
try {
long effectiveVersion = version != null ? version : getCurrentVersion(deviceId);
return service.patchDevice(deviceId, patch, effectiveVersion);
} catch (DeviceNotFoundException e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage());
} catch (OptimisticLockException e) {
throw new ResponseStatusException(HttpStatus.CONFLICT, e.getMessage());
}
}
private long getCurrentVersion(String deviceId) {
return service.getCurrentVersion(deviceId);
}
}