2025-07-10 17:37:30 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Pricing;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use App\Models\Pricing\AssetPrice;
|
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 15:07:24 +02:00
|
|
|
private User $user;
|
2026-05-02 09:52:42 +02:00
|
|
|
|
2026-05-02 15:07:24 +02:00
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->user = User::default();
|
|
|
|
|
}
|
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 15:07:24 +02:00
|
|
|
'current_price' => AssetPrice::current($this->user->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 15:07:24 +02:00
|
|
|
if (! $this->user->asset_id) {
|
2025-08-01 00:36:05 +02:00
|
|
|
return back()->withErrors(['asset' => 'Please set an asset first.']);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-02 15:07:24 +02:00
|
|
|
AssetPrice::updatePrice($this->user->asset_id, $validated['date'], $validated['price']);
|
2026-05-01 22:02:13 +02:00
|
|
|
|
2026-05-02 15:07:24 +02:00
|
|
|
if (! $this->user->price_tracking_enabled) {
|
|
|
|
|
$this->user->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
|
|
|
|
|
{
|
|
|
|
|
$limit = $request->get('limit', 30);
|
|
|
|
|
|
2026-05-02 15:07:24 +02:00
|
|
|
return response()->json(AssetPrice::history($this->user->asset_id, $limit));
|
2025-07-10 17:37:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function forDate(Request $request, string $date): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
return response()->json([
|
|
|
|
|
'date' => $date,
|
2026-05-02 15:07:24 +02:00
|
|
|
'price' => AssetPrice::forDate($date, $this->user->asset_id),
|
2025-07-10 17:37:30 +02:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|