From 6da692e7e10672639c537d5d8ef12e0a97079499 Mon Sep 17 00:00:00 2001 From: Sebastian Stenzel Date: Sat, 1 Jun 2024 18:03:41 +0200 Subject: [PATCH] cleanup --- .../cryptomator/hub/api/UsersResource.java | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/backend/src/main/java/org/cryptomator/hub/api/UsersResource.java b/backend/src/main/java/org/cryptomator/hub/api/UsersResource.java index 637beb7b..0b200057 100644 --- a/backend/src/main/java/org/cryptomator/hub/api/UsersResource.java +++ b/backend/src/main/java/org/cryptomator/hub/api/UsersResource.java @@ -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); }