From 773cb80ca0e1979a2ed5108fe748613e9c64b680 Mon Sep 17 00:00:00 2001 From: Pawel Zalejko Date: Sun, 25 Sep 2022 20:01:10 +0200 Subject: [PATCH] removed aggregate from main --- .../cdc/demo/cdc/AccountStateCdc.java | 41 ------------------- .../demo/cdc/AccountStateCdcRepository.java | 12 ------ .../cdc/demo/cdc/AccountStateCdcService.java | 30 -------------- 3 files changed, 83 deletions(-) delete mode 100644 app/src/main/java/com/gmail/pzalejko/cdc/demo/cdc/AccountStateCdc.java delete mode 100644 app/src/main/java/com/gmail/pzalejko/cdc/demo/cdc/AccountStateCdcRepository.java delete mode 100644 app/src/main/java/com/gmail/pzalejko/cdc/demo/cdc/AccountStateCdcService.java diff --git a/app/src/main/java/com/gmail/pzalejko/cdc/demo/cdc/AccountStateCdc.java b/app/src/main/java/com/gmail/pzalejko/cdc/demo/cdc/AccountStateCdc.java deleted file mode 100644 index 2e6a298..0000000 --- a/app/src/main/java/com/gmail/pzalejko/cdc/demo/cdc/AccountStateCdc.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.gmail.pzalejko.cdc.demo.cdc; - -import jakarta.persistence.*; -import jakarta.validation.constraints.NotNull; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; - -import java.math.BigDecimal; - -// here, are just longs, not any JOINs... -// we get 'before' and 'after' state for free -@Entity -@Table(indexes = { - @Index(name = "accountId", columnList = "accountId", unique = true) -}) -@NoArgsConstructor -class AccountStateCdc { - - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - @Getter - private Long id; - - @NotNull - private String name; - - @NotNull - private Long accountId; - @NotNull - private Long accountOwnerId; - - @Setter - private BigDecimal balance = BigDecimal.ZERO; - - AccountStateCdc(String name, Long accountId, Long accountOwnerId) { - this.name = name; - this.accountId = accountId; - this.accountOwnerId = accountOwnerId; - } -} diff --git a/app/src/main/java/com/gmail/pzalejko/cdc/demo/cdc/AccountStateCdcRepository.java b/app/src/main/java/com/gmail/pzalejko/cdc/demo/cdc/AccountStateCdcRepository.java deleted file mode 100644 index c66a541..0000000 --- a/app/src/main/java/com/gmail/pzalejko/cdc/demo/cdc/AccountStateCdcRepository.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.gmail.pzalejko.cdc.demo.cdc; - -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; - -import java.util.Optional; - -@Repository -interface AccountStateCdcRepository extends JpaRepository { - - Optional findByAccountId(Long accountId); -} diff --git a/app/src/main/java/com/gmail/pzalejko/cdc/demo/cdc/AccountStateCdcService.java b/app/src/main/java/com/gmail/pzalejko/cdc/demo/cdc/AccountStateCdcService.java deleted file mode 100644 index 340cd2e..0000000 --- a/app/src/main/java/com/gmail/pzalejko/cdc/demo/cdc/AccountStateCdcService.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.gmail.pzalejko.cdc.demo.cdc; - -import com.gmail.pzalejko.cdc.demo.account.domain.Account; -import lombok.NonNull; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -@Service -@RequiredArgsConstructor -public class AccountStateCdcService { - - private final AccountStateCdcRepository repository; - - @Transactional("transactionManager") - public void updateStateCdc(@NonNull Account account) { - var byAccountId = getAccountStateCdc(account); - byAccountId.setBalance(account.getBalance()); - repository.save(byAccountId); - } - - private AccountStateCdc getAccountStateCdc(Account account) { - return repository.findByAccountId(account.getId()) - .orElse(new AccountStateCdc( - account.getAccountOwner().getFullName(), - account.getId(), - account.getAccountOwner().getId()) - ); - } -}