feat(dashboard): add price history, caching, and i18n
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Features\Dashboard\Clients;
|
||||
use App\Features\Dashboard\Contracts\PriceClient;
|
||||
use Exception;
|
||||
use Illuminate\Http\Client\Factory;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Throwable;
|
||||
|
||||
@@ -14,14 +15,14 @@ class ZonneplanPriceClient implements PriceClient
|
||||
private readonly Factory $http,
|
||||
) {}
|
||||
|
||||
public function getElectricityPrices(): array
|
||||
public function getElectricityPrices(): Collection
|
||||
{
|
||||
return $this->get('/energy-prices/electricity/upcoming');
|
||||
return collect($this->get('/energy-prices/electricity/upcoming'));
|
||||
}
|
||||
|
||||
public function getGasPrices(): array
|
||||
public function getGasPrices(): Collection
|
||||
{
|
||||
return $this->get('/energy-prices/gas/upcoming');
|
||||
return collect($this->get('/energy-prices/gas/upcoming'));
|
||||
}
|
||||
|
||||
/** @return array<int, array<string, mixed>> */
|
||||
|
||||
@@ -2,9 +2,11 @@
|
||||
|
||||
namespace App\Features\Dashboard\Contracts;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
interface PriceClient
|
||||
{
|
||||
public function getElectricityPrices(): array;
|
||||
public function getElectricityPrices(): Collection;
|
||||
|
||||
public function getGasPrices(): array;
|
||||
public function getGasPrices(): Collection;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace App\Features\Dashboard\Controllers;
|
||||
|
||||
use App\Features\Dashboard\Data\DashboardData;
|
||||
use App\Features\Dashboard\Services\DashboardService;
|
||||
use App\Http\Controllers\Controller;
|
||||
|
||||
@@ -10,6 +9,6 @@ class DashboardController extends Controller
|
||||
{
|
||||
public function index(DashboardService $dashboardService)
|
||||
{
|
||||
return inertia('dashboard::index', DashboardData::from($dashboardService->getDashboardData()));
|
||||
return $dashboardService->getDashboardData();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,16 +2,24 @@
|
||||
|
||||
namespace App\Features\Dashboard\Data;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Spatie\LaravelData\Data;
|
||||
|
||||
class DashboardData extends Data
|
||||
{
|
||||
/**
|
||||
* @property array<ElectricityData> $electricity
|
||||
* @property array<GasData> $gas
|
||||
* @property Collection<ElectricityData> $electricityHistory
|
||||
* @property Collection<GasData> $gasHistory
|
||||
*/
|
||||
public function __construct(
|
||||
public array $electricity,
|
||||
public array $gas
|
||||
/** @var array<ElectricityData> */
|
||||
public Collection $electricityHistory,
|
||||
public ?ElectricityData $electricityCheapest,
|
||||
public ?ElectricityData $electricityDurable,
|
||||
public ?ElectricityData $electricityPriciest,
|
||||
/** @var array<ElectricityData> */
|
||||
public Collection $gasHistory,
|
||||
public ?GasData $gasCheapest,
|
||||
public ?GasData $gasPriciest,
|
||||
) {}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ use App\Features\Dashboard\Contracts\PriceRepository as ContractsPriceRepository
|
||||
use App\Features\Dashboard\Data\ElectricityData;
|
||||
use App\Features\Dashboard\Data\GasData;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Override;
|
||||
|
||||
class ZonneplanPriceRepository implements ContractsPriceRepository
|
||||
@@ -18,7 +19,7 @@ class ZonneplanPriceRepository implements ContractsPriceRepository
|
||||
#[Override]
|
||||
public function retrieveElectricity(): Collection
|
||||
{
|
||||
$data = $this->client->getElectricityPrices();
|
||||
$data = Cache::remember('electricity', 3600, fn () => $this->client->getElectricityPrices()->toArray());
|
||||
|
||||
return collect(ElectricityData::collect($data));
|
||||
}
|
||||
@@ -26,7 +27,7 @@ class ZonneplanPriceRepository implements ContractsPriceRepository
|
||||
#[Override]
|
||||
public function retrieveGas(): Collection
|
||||
{
|
||||
$data = $this->client->getGasPrices();
|
||||
$data = Cache::remember('gas', 3600, fn () => $this->client->getGasPrices()->toArray());
|
||||
|
||||
return collect(GasData::collect($data));
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Features\Dashboard\Services;
|
||||
|
||||
use App\Features\Dashboard\Contracts\PriceRepository;
|
||||
use App\Features\Dashboard\Data\DashboardData;
|
||||
|
||||
class DashboardService
|
||||
{
|
||||
@@ -10,11 +11,25 @@ class DashboardService
|
||||
public PriceRepository $repository
|
||||
) {}
|
||||
|
||||
public function getDashboardData(): array
|
||||
public function getDashboardData(): DashboardData
|
||||
{
|
||||
return [
|
||||
'electricity' => $this->repository->retrieveElectricity(),
|
||||
'gas' => $this->repository->retrieveGas(),
|
||||
];
|
||||
$electricity = $this->repository->retrieveElectricity();
|
||||
$eCheapest = $electricity->min('total_price_tax_included');
|
||||
$eDurable = $electricity->min('sustainability_score');
|
||||
$ePriciest = $electricity->max('total_price_tax_included');
|
||||
|
||||
$gas = $this->repository->retrieveGas();
|
||||
$gCheapest = $gas->min('total_price_tax_included');
|
||||
$gPriciest = $gas->min('total_price_tax_included');
|
||||
|
||||
return new DashboardData(
|
||||
$electricity,
|
||||
$eCheapest,
|
||||
$eDurable,
|
||||
$ePriciest,
|
||||
$gas,
|
||||
$gCheapest,
|
||||
$gPriciest
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user