From 22f96efb6c586f53fcfb360d88c8b8536d57eb48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20S=C3=B6res?= Date: Fri, 20 Oct 2023 10:44:38 +0200 Subject: [PATCH] prevent multiple locks by same user --- .../collab/springbackend/items/ItemsRepository.java | 4 +++- .../springbackend/items/ItemsWebSocketHandler.java | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemsRepository.java b/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemsRepository.java index 71672a0..be8a841 100644 --- a/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemsRepository.java +++ b/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemsRepository.java @@ -3,7 +3,9 @@ import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface ItemsRepository extends CrudRepository { - + List findByUserLock(String userLock); } diff --git a/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemsWebSocketHandler.java b/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemsWebSocketHandler.java index 4aadbf4..381ffeb 100644 --- a/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemsWebSocketHandler.java +++ b/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemsWebSocketHandler.java @@ -49,6 +49,7 @@ public void handleItemLock(Message message) throws JsonProcessingExcepti String userName = optionalToken.map(AbstractAuthenticationToken::getName).orElse("dummyUser"); if (dto.lock()) { if (item.getUserLock() == null) { + resetUserLocks(userName); item.setUserLock(userName); itemsRepository.save(item); notifyInventoryChange(); @@ -62,6 +63,14 @@ public void handleItemLock(Message message) throws JsonProcessingExcepti } } + private void resetUserLocks(String userName) { + var entities = itemsRepository.findByUserLock(userName); + for (var entity : entities) { + entity.setUserLock(null); + } + itemsRepository.saveAll(entities); + } + @MessageMapping("/itemtransfer") public void handleItemTransfer(Message message) throws JsonProcessingException { var dto = objectMapper.readValue(message.getPayload(), ItemTransferDto.class);