From 04c00af3baa8fefd75b68c26217efc2b61144527 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 12 Jun 2021 11:57:56 +0200 Subject: [PATCH] WIP --- .../transfer/controller/TransferDTO.java | 54 +++++++++++++ .../InMemoryTransferRepository.java | 81 +++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 transfer/src/main/java/de/juplo/kafka/payment/transfer/controller/TransferDTO.java create mode 100644 transfer/src/main/java/de/juplo/kafka/payment/transfer/persistence/InMemoryTransferRepository.java diff --git a/transfer/src/main/java/de/juplo/kafka/payment/transfer/controller/TransferDTO.java b/transfer/src/main/java/de/juplo/kafka/payment/transfer/controller/TransferDTO.java new file mode 100644 index 0000000..6f1310e --- /dev/null +++ b/transfer/src/main/java/de/juplo/kafka/payment/transfer/controller/TransferDTO.java @@ -0,0 +1,54 @@ +package de.juplo.kafka.payment.transfer.controller; + +import de.juplo.kafka.payment.transfer.domain.Transfer; +import lombok.Builder; +import lombok.Data; + +import javax.validation.constraints.NotNull; + + +/** + * Simple DTO used by the REST interface + */ +@Data +@Builder +public class TransferDTO +{ + @NotNull(message = "Cannot be null") + private long id; + @NotNull(message = "Cannot be null") + private long payer; + @NotNull(message = "Cannot be null") + private long payee; + @NotNull(message = "Cannot be null") + private int amount; + + private Transfer.State state; + + + public Transfer toTransfer() + { + return + Transfer + .builder() + .id(id) + .payer(payer) + .payee(payee) + .amount(amount) + .build(); + } + + + public static TransferDTO of(Transfer transfer) + { + return + TransferDTO + .builder() + .id(transfer.getId()) + .payer(transfer.getPayer()) + .payee(transfer.getPayee()) + .amount(transfer.getAmount()) + .state(transfer.getState()) + .build(); + } +} diff --git a/transfer/src/main/java/de/juplo/kafka/payment/transfer/persistence/InMemoryTransferRepository.java b/transfer/src/main/java/de/juplo/kafka/payment/transfer/persistence/InMemoryTransferRepository.java new file mode 100644 index 0000000..5ef2094 --- /dev/null +++ b/transfer/src/main/java/de/juplo/kafka/payment/transfer/persistence/InMemoryTransferRepository.java @@ -0,0 +1,81 @@ +package de.juplo.kafka.payment.transfer.persistence; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import de.juplo.kafka.payment.transfer.domain.Transfer; +import de.juplo.kafka.payment.transfer.domain.TransferRepository; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + + +@Component +@RequiredArgsConstructor +@Slf4j +public class InMemoryTransferRepository implements TransferRepository +{ + private final Map map = new HashMap<>(); + private final ObjectMapper mapper; + + + @Override + public synchronized void store(Transfer transfer) + { + Optional + .ofNullable(map.get(transfer.getId())) + .ifPresent( + json -> + { + throw new IllegalArgumentException("Could not overwrite " + json + " with " + transfer); + }); + + try + { + map.put(transfer.getId(), mapper.writeValueAsString(transfer)); + } + catch (JsonProcessingException e) + { + log.error("Could not convert Transfer.class: {}", transfer, e); + } + } + + @Override + public synchronized Optional get(Long id) + { + return + Optional + .ofNullable(map.get(id)) + .map(json -> { + try + { + return mapper.readValue(json, Transfer.class); + } + catch (JsonProcessingException e) + { + throw new RuntimeException("Could not convert JSON: " + json, e); + } + }); + } + + @Override + public synchronized void update(Long id, Transfer.State oldState, Transfer.State newState) + { + Transfer transfer = get(id).orElseThrow(() -> new IllegalArgumentException("Could not find transfer " + id)); + + if (transfer.getState() != oldState) + throw new IllegalArgumentException(("Unexpectd state for " + transfer + ", expected: " + oldState)); + + transfer.setState(newState); + store(transfer); + } + + @Override + public void remove(Long id) + { + map.remove(id); + } +} -- 2.20.1