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 420b7656c1485da2dbadcb921afa3ffde05ac05b..c1bab7e545e10be4c7a6c345b0c58122ff28a36b 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 92c0beb3154a9e64d5d2c00b4d5ea1e5478b9fe2..434eec9d563153b34e53ab97b03e6711f6741310 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 85d41b9a6329d1e4cf7779c0337f9e27083b2c6d..d82e7682545734c48a77fe033f1961cceea42c8a 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 e5e39fe1cb29ca354bdba2abe0bde83dea7b5c68..8d6a43891a5318c4142ae305a8981563c5a010b5 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 bd085336359d5aebe7cf2933f2f0cd16b4ffac96..84a79052e43ea634df7a44ff8946926f9bd82a08 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 83766fac4a0923162f607562fe7d6866d2fc39c5..576b4dda8ea10c5b1cabff7de9df065871551f3b 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 b4c18013e2821a0698eb44404a0c129e52a41308..52190793d7f1a9393e0f9364d2feb12499083127 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())