2026-05-02 17:10:00 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Models\Asset;
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
use Illuminate\Http\JsonResponse;
|
2026-05-03 01:30:57 +02:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
2026-05-02 17:10:00 +02:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
|
|
class TrackerController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function show(): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$tracker = User::default()->tracker;
|
|
|
|
|
|
|
|
|
|
if (! $tracker) {
|
|
|
|
|
return response()->json(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response()->json($tracker->load('asset'));
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-03 01:30:57 +02:00
|
|
|
public function store(Request $request): RedirectResponse|JsonResponse
|
2026-05-02 17:10:00 +02:00
|
|
|
{
|
|
|
|
|
$validated = $request->validate([
|
|
|
|
|
'label' => 'required|string|max:255',
|
|
|
|
|
'unit' => 'required|string|max:50',
|
|
|
|
|
'price_tracking_enabled' => 'boolean',
|
|
|
|
|
'symbol' => 'nullable|string|max:10',
|
|
|
|
|
'full_name' => 'nullable|string|max:255',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$user = User::default();
|
|
|
|
|
|
2026-05-02 18:17:42 +02:00
|
|
|
if ($user->tracker) {
|
|
|
|
|
return response()->json(['error' => 'Tracker already exists.'], 409);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-02 17:10:00 +02:00
|
|
|
$assetId = null;
|
|
|
|
|
if (! empty($validated['symbol'])) {
|
|
|
|
|
$asset = Asset::findOrCreateBySymbol($validated['symbol'], $validated['full_name'] ?? null);
|
|
|
|
|
$assetId = $asset->id;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-03 01:30:57 +02:00
|
|
|
$user->tracker()->create([
|
2026-05-02 17:10:00 +02:00
|
|
|
'label' => $validated['label'],
|
|
|
|
|
'unit' => $validated['unit'],
|
|
|
|
|
'price_tracking_enabled' => $validated['price_tracking_enabled'] ?? false,
|
|
|
|
|
'asset_id' => $assetId,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-05-03 01:30:57 +02:00
|
|
|
return back();
|
2026-05-02 17:10:00 +02:00
|
|
|
}
|
|
|
|
|
|
2026-05-03 01:30:57 +02:00
|
|
|
public function update(Request $request): RedirectResponse|JsonResponse
|
2026-05-02 17:10:00 +02:00
|
|
|
{
|
|
|
|
|
$validated = $request->validate([
|
|
|
|
|
'label' => 'sometimes|string|max:255',
|
|
|
|
|
'unit' => 'sometimes|string|max:50',
|
|
|
|
|
'price_tracking_enabled' => 'sometimes|boolean',
|
|
|
|
|
'symbol' => 'nullable|string|max:10',
|
|
|
|
|
'full_name' => 'nullable|string|max:255',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$tracker = User::default()->tracker;
|
|
|
|
|
|
|
|
|
|
if (! $tracker) {
|
|
|
|
|
return response()->json(['error' => 'No tracker found.'], 404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (array_key_exists('symbol', $validated)) {
|
|
|
|
|
if ($validated['symbol']) {
|
|
|
|
|
$asset = Asset::findOrCreateBySymbol($validated['symbol'], $validated['full_name'] ?? null);
|
|
|
|
|
$tracker->asset_id = $asset->id;
|
|
|
|
|
} else {
|
|
|
|
|
$tracker->asset_id = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-02 18:17:42 +02:00
|
|
|
$update = [];
|
|
|
|
|
if (isset($validated['label'])) {
|
|
|
|
|
$update['label'] = $validated['label'];
|
|
|
|
|
}
|
|
|
|
|
if (isset($validated['unit'])) {
|
|
|
|
|
$update['unit'] = $validated['unit'];
|
|
|
|
|
}
|
|
|
|
|
if (array_key_exists('price_tracking_enabled', $validated)) {
|
|
|
|
|
$update['price_tracking_enabled'] = $validated['price_tracking_enabled'];
|
|
|
|
|
}
|
|
|
|
|
if (array_key_exists('symbol', $validated)) {
|
|
|
|
|
$update['asset_id'] = $tracker->asset_id;
|
|
|
|
|
}
|
2026-05-02 17:10:00 +02:00
|
|
|
|
|
|
|
|
$tracker->update($update);
|
|
|
|
|
|
2026-05-03 01:30:57 +02:00
|
|
|
return back();
|
2026-05-02 17:10:00 +02:00
|
|
|
}
|
|
|
|
|
}
|