From 429ffcf72cd67083d49ab2f53462c21965ec7624 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20F=C3=A1bry?= <xfabry@fi.muni.cz> Date: Sat, 25 May 2024 18:16:27 +0200 Subject: [PATCH] Fix the creation of accounts in transaction service --- .../muni/fi/obs/controller/CurrencyController.java | 6 ++++++ .../fi/obs/data/repository/CurrencyRepository.java | 2 ++ .../java/cz/muni/fi/obs/facade/CurrencyFacade.java | 4 ++++ .../java/cz/muni/fi/obs/service/CurrencyService.java | 4 ++++ .../cz/muni/fi/obs/controller/AccountController.java | 3 ++- .../cz/muni/fi/obs/http/CurrencyServiceClient.java | 10 ++++++++++ .../java/cz/muni/fi/obs/service/AccountService.java | 12 ++++++++++-- 7 files changed, 38 insertions(+), 3 deletions(-) diff --git a/currency-service/src/main/java/cz/muni/fi/obs/controller/CurrencyController.java b/currency-service/src/main/java/cz/muni/fi/obs/controller/CurrencyController.java index 420b765..c1bab7e 100644 --- a/currency-service/src/main/java/cz/muni/fi/obs/controller/CurrencyController.java +++ b/currency-service/src/main/java/cz/muni/fi/obs/controller/CurrencyController.java @@ -36,4 +36,10 @@ public class CurrencyController { @RequestParam(defaultValue = "50") Integer pageSize) { return ResponseEntity.ok(currencyFacade.listPage(Pageable.ofSize(pageSize).withPage(page))); } + + @Operation(summary = "Check if currency with code exists") + @GetMapping("/exists") + public boolean currencies(@RequestParam String code) { + return currencyFacade.codeExists(code); + } } diff --git a/currency-service/src/main/java/cz/muni/fi/obs/data/repository/CurrencyRepository.java b/currency-service/src/main/java/cz/muni/fi/obs/data/repository/CurrencyRepository.java index 92c0beb..434eec9 100644 --- a/currency-service/src/main/java/cz/muni/fi/obs/data/repository/CurrencyRepository.java +++ b/currency-service/src/main/java/cz/muni/fi/obs/data/repository/CurrencyRepository.java @@ -10,4 +10,6 @@ import java.util.Optional; public interface CurrencyRepository extends JpaRepository<Currency, String> { Optional<Currency> findByCode(String code); + + boolean existsByCode(String code); } diff --git a/currency-service/src/main/java/cz/muni/fi/obs/facade/CurrencyFacade.java b/currency-service/src/main/java/cz/muni/fi/obs/facade/CurrencyFacade.java index 85d41b9..d82e768 100644 --- a/currency-service/src/main/java/cz/muni/fi/obs/facade/CurrencyFacade.java +++ b/currency-service/src/main/java/cz/muni/fi/obs/facade/CurrencyFacade.java @@ -39,4 +39,8 @@ public class CurrencyFacade { .collect(Collectors.toList()); return new PageImpl<>(dtos, pageRequest, currencies.getTotalElements()); } + + public boolean codeExists(String code) { + return currencyService.codeExists(code); + } } diff --git a/currency-service/src/main/java/cz/muni/fi/obs/service/CurrencyService.java b/currency-service/src/main/java/cz/muni/fi/obs/service/CurrencyService.java index e5e39fe..8d6a438 100644 --- a/currency-service/src/main/java/cz/muni/fi/obs/service/CurrencyService.java +++ b/currency-service/src/main/java/cz/muni/fi/obs/service/CurrencyService.java @@ -22,4 +22,8 @@ public class CurrencyService { public Page<Currency> listPage(Pageable pageable) { return currencyRepository.findAll(pageable); } + + public boolean codeExists(String code) { + return currencyRepository.existsByCode(code); + } } diff --git a/transaction-service/src/main/java/cz/muni/fi/obs/controller/AccountController.java b/transaction-service/src/main/java/cz/muni/fi/obs/controller/AccountController.java index bd08533..84a7905 100644 --- a/transaction-service/src/main/java/cz/muni/fi/obs/controller/AccountController.java +++ b/transaction-service/src/main/java/cz/muni/fi/obs/controller/AccountController.java @@ -48,7 +48,8 @@ public class AccountController { description = "Creates an account for customer from request body", responses = { @ApiResponse(responseCode = "201", description = "Account created successfully"), - @ApiResponse(responseCode = "400", description = "Invalid request body") + @ApiResponse(responseCode = "400", description = "Invalid request body"), + @ApiResponse(responseCode = "404", description = "Currency not found") } ) @PostMapping(value = "/create", consumes = MediaType.APPLICATION_JSON_VALUE) diff --git a/transaction-service/src/main/java/cz/muni/fi/obs/http/CurrencyServiceClient.java b/transaction-service/src/main/java/cz/muni/fi/obs/http/CurrencyServiceClient.java index 83766fa..576b4dd 100644 --- a/transaction-service/src/main/java/cz/muni/fi/obs/http/CurrencyServiceClient.java +++ b/transaction-service/src/main/java/cz/muni/fi/obs/http/CurrencyServiceClient.java @@ -3,6 +3,7 @@ package cz.muni.fi.obs.http; import java.util.Optional; import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import cz.muni.fi.obs.api.CurrencyExchangeRequest; @@ -21,6 +22,9 @@ public interface CurrencyServiceClient { @PostMapping("/v1/currencies/exchange") Optional<CurrencyExchangeResult> getCurrencyExchange(CurrencyExchangeRequest currencyExchangeRequest); + @GetMapping("/v1/currencies/exists") + boolean currencyExists(String currencyCode); + @Slf4j class Fallback implements CurrencyServiceClient { @@ -29,5 +33,11 @@ public interface CurrencyServiceClient { log.warn("Could not get currency exchange rate, returning null"); return Optional.empty(); } + + @Override + public boolean currencyExists(String currencyCode) { + log.warn("Could not check if currency exists, returning false"); + return false; + } } } diff --git a/transaction-service/src/main/java/cz/muni/fi/obs/service/AccountService.java b/transaction-service/src/main/java/cz/muni/fi/obs/service/AccountService.java index b4c1801..5219079 100644 --- a/transaction-service/src/main/java/cz/muni/fi/obs/service/AccountService.java +++ b/transaction-service/src/main/java/cz/muni/fi/obs/service/AccountService.java @@ -3,6 +3,7 @@ package cz.muni.fi.obs.service; import java.util.List; import java.util.UUID; +import cz.muni.fi.obs.http.CurrencyServiceClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -17,13 +18,20 @@ import cz.muni.fi.obs.exceptions.ResourceNotFoundException; public class AccountService { private final AccountRepository repository; + private final CurrencyServiceClient client; @Autowired - public AccountService(AccountRepository repository) { + public AccountService(AccountRepository repository, CurrencyServiceClient client) { this.repository = repository; - } + this.client = client; + } public AccountDbo createAccount(AccountCreateDto accountCreateDto) { + + var exists = client.currencyExists(accountCreateDto.currencyCode()); + if (!exists) { + throw new ResourceNotFoundException("Currency with code " + accountCreateDto.currencyCode() + " does not exist"); + } var accountDbo = AccountDbo.builder() .id(UUID.randomUUID().toString()) .currencyCode(accountCreateDto.currencyCode()) -- GitLab