63 lines
1.7 KiB
PHP
63 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Pricing;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Pricing\AssetPrice;
|
|
use App\Models\User;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class PricingController extends Controller
|
|
{
|
|
private User $user;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->user = User::default();
|
|
}
|
|
|
|
public function current(): JsonResponse
|
|
{
|
|
return response()->json([
|
|
'current_price' => AssetPrice::current($this->user->asset_id),
|
|
]);
|
|
}
|
|
|
|
public function update(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'date' => 'required|date|before_or_equal:today',
|
|
'price' => 'required|numeric|min:0.0001',
|
|
]);
|
|
|
|
if (! $this->user->asset_id) {
|
|
return back()->withErrors(['asset' => 'Please set an asset first.']);
|
|
}
|
|
|
|
AssetPrice::updatePrice($this->user->asset_id, $validated['date'], $validated['price']);
|
|
|
|
if (! $this->user->price_tracking_enabled) {
|
|
$this->user->update(['price_tracking_enabled' => true]);
|
|
}
|
|
|
|
return back()->with('success', 'Asset price updated successfully!');
|
|
}
|
|
|
|
public function history(Request $request): JsonResponse
|
|
{
|
|
$limit = min(max(1, $request->integer('limit', 30)), 365);
|
|
|
|
return response()->json(AssetPrice::history($this->user->asset_id, $limit));
|
|
}
|
|
|
|
public function forDate(Request $request, string $date): JsonResponse
|
|
{
|
|
validator(['date' => $date], ['date' => 'required|date_format:Y-m-d'])->validate();
|
|
|
|
return response()->json([
|
|
'date' => $date,
|
|
'price' => AssetPrice::forDate($date, $this->user->asset_id),
|
|
]);
|
|
}
|
|
}
|