From 41f9374a05276e5f8caf5412a49c58b47f06b51f Mon Sep 17 00:00:00 2001 From: sabexzero Date: Mon, 1 Apr 2024 23:12:10 +0300 Subject: [PATCH] The last attempt to design an application "on the go", then UML diagrams with a fully thought-out application architecture will appear in the repository. The method of buying cryptocurrencies has been redesigned --- .../TradeHub/domain/currency/Currency.java | 18 ------- .../example/TradeHub/domain/user/User.java | 1 - .../TradeHub/domain/wallet/CryptoWallet.java | 12 +++++ .../TradeHub/domain/wallet/Wallet.java | 41 ---------------- .../currency/CurrencyRepository.java | 9 ---- .../repository/wallet/WalletRepository.java | 14 ------ .../TradeHub/service/PaymentService.java | 49 ++++++++++++------- .../example/TradeHub/web/HomeController.java | 5 -- 8 files changed, 44 insertions(+), 105 deletions(-) delete mode 100644 src/main/java/com/example/TradeHub/domain/currency/Currency.java delete mode 100644 src/main/java/com/example/TradeHub/domain/wallet/Wallet.java delete mode 100644 src/main/java/com/example/TradeHub/repository/currency/CurrencyRepository.java delete mode 100644 src/main/java/com/example/TradeHub/repository/wallet/WalletRepository.java diff --git a/src/main/java/com/example/TradeHub/domain/currency/Currency.java b/src/main/java/com/example/TradeHub/domain/currency/Currency.java deleted file mode 100644 index 98e27b5..0000000 --- a/src/main/java/com/example/TradeHub/domain/currency/Currency.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.example.TradeHub.domain.currency; - -import io.swagger.v3.oas.annotations.media.Schema; -import org.springframework.data.annotation.Id; -import org.springframework.data.relational.core.mapping.Column; -import org.springframework.data.relational.core.mapping.Table; - -@Schema(description = "The essence of the currency," + - "domain-specific fields were described separately") -@Table("currencies") -public class Currency { - @Id - public Long id; - - @Schema(description = "Contains the code that will be needed to get information using BinanceAPI") - @Column(value = "base_asset") - public String baseAsset; -} diff --git a/src/main/java/com/example/TradeHub/domain/user/User.java b/src/main/java/com/example/TradeHub/domain/user/User.java index f5080da..99d45cc 100644 --- a/src/main/java/com/example/TradeHub/domain/user/User.java +++ b/src/main/java/com/example/TradeHub/domain/user/User.java @@ -1,7 +1,6 @@ package com.example.TradeHub.domain.user; import com.example.TradeHub.domain.wallet.CryptoWallet; -import com.example.TradeHub.domain.wallet.Wallet; import org.springframework.data.annotation.Id; import org.springframework.data.relational.core.mapping.MappedCollection; import org.springframework.data.relational.core.mapping.Table; diff --git a/src/main/java/com/example/TradeHub/domain/wallet/CryptoWallet.java b/src/main/java/com/example/TradeHub/domain/wallet/CryptoWallet.java index a5b492f..44bfe0c 100644 --- a/src/main/java/com/example/TradeHub/domain/wallet/CryptoWallet.java +++ b/src/main/java/com/example/TradeHub/domain/wallet/CryptoWallet.java @@ -3,6 +3,10 @@ import com.example.TradeHub.domain.cryptocurrency.Cryptocurrency; import com.example.TradeHub.domain.user.User; import io.swagger.v3.oas.annotations.media.Schema; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.NoArgsConstructor; +import org.springframework.data.annotation.Id; import org.springframework.data.relational.core.mapping.Column; import org.springframework.data.relational.core.mapping.Table; @@ -11,7 +15,11 @@ @Schema(description = "The essence of the wallet, each wallet is for a separate currency, " + "each user can have many wallets that form his portfolio of cryptocurrencies") @Table(value = "wallets") +@Builder +@AllArgsConstructor +@NoArgsConstructor public class CryptoWallet { + @Id private Long id; @Column(value = "user_id") @@ -24,4 +32,8 @@ public class CryptoWallet { private Long cryptocurrencyId; private Cryptocurrency cryptocurrency; + public void IncreaseBalance(BigDecimal value){ + balance = balance.add(value); + } + } diff --git a/src/main/java/com/example/TradeHub/domain/wallet/Wallet.java b/src/main/java/com/example/TradeHub/domain/wallet/Wallet.java deleted file mode 100644 index 69fdd73..0000000 --- a/src/main/java/com/example/TradeHub/domain/wallet/Wallet.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.example.TradeHub.domain.wallet; - -import com.example.TradeHub.domain.currency.Currency; -import com.example.TradeHub.domain.user.User; -import io.swagger.v3.oas.annotations.media.Schema; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.NoArgsConstructor; -import org.springframework.data.relational.core.mapping.Column; -import org.springframework.data.relational.core.mapping.Table; - -import java.math.BigDecimal; - -@Schema(description = "The essence of the wallet, each wallet is for a separate currency, " + - "each user can have many wallets that form his portfolio of currencies") -@Table(value = "wallets") -@Builder -@AllArgsConstructor -@NoArgsConstructor -public class Wallet { - private Long id; - - @Column(value = "user_id") - private Long userId; - private User user; - - private BigDecimal balance; - - @Column(value = "currency_id") - private Long currencyId; - private Currency currency; - - public void IncreaseBalance(BigDecimal value){ - balance = balance.add(value); - } - - public void DecreaseBalance(BigDecimal value){ - balance = balance.subtract(value); - } - -} diff --git a/src/main/java/com/example/TradeHub/repository/currency/CurrencyRepository.java b/src/main/java/com/example/TradeHub/repository/currency/CurrencyRepository.java deleted file mode 100644 index b64c2d7..0000000 --- a/src/main/java/com/example/TradeHub/repository/currency/CurrencyRepository.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.example.TradeHub.repository.currency; - -import com.example.TradeHub.domain.cryptocurrency.Cryptocurrency; -import com.example.TradeHub.domain.currency.Currency; -import org.springframework.data.repository.CrudRepository; -import org.springframework.data.repository.Repository; - -public interface CurrencyRepository extends CrudRepository { -} diff --git a/src/main/java/com/example/TradeHub/repository/wallet/WalletRepository.java b/src/main/java/com/example/TradeHub/repository/wallet/WalletRepository.java deleted file mode 100644 index 351b0b4..0000000 --- a/src/main/java/com/example/TradeHub/repository/wallet/WalletRepository.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.example.TradeHub.repository.wallet; - -import com.example.TradeHub.domain.currency.Currency; -import com.example.TradeHub.domain.user.User; -import com.example.TradeHub.domain.wallet.CryptoWallet; -import com.example.TradeHub.domain.wallet.Wallet; -import org.springframework.data.repository.CrudRepository; - -import java.util.List; -import java.util.Optional; - -public interface WalletRepository extends CrudRepository { - Optional findByUserAndCurrency(User user, Currency currency); -} diff --git a/src/main/java/com/example/TradeHub/service/PaymentService.java b/src/main/java/com/example/TradeHub/service/PaymentService.java index 1ba2f58..e3bab9a 100644 --- a/src/main/java/com/example/TradeHub/service/PaymentService.java +++ b/src/main/java/com/example/TradeHub/service/PaymentService.java @@ -2,10 +2,12 @@ import com.example.TradeHub.domain.cryptocurrency.Cryptocurrency; import com.example.TradeHub.domain.currency.Currency; +import com.example.TradeHub.domain.specified.CryptoTransaction; import com.example.TradeHub.domain.user.User; -import com.example.TradeHub.domain.wallet.Wallet; +import com.example.TradeHub.domain.wallet.CryptoWallet; +import com.example.TradeHub.repository.specified.CryptoTransactionRepository; import com.example.TradeHub.repository.user.UserRepository; -import com.example.TradeHub.repository.wallet.WalletRepository; +import com.example.TradeHub.repository.wallet.CryptoWalletRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -15,30 +17,43 @@ @Service @RequiredArgsConstructor public class PaymentService { - public final UserRepository userRepository; - public final WalletRepository walletRepository; + private final UserRepository userRepository; + private final CryptoWalletRepository cryptoWalletRepository; + private final CryptoTransactionRepository cryptoTransactionRepository; - public Wallet depositBalance(Long userId, Currency currency, BigDecimal amount){ - User userToDeposit = userRepository.findById(userId).orElseThrow(() -> new RuntimeException("User not found")); - Optional optionalWallet = walletRepository.findByUserAndCurrency(userToDeposit, currency); + + public CryptoWallet buyCryptoCurrency(Long userId, Cryptocurrency cryptocurrencyToBuy, BigDecimal amount, Currency payment, BigDecimal unitPrice){ + User user = userRepository.findById(userId).orElseThrow(() -> new RuntimeException("User not found")); + Optional optionalWallet = cryptoWalletRepository.findByUserAndCryptocurrency(user, cryptocurrencyToBuy); + + //Debiting the Currency account in the amount of amount * UnitPrice occurs at the controller level + + CryptoWallet resultWallet; if (optionalWallet.isPresent()) { - Wallet userWallet = optionalWallet.get(); + CryptoWallet userWallet = optionalWallet.get(); userWallet.IncreaseBalance(amount); - return walletRepository.save(userWallet); + resultWallet = cryptoWalletRepository.save(userWallet); } else { - Wallet newUserWallet = Wallet.builder() + CryptoWallet newUserWallet = CryptoWallet.builder() .balance(amount) - .user(userToDeposit) - .currency(currency) + .user(user) + .cryptocurrency(cryptocurrencyToBuy) .build(); - return walletRepository.save(newUserWallet); + resultWallet = cryptoWalletRepository.save(newUserWallet); } - } - public User buyCryptoCurrency(Long userId, Cryptocurrency cryptocurrency, BigDecimal amount){ - //TODO: Implemented method to buy cryptocurrency - return new User(); + + cryptoTransactionRepository.save( + CryptoTransaction.builder() + .baseAsset(payment.baseAsset) + .quoteAsset(cryptocurrencyToBuy.baseAsset) + .baseAmount(amount.multiply(unitPrice)) + .quoteAmount(amount) + .build() + ); + + return resultWallet; } } diff --git a/src/main/java/com/example/TradeHub/web/HomeController.java b/src/main/java/com/example/TradeHub/web/HomeController.java index 3511758..58a67a7 100644 --- a/src/main/java/com/example/TradeHub/web/HomeController.java +++ b/src/main/java/com/example/TradeHub/web/HomeController.java @@ -1,13 +1,8 @@ package com.example.TradeHub.web; -import com.example.TradeHub.repository.cryptocurrency.CryptoCurrencyRepository; import com.example.TradeHub.repository.currency.CurrencyRepository; -import com.example.TradeHub.repository.wallet.WalletRepository; import lombok.RequiredArgsConstructor; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;