buckets/app/Http/Controllers/ScenarioController.php
2025-12-29 23:32:05 +01:00

63 lines
2 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Actions\CreateBucketAction;
use App\Models\Scenario;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;
class ScenarioController extends Controller
{
public function index(): Response
{
return Inertia::render('Scenarios/Index', [
'scenarios' => Scenario::orderBy('created_at', 'desc')->get()
]);
}
public function show(Scenario $scenario): Response
{
$scenario->load(['buckets' => function ($query) {
$query->orderedBySortOrder();
}]);
return Inertia::render('Scenarios/Show', [
'scenario' => $scenario,
'buckets' => $scenario->buckets->map(function ($bucket) {
return [
'id' => $bucket->id,
'name' => $bucket->name,
'priority' => $bucket->priority,
'sort_order' => $bucket->sort_order,
'allocation_type' => $bucket->allocation_type,
'allocation_value' => $bucket->allocation_value,
'allocation_type_label' => $bucket->getAllocationTypeLabel(),
'formatted_allocation_value' => $bucket->getFormattedAllocationValue(),
'current_balance' => $bucket->getCurrentBalance(),
'has_available_space' => $bucket->hasAvailableSpace(),
'available_space' => $bucket->getAvailableSpace(),
];
})
]);
}
public function store(Request $request): RedirectResponse
{
$request->validate([
'name' => 'required|string|max:255',
]);
$scenario = Scenario::create([
'name' => $request->name,
]);
// Create default buckets using the action
$createBucketAction = new CreateBucketAction();
$createBucketAction->createDefaultBuckets($scenario);
return redirect()->route('scenarios.show', $scenario);
}
}