94 lines
2.5 KiB
Java
94 lines
2.5 KiB
Java
package tk.artikus.angular;
|
|
|
|
import java.time.LocalDate;
|
|
import java.time.LocalDateTime;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
import jakarta.annotation.PostConstruct;
|
|
import lombok.Data;
|
|
import lombok.NoArgsConstructor;
|
|
import services.Price;
|
|
import services.tasks.Currency;
|
|
|
|
@Data
|
|
@NoArgsConstructor
|
|
class DataCena {
|
|
LocalDate data;
|
|
float cena;
|
|
}
|
|
|
|
@Data
|
|
class Rate {
|
|
String no;
|
|
LocalDate effectiveDate;
|
|
float mid;
|
|
}
|
|
|
|
@Data
|
|
class ExchangeRatesSeries {
|
|
String table;
|
|
String currency;
|
|
String code;
|
|
|
|
Rate[] rates;
|
|
|
|
}
|
|
|
|
@Component
|
|
public class NbpTask {
|
|
|
|
final String nbp_url_gold = "http://api.nbp.pl/api/cenyzlota";
|
|
final String nbp_url_exchanges = "http://api.nbp.pl/api/exchangerates/rates/A/%s";
|
|
|
|
@Autowired
|
|
RestTemplate restTemplate;
|
|
|
|
@Autowired
|
|
PricesContainer pricesContainer;
|
|
|
|
|
|
@PostConstruct
|
|
@Scheduled(cron = "0 5 10 * * *")
|
|
void getNbpData( ) {
|
|
Currency[] curencies =
|
|
{ Currency.Dollar, Currency.Euro, Currency.JapaneseYen, Currency.Pound, Currency.ChineJuan, Currency.SwissFranc };
|
|
for( Currency curency : curencies ) {
|
|
|
|
String currencyCode = curency.getCode();
|
|
String url = String.format( nbp_url_exchanges, currencyCode );
|
|
ExchangeRatesSeries exchangeRatesSeries = restTemplate.getForObject( url,
|
|
ExchangeRatesSeries.class );
|
|
Rate rate = exchangeRatesSeries.rates[0];
|
|
long toHundrets = (long) (rate.getMid() *100);
|
|
Price price = new Price( currencyCode, toHundrets, Currency.Zł.getCode(), rate.effectiveDate );
|
|
pricesContainer.save( currencyCode, price );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@PostConstruct
|
|
@Scheduled(cron = "0 5 10 * * *")
|
|
void getNbpGoldData( ) {
|
|
DataCena[] aDataCena = restTemplate.getForObject( nbp_url_gold, DataCena[].class );
|
|
DataCena dataCena = aDataCena[0];
|
|
|
|
LocalDate date = dataCena.getData();
|
|
float cena = dataCena.getCena() * (float)31.1034768;
|
|
long toHundrets = (long) ( cena *100 );
|
|
String metalSymbol = Metal.Gold.getSymbol();
|
|
Price price = new Price( metalSymbol, toHundrets, Currency.Zł.getCode(), date );
|
|
pricesContainer.save( metalSymbol, price );
|
|
|
|
}
|
|
|
|
public Price getPrice(String symbol) {
|
|
return pricesContainer.getPrice( symbol );
|
|
}
|
|
|
|
}
|