refactor(dashboard,history): replace ViewModels with Pinia stores and improve backend

This commit is contained in:
2026-08-05 09:23:18 +02:00
parent a0387126de
commit 90930cd223
29 changed files with 1372 additions and 237 deletions
+44 -9
View File
@@ -13,39 +13,74 @@ use Override;
class ZonneplanPriceRepository implements ContractsPriceRepository
{
private int $cacheTtl;
private string $cachePrefix;
public function __construct(
public PriceClient $client
) {}
) {
$this->cacheTtl = config('cache_prices.ttl', 3600);
$this->cachePrefix = config('cache_prices.prefix', 'zonneplan:');
}
#[Override]
public function retrieveElectricity(): Collection
{
$data = Cache::remember('electricity:upcomming', 3600, fn () => $this->client->getElectricityPrices()->toArray());
$raw = $this->remember(
'electricity:upcoming',
fn () => $this->client->getElectricityPrices()->toArray()
);
return ElectricityData::collect($data, Collection::class);
return ElectricityData::collect($raw, Collection::class);
}
#[Override]
public function retrieveGas(): Collection
{
$data = Cache::remember('gas:upcomming', 3600, fn () => $this->client->getGasPrices()->toArray());
$raw = $this->remember(
'gas:upcoming',
fn () => $this->client->getGasPrices()->toArray()
);
return GasData::collect($data, Collection::class);
return GasData::collect($raw, Collection::class);
}
#[Override]
public function retrieveElectricityForDate(Carbon $date): Collection
{
$data = Cache::remember('electricity:'.$date->format('Y-m-d'), 3600, fn () => $this->client->getElectricityPricesForDate($date)->toArray());
$raw = $this->remember(
'electricity:' . $date->format('Y-m-d'),
fn () => $this->client->getElectricityPricesForDate($date)->toArray()
);
return GasData::collect($data, Collection::class);
return ElectricityData::collect($raw, Collection::class);
}
#[Override]
public function retrieveGasForDate(Carbon $date): Collection
{
$data = Cache::remember('gas:'.$date->format('Y-m-d'), 3600, fn () => $this->client->getGasPricesForDate($date)->toArray());
$raw = $this->remember(
'gas:' . $date->format('Y-m-d'),
fn () => $this->client->getGasPricesForDate($date)->toArray()
);
return GasData::collect($data, Collection::class);
return GasData::collect($raw, Collection::class);
}
/**
* Cache via JSON encoding to ensure clean array structure on retrieval.
*
* @param callable(): array $callback
* @return array
*/
private function remember(string $key, callable $callback): array
{
$cached = Cache::remember(
$this->cachePrefix . $key,
$this->cacheTtl,
fn () => json_encode($callback())
);
return json_decode($cached, true);
}
}