From f2aff7518be55da0fd250e04a4bfc1bbbd5a3d0a Mon Sep 17 00:00:00 2001 From: Calvin Morrison Date: Tue, 2 Aug 2022 14:36:09 -0400 Subject: data model, query and storage works, frontend display basic last report --- app/Console/Commands/updateRates.php | 79 ++++++++++++++++++++++++++++++++++++ app/Console/Kernel.php | 1 + 2 files changed, 80 insertions(+) create mode 100644 app/Console/Commands/updateRates.php (limited to 'app/Console') diff --git a/app/Console/Commands/updateRates.php b/app/Console/Commands/updateRates.php new file mode 100644 index 0000000..12830db --- /dev/null +++ b/app/Console/Commands/updateRates.php @@ -0,0 +1,79 @@ +get('https://api.coingecko.com/api/v3/exchange_rates', []); + + if($res->getStatusCode() != 200) { + echo "failed to make query to coingecko."; + } + + else { + + $parsed = json_decode($res->getBody(),true); + + if(array_key_exists('rates', $parsed)) { + + foreach($parsed['rates'] as $rate) { + + // grab our BTC object, since all results are relative to it. + $btc = Currency::Where('id', 1)->first(); + + // setup a Currency object if we don't have one already. + if(!$currency = Currency::where('unit', '=', $rate['unit'])->first()) { + $currency = new Currency(['name'=> $rate['name'], 'unit' => $rate['unit']]); + $currency->save(); + } + + // build rate. + $rateObj = new Rates; + $rateObj->value = $rate['value']; + $rateObj->currency_id = $currency->id; + $rateObj->relative_id = $btc->id; + $rateObj->save(); + } + } + + } + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index d8bc1d2..700279d 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -16,6 +16,7 @@ class Kernel extends ConsoleKernel protected function schedule(Schedule $schedule) { // $schedule->command('inspire')->hourly(); + $schedule->command('rates:update')->everyTwoMinutes(); } /** -- cgit v1.2.3