2025-07-10 17:37:30 +02:00
|
|
|
<?php
|
|
|
|
|
|
2026-05-02 17:10:00 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2025-07-10 17:37:30 +02:00
|
|
|
namespace App\Http\Controllers\Pricing;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use App\Models\Pricing\AssetPrice;
|
2026-05-02 17:10:00 +02:00
|
|
|
use App\Models\Tracker;
|
2026-05-02 09:52:42 +02:00
|
|
|
use App\Models\User;
|
2025-07-10 17:37:30 +02:00
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
|
|
class PricingController extends Controller
|
|
|
|
|
{
|
2026-05-02 17:10:00 +02:00
|
|
|
private ?Tracker $tracker;
|
2026-05-02 09:52:42 +02:00
|
|
|
|
2026-05-02 15:07:24 +02:00
|
|
|
public function __construct()
|
|
|
|
|
{
|
2026-05-02 17:10:00 +02:00
|
|
|
$this->tracker = User::default()->tracker;
|
2026-05-02 15:07:24 +02:00
|
|
|
}
|
2025-07-10 17:37:30 +02:00
|
|
|
|
2026-05-02 15:07:24 +02:00
|
|
|
public function current(): JsonResponse
|
|
|
|
|
{
|
2025-07-10 17:37:30 +02:00
|
|
|
return response()->json([
|
2026-05-02 17:10:00 +02:00
|
|
|
'current_price' => AssetPrice::current($this->tracker?->asset_id),
|
2025-07-10 17:37:30 +02:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-13 01:07:16 +02:00
|
|
|
public function update(Request $request)
|
2025-07-10 17:37:30 +02:00
|
|
|
{
|
|
|
|
|
$validated = $request->validate([
|
|
|
|
|
'date' => 'required|date|before_or_equal:today',
|
|
|
|
|
'price' => 'required|numeric|min:0.0001',
|
|
|
|
|
]);
|
|
|
|
|
|
2026-05-02 17:10:00 +02:00
|
|
|
if (! $this->tracker?->asset_id) {
|
2025-08-01 00:36:05 +02:00
|
|
|
return back()->withErrors(['asset' => 'Please set an asset first.']);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-02 17:10:00 +02:00
|
|
|
AssetPrice::updatePrice($this->tracker->asset_id, $validated['date'], $validated['price']);
|
2026-05-01 22:02:13 +02:00
|
|
|
|
2026-05-02 17:10:00 +02:00
|
|
|
if (! $this->tracker->price_tracking_enabled) {
|
|
|
|
|
$this->tracker->update(['price_tracking_enabled' => true]);
|
2026-05-01 22:02:13 +02:00
|
|
|
}
|
2025-07-10 17:37:30 +02:00
|
|
|
|
2025-07-13 01:07:16 +02:00
|
|
|
return back()->with('success', 'Asset price updated successfully!');
|
2025-07-10 17:37:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function history(Request $request): JsonResponse
|
|
|
|
|
{
|
2026-05-02 16:14:31 +02:00
|
|
|
$limit = min(max(1, $request->integer('limit', 30)), 365);
|
2025-07-10 17:37:30 +02:00
|
|
|
|
2026-05-02 17:10:00 +02:00
|
|
|
return response()->json(AssetPrice::history($this->tracker?->asset_id, $limit));
|
2025-07-10 17:37:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function forDate(Request $request, string $date): JsonResponse
|
|
|
|
|
{
|
2026-05-02 16:14:31 +02:00
|
|
|
validator(['date' => $date], ['date' => 'required|date_format:Y-m-d'])->validate();
|
|
|
|
|
|
2025-07-10 17:37:30 +02:00
|
|
|
return response()->json([
|
|
|
|
|
'date' => $date,
|
2026-05-02 17:10:00 +02:00
|
|
|
'price' => AssetPrice::forDate($date, $this->tracker?->asset_id),
|
2025-07-10 17:37:30 +02:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|