<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Cache;

class GoldController extends Controller
{
    public function getGoldPrice()
    {
        // 1 minute cache
        $goldPrice = Cache::remember('gold_price_usd', 60, function () {

            $response = Http::get('https://gold-api.com/price/XAU');

            if ($response->successful()) {
                return $response->json();
            }

            return null;
        });

        return response()->json($goldPrice);
    }
}