Class CurrencyDataServiceImpl

java.lang.Object
api.mpba.rastvdmy.service.impl.CurrencyDataServiceImpl
All Implemented Interfaces:
CurrencyDataService

@Service public class CurrencyDataServiceImpl extends Object implements CurrencyDataService
Service implementation for managing currency data. This service handles retrieving currency information from the database and external APIs.
  • Constructor Details

    • CurrencyDataServiceImpl

      @Autowired public CurrencyDataServiceImpl(CurrencyDataRepository currencyDataRepository, org.springframework.web.client.RestTemplate restTemplate, org.springframework.jdbc.core.JdbcTemplate jdbcTemplate, TokenVerifierService tokenVerifierService)
      Constructs a new CurrencyDataServiceImpl with the specified repositories, RestTemplate, JdbcTemplate, JwtService, and UserProfileRepository.
      Parameters:
      currencyDataRepository - the repository for currency data
      restTemplate - the RestTemplate for making HTTP requests
      jdbcTemplate - the JdbcTemplate for executing SQL queries
      tokenVerifierService - the service for extracting user token and getting user data from the request
  • Method Details

    • findAllCurrencies

      public List<CurrencyData> findAllCurrencies(jakarta.servlet.http.HttpServletRequest request)
      Retrieves all available currencies.
      Specified by:
      findAllCurrencies in interface CurrencyDataService
      Parameters:
      request - the HTTP request containing user data
      Returns:
      a list of all available currencies
    • findByCurrency

      public CurrencyData findByCurrency(jakarta.servlet.http.HttpServletRequest request, String currencyType)
      Retrieves currency data for a specified currency type. If the currency is not found in the database, it fetches it from an external API.
      Specified by:
      findByCurrency in interface CurrencyDataService
      Parameters:
      request - the HTTP request containing user data
      currencyType - the type of currency to retrieve
      Returns:
      the currency data for the specified type
      Throws:
      ApplicationException - if the specified currency type is invalid or not found
    • convertCurrency

      public CurrencyData convertCurrency(String baseCurrency, String targetCurrency)
      Converts an amount from the base currency to the target currency using an external API.
      Specified by:
      convertCurrency in interface CurrencyDataService
      Parameters:
      baseCurrency - the currency from which to convert
      targetCurrency - the currency to which to convert
      Returns:
      the currency data for the target currency
    • initializeData

      @PostConstruct public void initializeData()
      Initializes the database with currency data and connects the admin user to all currencies.
    • findAllExchangeRates

      @Scheduled(fixedRate=86400000L) public void findAllExchangeRates()
      Retrieves all exchange rates from an external API and updates the database. This method is scheduled to run every 24 hours.
      Specified by:
      findAllExchangeRates in interface CurrencyDataService