feat: add BrentPriceResource with 30-day line chart widget
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
46
app/Filament/Resources/BrentPriceResource.php
Normal file
46
app/Filament/Resources/BrentPriceResource.php
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Resources;
|
||||||
|
|
||||||
|
use App\Filament\Resources\BrentPriceResource\Pages\ListBrentPrices;
|
||||||
|
use App\Models\BrentPrice;
|
||||||
|
use Filament\Resources\Resource;
|
||||||
|
use Filament\Tables\Columns\TextColumn;
|
||||||
|
use Filament\Tables\Table;
|
||||||
|
|
||||||
|
class BrentPriceResource extends Resource
|
||||||
|
{
|
||||||
|
protected static ?string $model = BrentPrice::class;
|
||||||
|
|
||||||
|
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-currency-dollar';
|
||||||
|
|
||||||
|
protected static string|\UnitEnum|null $navigationGroup = 'Data';
|
||||||
|
|
||||||
|
protected static ?string $navigationLabel = 'Brent Prices';
|
||||||
|
|
||||||
|
protected static ?int $navigationSort = 2;
|
||||||
|
|
||||||
|
public static function table(Table $table): Table
|
||||||
|
{
|
||||||
|
return $table
|
||||||
|
->columns([
|
||||||
|
TextColumn::make('date')
|
||||||
|
->date('d M Y')
|
||||||
|
->sortable(),
|
||||||
|
TextColumn::make('price_usd')
|
||||||
|
->label('Price (USD/barrel)')
|
||||||
|
->numeric(2)
|
||||||
|
->sortable(),
|
||||||
|
])
|
||||||
|
->defaultSort('date', 'desc')
|
||||||
|
->recordActions([])
|
||||||
|
->filters([]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getPages(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'index' => ListBrentPrices::route('/'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Resources\BrentPriceResource\Pages;
|
||||||
|
|
||||||
|
use App\Filament\Resources\BrentPriceResource;
|
||||||
|
use App\Filament\Widgets\BrentPriceChartWidget;
|
||||||
|
use Filament\Resources\Pages\ListRecords;
|
||||||
|
|
||||||
|
class ListBrentPrices extends ListRecords
|
||||||
|
{
|
||||||
|
protected static string $resource = BrentPriceResource::class;
|
||||||
|
|
||||||
|
protected function getHeaderActions(): array
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getHeaderWidgets(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
BrentPriceChartWidget::class,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
40
app/Filament/Widgets/BrentPriceChartWidget.php
Normal file
40
app/Filament/Widgets/BrentPriceChartWidget.php
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Widgets;
|
||||||
|
|
||||||
|
use App\Models\BrentPrice;
|
||||||
|
use Filament\Widgets\ChartWidget;
|
||||||
|
|
||||||
|
class BrentPriceChartWidget extends ChartWidget
|
||||||
|
{
|
||||||
|
protected ?string $heading = 'Brent Crude — Last 30 Days (USD/barrel)';
|
||||||
|
|
||||||
|
protected ?string $pollingInterval = null;
|
||||||
|
|
||||||
|
protected function getData(): array
|
||||||
|
{
|
||||||
|
$prices = BrentPrice::orderBy('date')
|
||||||
|
->where('date', '>=', now()->subDays(30)->toDateString())
|
||||||
|
->get();
|
||||||
|
|
||||||
|
return [
|
||||||
|
'datasets' => [
|
||||||
|
[
|
||||||
|
'label' => 'USD/barrel',
|
||||||
|
'data' => $prices->pluck('price_usd')->map(fn ($p) => (float) $p)->toArray(),
|
||||||
|
'borderColor' => '#f59e0b',
|
||||||
|
'fill' => false,
|
||||||
|
'tension' => 0.3,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'labels' => $prices->pluck('date')
|
||||||
|
->map(fn ($d) => $d->format('d M'))
|
||||||
|
->toArray(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getType(): string
|
||||||
|
{
|
||||||
|
return 'line';
|
||||||
|
}
|
||||||
|
}
|
||||||
22
tests/Feature/Admin/BrentPriceResourceTest.php
Normal file
22
tests/Feature/Admin/BrentPriceResourceTest.php
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Filament\Resources\BrentPriceResource\Pages\ListBrentPrices;
|
||||||
|
use App\Models\BrentPrice;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Livewire\Livewire;
|
||||||
|
|
||||||
|
uses(RefreshDatabase::class);
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
$this->admin = User::factory()->admin()->create();
|
||||||
|
$this->actingAs($this->admin);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('renders the brent price list', function () {
|
||||||
|
$prices = BrentPrice::factory()->count(3)->create();
|
||||||
|
|
||||||
|
Livewire::test(ListBrentPrices::class)
|
||||||
|
->assertOk()
|
||||||
|
->assertCanSeeTableRecords($prices);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user