-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dodany NBPExchangeService i wszystko wokół
dodać handling odpowiedzi
- Loading branch information
Showing
9 changed files
with
93 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/java/pl/janis/komornik/dto/ExchangeRateResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package pl.janis.komornik.dto; | ||
|
||
public record ExchangeRateResponse( | ||
String table, | ||
String currency, | ||
String code, | ||
Rate[] rates | ||
) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package pl.janis.komornik.dto; | ||
|
||
import java.math.BigDecimal; | ||
|
||
public record Rate( | ||
String no, | ||
String effectiveDate, | ||
BigDecimal mid | ||
) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/java/pl/janis/komornik/service/NBPExchangeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package pl.janis.komornik.service; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
import pl.janis.komornik.dto.ExchangeRateResponse; | ||
|
||
import java.math.BigDecimal; | ||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
@Service | ||
public class NBPExchangeService { | ||
|
||
public BigDecimal getExchangeRate(BigDecimal amount, String currency) { | ||
LocalDate today = LocalDate.now(); | ||
String formattedDate = today.format(DateTimeFormatter.ISO_LOCAL_DATE); | ||
String url = String.format("https://api.nbp.pl/api/exchangerates/rates/a/%s/%s/?format=json", currency, formattedDate); | ||
RestTemplate restTemplate = new RestTemplate(); | ||
ExchangeRateResponse result = restTemplate.getForObject(url, ExchangeRateResponse.class); | ||
if (result != null) { | ||
return result.rates()[0].mid().multiply(amount); | ||
} | ||
return BigDecimal.ONE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters