Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
overheadhunter committed Jun 1, 2024
1 parent 5607799 commit 6da692e
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions backend/src/main/java/org/cryptomator/hub/api/UsersResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,23 @@ public Response putMe(@Nullable @Valid UserDto dto) {
}

/**
* updates those devices that are present in both the entity and the dto. No devices are added or removed.
* Updates those devices that are present in both the entity and the DTO. No devices are added or removed.
*
* @param userEntity The persistent entity
* @param userDto The DTO
* @param userDto The DTO
*/
private void updateDevices(User userEntity, UserDto userDto) {
var dtos = userDto.devices.stream().collect(Collectors.toMap(DeviceResource.DeviceDto::id, Function.identity()));
var updatedDevices = userEntity.devices.stream().filter(d -> dtos.containsKey(d.getId())).peek(device -> {
var dto = dtos.get(device.getId());
device.setType(dto.type());
device.setName(dto.name());
device.setPublickey(dto.publicKey());
device.setUserPrivateKeys(dto.userPrivateKeys());
});
var devices = userEntity.devices.stream().collect(Collectors.toUnmodifiableMap(Device::getId, Function.identity()));
var updatedDevices = userDto.devices.stream()
.filter(d -> devices.containsKey(d.id())) // only look at DTOs for which we find a matching existing entity
.map(dto -> {
var device = devices.get(dto.id());
device.setType(dto.type());
device.setName(dto.name());
device.setPublickey(dto.publicKey());
device.setUserPrivateKeys(dto.userPrivateKeys());
return device;
});
deviceRepo.persist(updatedDevices);
}

Expand Down

0 comments on commit 6da692e

Please sign in to comment.