aboutsummaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorCalvin Morrison <calvin@fastmailteam.com>2022-08-02 14:36:09 -0400
committerCalvin Morrison <calvin@fastmailteam.com>2022-08-02 14:36:09 -0400
commitf2aff7518be55da0fd250e04a4bfc1bbbd5a3d0a (patch)
tree6d5a54ecaf98175aa9db2ebfbf3182c8f3ddf2b3 /app
parent2466d29fe2319c1057cca7cf1e1977451088276e (diff)
data model, query and storage works, frontend display basic last report
Diffstat (limited to 'app')
-rw-r--r--app/Console/Commands/updateRates.php79
-rw-r--r--app/Console/Kernel.php1
-rw-r--r--app/Models/Currency.php14
-rw-r--r--app/Models/Rates.php23
4 files changed, 117 insertions, 0 deletions
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 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Models\Rates;
+use App\Models\Currency;
+
+use Illuminate\Console\Command;
+use \GuzzleHttp\Client;
+
+class updateRates extends Command
+{
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'rates:update';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = 'Pull updated rates from our API';
+
+ /**
+ * Create a new command instance.
+ *
+ * @return void
+ */
+ public function __construct()
+ {
+ parent::__construct();
+ }
+
+ /**
+ * Execute the console command.
+ *
+ * @return int
+ */
+ public function handle()
+ {
+ $client = new \GuzzleHttp\Client();
+ $res = $client->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();
}
/**
diff --git a/app/Models/Currency.php b/app/Models/Currency.php
new file mode 100644
index 0000000..f64bf15
--- /dev/null
+++ b/app/Models/Currency.php
@@ -0,0 +1,14 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Currency extends Model
+{
+ protected $connection = 'sqlite';
+ protected $table = 'currency';
+ protected $fillable = [
+ 'name', 'unit', 'description'
+];
+};
diff --git a/app/Models/Rates.php b/app/Models/Rates.php
new file mode 100644
index 0000000..5cabf1e
--- /dev/null
+++ b/app/Models/Rates.php
@@ -0,0 +1,23 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Rates extends Model
+{
+ protected $connection = 'sqlite';
+
+ protected $fillable = [
+ 'currency', 'relative', 'value'
+ ];
+
+ public function currency()
+ {
+ return $this->belongsTo(Currency::class, 'currency_id');
+ }
+ public function relative()
+ {
+ return $this->belongsTo(Currency::class, 'relative_id');
+ }
+}