Moved TransferService into package ports
authorKai Moritz <kai@juplo.de>
Sat, 19 Jun 2021 12:51:25 +0000 (14:51 +0200)
committerKai Moritz <kai@juplo.de>
Sat, 19 Jun 2021 16:22:29 +0000 (18:22 +0200)
src/main/java/de/juplo/kafka/payment/transfer/TransferServiceApplication.java
src/main/java/de/juplo/kafka/payment/transfer/domain/TransferService.java [deleted file]
src/main/java/de/juplo/kafka/payment/transfer/ports/TransferService.java [new file with mode: 0644]

index 557b857..7e90c57 100644 (file)
@@ -7,8 +7,8 @@ import de.juplo.kafka.payment.transfer.adapter.NoOpMessageService;
 import de.juplo.kafka.payment.transfer.adapter.TransferConsumer;
 import de.juplo.kafka.payment.transfer.adapter.TransferController;
 import de.juplo.kafka.payment.transfer.domain.Transfer;
-import de.juplo.kafka.payment.transfer.domain.TransferService;
 import de.juplo.kafka.payment.transfer.ports.TransferRepository;
+import de.juplo.kafka.payment.transfer.ports.TransferService;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.kafka.clients.consumer.ConsumerConfig;
 import org.apache.kafka.clients.consumer.KafkaConsumer;
diff --git a/src/main/java/de/juplo/kafka/payment/transfer/domain/TransferService.java b/src/main/java/de/juplo/kafka/payment/transfer/domain/TransferService.java
deleted file mode 100644 (file)
index ed1a98e..0000000
+++ /dev/null
@@ -1,91 +0,0 @@
-package de.juplo.kafka.payment.transfer.domain;
-
-
-import de.juplo.kafka.payment.transfer.ports.*;
-import lombok.RequiredArgsConstructor;
-import lombok.extern.slf4j.Slf4j;
-
-import java.util.Optional;
-
-import static de.juplo.kafka.payment.transfer.domain.Transfer.State.CHECKED;
-import static de.juplo.kafka.payment.transfer.domain.Transfer.State.CREATED;
-
-
-@Slf4j
-@RequiredArgsConstructor
-public class TransferService implements CreateTransferUseCase, HandleStateChangeUseCase, GetTransferUseCase
-{
-  private final TransferRepository repository;
-  private final MessagingService messagingService;
-
-  @Override
-  public void create(Long id, Long payer, Long payee, Integer amount)
-  {
-    repository
-        .get(id)
-        .ifPresentOrElse(
-            stored -> log.info(
-                "transfer already exisits: {}, ignoring: id={}, payer={}, payee={}, amount={}",
-                stored,
-                payer,
-                payee,
-                amount),
-            () ->
-            {
-              Transfer transfer =
-                  Transfer
-                      .builder()
-                      .id(id)
-                      .payer(payer)
-                      .payee(payee)
-                      .amount(amount)
-                      .build();
-
-              log.info("creating transfer: {}", transfer);
-              repository.store(transfer);
-              messagingService.send(transfer.getId(), CREATED);
-            });
-  }
-
-  @Override
-  public void handleStateChange(Long id, Transfer.State state)
-  {
-    get(id)
-        .ifPresentOrElse(
-            transfer ->
-            {
-              switch (state)
-              {
-                case CREATED:
-
-                  transfer.setState(CREATED);
-                  repository.store(transfer);
-                  check(transfer);
-                  break;
-
-                case CHECKED:
-
-                  transfer.setState(CHECKED);
-                  repository.store(transfer);
-                  // TODO: What's next...?
-                  break;
-
-                default:
-
-                  log.warn("TODO: handle {} state {}", state.foreign ? "foreign" : "domain", state);
-              }
-            },
-            () -> log.error("unknown transfer: {}", id));
-  }
-
-  private void check(Transfer transfer)
-  {
-    // TODO: Do some time consuming checks...
-    messagingService.send(transfer.getId(), CHECKED);
-  }
-
-  public Optional<Transfer> get(Long id)
-  {
-    return repository.get(id);
-  }
-}
diff --git a/src/main/java/de/juplo/kafka/payment/transfer/ports/TransferService.java b/src/main/java/de/juplo/kafka/payment/transfer/ports/TransferService.java
new file mode 100644 (file)
index 0000000..d4eb3ef
--- /dev/null
@@ -0,0 +1,91 @@
+package de.juplo.kafka.payment.transfer.ports;
+
+
+import de.juplo.kafka.payment.transfer.domain.Transfer;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+
+import java.util.Optional;
+
+import static de.juplo.kafka.payment.transfer.domain.Transfer.State.CHECKED;
+import static de.juplo.kafka.payment.transfer.domain.Transfer.State.CREATED;
+
+
+@Slf4j
+@RequiredArgsConstructor
+public class TransferService implements CreateTransferUseCase, HandleStateChangeUseCase, GetTransferUseCase
+{
+  private final TransferRepository repository;
+  private final MessagingService messagingService;
+
+  @Override
+  public void create(Long id, Long payer, Long payee, Integer amount)
+  {
+    repository
+        .get(id)
+        .ifPresentOrElse(
+            stored -> log.info(
+                "transfer already exisits: {}, ignoring: id={}, payer={}, payee={}, amount={}",
+                stored,
+                payer,
+                payee,
+                amount),
+            () ->
+            {
+              Transfer transfer =
+                  Transfer
+                      .builder()
+                      .id(id)
+                      .payer(payer)
+                      .payee(payee)
+                      .amount(amount)
+                      .build();
+
+              log.info("creating transfer: {}", transfer);
+              repository.store(transfer);
+              messagingService.send(transfer.getId(), CREATED);
+            });
+  }
+
+  @Override
+  public void handleStateChange(Long id, Transfer.State state)
+  {
+    get(id)
+        .ifPresentOrElse(
+            transfer ->
+            {
+              switch (state)
+              {
+                case CREATED:
+
+                  transfer.setState(CREATED);
+                  repository.store(transfer);
+                  check(transfer);
+                  break;
+
+                case CHECKED:
+
+                  transfer.setState(CHECKED);
+                  repository.store(transfer);
+                  // TODO: What's next...?
+                  break;
+
+                default:
+
+                  log.warn("TODO: handle {} state {}", state.foreign ? "foreign" : "domain", state);
+              }
+            },
+            () -> log.error("unknown transfer: {}", id));
+  }
+
+  private void check(Transfer transfer)
+  {
+    // TODO: Do some time consuming checks...
+    messagingService.send(transfer.getId(), CHECKED);
+  }
+
+  public Optional<Transfer> get(Long id)
+  {
+    return repository.get(id);
+  }
+}