Skip to content

Commit

Permalink
prevent multiple locks by same user
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsoeres committed Oct 20, 2023
1 parent cabe4ca commit 22f96ef
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ItemEntity, Long> {

List<ItemEntity> findByUserLock(String userLock);
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public void handleItemLock(Message<String> 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();
Expand All @@ -62,6 +63,14 @@ public void handleItemLock(Message<String> 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<String> message) throws JsonProcessingException {
var dto = objectMapper.readValue(message.getPayload(), ItemTransferDto.class);
Expand Down

0 comments on commit 22f96ef

Please sign in to comment.