Skip to content

Commit 1a59977

Browse files
committed
on request download and store rate when no already present in database
1 parent 5e24ce1 commit 1a59977

3 files changed

Lines changed: 20 additions & 4 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ Final configuration is stored in `/etc/cnb-cache/cnb-cache.env` file
5151

5252
After installation the currencies listing is available on the `/cnb-cache/` path.
5353

54+
you can use following URL parameters:
55+
56+
* `currency` - currency code (default: CZK)
57+
* `when` - date of rate (default: today)
58+
* `date` - date of rate in format YYYY-MM-DD
59+
* `age` - age of rate in days (default: 0)
60+
* `when` - yesterday, beforeyesterday - today is default
61+
5462
* <http://localhost/cnb-cache/?currency=eur> - todays EUR rate
5563
* <http://localhost/cnb-cache/?currency=USD&when=yesterday> - yesterday $ rate
5664

src/SpojeNet/Cnb/ExchangeRate.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,10 @@ public function cnbCsv2Data(string $ratesRaw) {
9090
/**
9191
* Insert Data to SQL.
9292
*
93-
* @return bool
93+
* @return array<string,array<string,string|float|int>>
9494
*/
95-
public function storeDay(int $dayBack = 0): void {
95+
public function storeDay(int $dayBack = 0): array {
96+
$stored = [];
9697
$datum = self::dateBeforeDays($dayBack);
9798

9899
foreach ($this->cnbCsv2Data($this->exchangeRateRaw($datum)) as $currencyData) {
@@ -101,13 +102,15 @@ public function storeDay(int $dayBack = 0): void {
101102
if (\array_key_exists($currencyData['code'], $this->currencies)) {
102103
if (!$this->recordExist(['code' => $currencyData['code'], 'date' => $datum])) {
103104
if ($this->insertToSQL($currencyData)) {
105+
$stored[$currencyData['code']] = $currencyData;
104106
$this->addStatusMessage(sprintf(_('Stored: %s for %s'), $currencyData['code'], $currencyData['date']), 'success');
105107
}
106108
} else {
107109
$this->addStatusMessage(sprintf(_('Already present: %s for %s'), $currencyData['code'], $currencyData['date']), 'warning');
108110
}
109111
}
110112
}
113+
return $stored;
111114
}
112115

113116
public function dropOlder(int $days): void {
@@ -127,7 +130,7 @@ public function getRateInfo($currency, $age): array {
127130
if($rateInfo){
128131
$result = $rateInfo[0];
129132
} else {
130-
$result['message'] = 'no record for '. self::dateBeforeDays($age);
133+
$result = $this->storeDay($age)[$currency];
131134
}
132135

133136
$result['age'] = $age;

src/index.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
$currency = $_GET['currency'] ?? null;
2828
$when = $_GET['when'] ?? null;
29+
$date = $when = $_GET['date'] ?? null;
2930

3031
switch ($when) {
3132
case 'yesterday':
@@ -38,7 +39,11 @@
3839
break;
3940

4041
default:
41-
$age = isset($_GET['age']) ? (int) $_GET['age'] : 0;
42+
if($date){
43+
$age = (new DateTime())->diff(new DateTime($date))->days;
44+
} else {
45+
$age = isset($_GET['age']) ? (int) $_GET['age'] : 0;
46+
}
4247

4348
break;
4449
}

0 commit comments

Comments
 (0)