2025-07-10 17:37:30 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Pricing;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use App\Models\Pricing\AssetPrice;
|
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
|
|
class PricingController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function current(): JsonResponse
|
|
|
|
|
{
|
2025-08-01 00:36:05 +02:00
|
|
|
$user = auth()->user();
|
|
|
|
|
$assetId = $user->asset_id;
|
|
|
|
|
|
|
|
|
|
$price = AssetPrice::current($assetId);
|
2025-07-10 17:37:30 +02:00
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'current_price' => $price,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
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',
|
|
|
|
|
]);
|
|
|
|
|
|
2025-08-01 00:36:05 +02:00
|
|
|
$user = auth()->user();
|
|
|
|
|
|
|
|
|
|
if (!$user->asset_id) {
|
|
|
|
|
return back()->withErrors(['asset' => 'Please set an asset first.']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$assetPrice = AssetPrice::updatePrice($user->asset_id, $validated['date'], $validated['price']);
|
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
|
|
|
|
|
{
|
2025-08-01 00:36:05 +02:00
|
|
|
$user = auth()->user();
|
|
|
|
|
$assetId = $user->asset_id;
|
|
|
|
|
|
2025-07-10 17:37:30 +02:00
|
|
|
$limit = $request->get('limit', 30);
|
2025-08-01 00:36:05 +02:00
|
|
|
$history = AssetPrice::history($assetId, $limit);
|
2025-07-10 17:37:30 +02:00
|
|
|
|
|
|
|
|
return response()->json($history);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function forDate(Request $request, string $date): JsonResponse
|
|
|
|
|
{
|
2025-08-01 00:36:05 +02:00
|
|
|
$user = auth()->user();
|
|
|
|
|
$assetId = $user->asset_id;
|
|
|
|
|
|
|
|
|
|
$price = AssetPrice::forDate($date, $assetId);
|
2025-07-10 17:37:30 +02:00
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'date' => $date,
|
|
|
|
|
'price' => $price,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|