56 lines
2.1 KiB
PHP
56 lines
2.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\Api;
|
||
|
|
|
||
|
|
use App\Models\Group;
|
||
|
|
use App\Models\Trading;
|
||
|
|
use App\Transformers\TradingTransformer;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
|
||
|
|
use function PHPUnit\Framework\isEmpty;
|
||
|
|
|
||
|
|
class TradingsController extends ApiController
|
||
|
|
{
|
||
|
|
public function index(Request $request)
|
||
|
|
{
|
||
|
|
$last_tradings = Group::where("type", "trading")->with("tradings")->latest()->first()->tradings;
|
||
|
|
$last_tradings = $last_tradings->unique('title');
|
||
|
|
|
||
|
|
$tradings = [];
|
||
|
|
foreach($last_tradings as $trading){
|
||
|
|
$title = trim(strtok($trading->title, '('));
|
||
|
|
$before_last_trading = Trading::where("title",'LIKE', "%{$title}%")->where('group_id', '!=', $trading->group_id)->latest()->first();
|
||
|
|
|
||
|
|
$difference = !$before_last_trading ? 0 : $this->getPercentageDifference($trading->price ?? 0, $before_last_trading->price ?? 0);
|
||
|
|
|
||
|
|
array_push($tradings, (object)[
|
||
|
|
'id' => $trading->id,
|
||
|
|
'title' => $title,
|
||
|
|
'unit' => $trading->unit,
|
||
|
|
'price' => $trading->price,
|
||
|
|
'old_price' => !$before_last_trading ? 0 : $before_last_trading->price,
|
||
|
|
'price_change' => number_format($difference, 1, ".", "")."%",
|
||
|
|
'amount' => $trading->amount,
|
||
|
|
'currency' => $trading->currency,
|
||
|
|
'seller_country' => $trading->seller_country,
|
||
|
|
'buyer_country' => $trading->buyer_country,
|
||
|
|
'point' => $trading->point,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
$tradings = Trading::hydrate($tradings);
|
||
|
|
|
||
|
|
return $this->respondWithCollection($tradings, new TradingTransformer);
|
||
|
|
dd($tradings);
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
function getPercentageDifference($new, $old){
|
||
|
|
// return (($new - $old) / $old) * 100;
|
||
|
|
// return (abs(($old - $new)) / ($old + $new) /2);
|
||
|
|
// return (abs($old - $new) / (($old + $new)/ 2)) * 100;
|
||
|
|
// return (abs($old - $new) / (($old + $new)/ 2)) * 100;
|
||
|
|
return (($new - $old) / abs($old)) * 100;
|
||
|
|
}
|
||
|
|
}
|