From 56a48eb6b4eb8a6a451512d772bf4298fce8f4f4 Mon Sep 17 00:00:00 2001 From: Boris Rizov Date: Sun, 4 Feb 2024 19:26:07 +0100 Subject: [PATCH] feat: add missing relationships to entities --- .../dao/entity/Wallet.java | 3 ++ .../dao/entity/WalletKey.java | 28 +++++++++++++++---- .../dao/repository/WalletKeyRepository.java | 5 ++++ .../service/WalletService.java | 2 +- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/eclipse/tractusx/managedidentitywallets/dao/entity/Wallet.java b/src/main/java/org/eclipse/tractusx/managedidentitywallets/dao/entity/Wallet.java index 4cb65dba9..dead498f3 100644 --- a/src/main/java/org/eclipse/tractusx/managedidentitywallets/dao/entity/Wallet.java +++ b/src/main/java/org/eclipse/tractusx/managedidentitywallets/dao/entity/Wallet.java @@ -64,6 +64,9 @@ public class Wallet extends MIWBaseEntity { @Convert(converter = StringToDidDocumentConverter.class) private DidDocument didDocument; + @OneToMany(mappedBy = "wallet", cascade = CascadeType.ALL, orphanRemoval = true) + private List walletKeys; + @Transient private List verifiableCredentials; } diff --git a/src/main/java/org/eclipse/tractusx/managedidentitywallets/dao/entity/WalletKey.java b/src/main/java/org/eclipse/tractusx/managedidentitywallets/dao/entity/WalletKey.java index 565ec9692..dd6b59019 100644 --- a/src/main/java/org/eclipse/tractusx/managedidentitywallets/dao/entity/WalletKey.java +++ b/src/main/java/org/eclipse/tractusx/managedidentitywallets/dao/entity/WalletKey.java @@ -22,8 +22,20 @@ package org.eclipse.tractusx.managedidentitywallets.dao.entity; import com.fasterxml.jackson.annotation.JsonIgnore; -import jakarta.persistence.*; -import lombok.*; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.MapsId; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import org.eclipse.tractusx.managedidentitywallets.domain.KeyPair; /** * The type Wallet key. @@ -42,9 +54,6 @@ public class WalletKey extends MIWBaseEntity { @Column(name = "id", columnDefinition = "serial", nullable = false, unique = true) private Long id; - @Column(nullable = false) - private Long walletId; - @Column(nullable = false) private String vaultAccessToken; @@ -57,5 +66,14 @@ public class WalletKey extends MIWBaseEntity { @Column(nullable = false) private String publicKey; + @ManyToOne + @MapsId + @JoinColumn(name = "walletId", columnDefinition = "bigint") + private Wallet wallet; + private String keyId; + + public KeyPair toDto() { + return new KeyPair(keyId, privateKey, publicKey); + } } diff --git a/src/main/java/org/eclipse/tractusx/managedidentitywallets/dao/repository/WalletKeyRepository.java b/src/main/java/org/eclipse/tractusx/managedidentitywallets/dao/repository/WalletKeyRepository.java index 26874d35e..8cd1db318 100644 --- a/src/main/java/org/eclipse/tractusx/managedidentitywallets/dao/repository/WalletKeyRepository.java +++ b/src/main/java/org/eclipse/tractusx/managedidentitywallets/dao/repository/WalletKeyRepository.java @@ -22,6 +22,7 @@ package org.eclipse.tractusx.managedidentitywallets.dao.repository; import com.smartsensesolutions.java.commons.base.repository.BaseRepository; +import org.eclipse.tractusx.managedidentitywallets.dao.entity.Wallet; import org.eclipse.tractusx.managedidentitywallets.dao.entity.WalletKey; import org.springframework.stereotype.Repository; @@ -37,4 +38,8 @@ public interface WalletKeyRepository extends BaseRepository { * @return the by wallet id */ WalletKey getByWalletId(Long id); + + WalletKey findFirstByWallet_Bpn(String bpn); + + WalletKey findFirstByWallet_Did(String did); } diff --git a/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/WalletService.java b/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/WalletService.java index 3dd1435e9..1436d5f6b 100644 --- a/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/WalletService.java +++ b/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/WalletService.java @@ -263,7 +263,7 @@ private Wallet createWallet(CreateWalletRequest request, boolean authority, Stri //Save key walletKeyService.getRepository().save(WalletKey.builder() - .walletId(wallet.getId()) + .wallet(wallet) .keyId(keyId) .referenceKey("dummy ref key, removed once vault setup is ready") .vaultAccessToken("dummy vault access token, removed once vault setup is ready")