Skip to content
Snippets Groups Projects
Commit 429ffcf7 authored by Filip Fábry's avatar Filip Fábry
Browse files

Fix the creation of accounts in transaction service

parent d66931a4
No related branches found
No related tags found
1 merge request!46Fix the creation of accounts in transaction service
......@@ -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);
}
}
......@@ -10,4 +10,6 @@ import java.util.Optional;
public interface CurrencyRepository extends JpaRepository<Currency, String> {
Optional<Currency> findByCode(String code);
boolean existsByCode(String code);
}
......@@ -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);
}
}
......@@ -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);
}
}
......@@ -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)
......
......@@ -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;
}
}
}
......@@ -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())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment