Backend coins and update
This commit is contained in:
@ -63,4 +63,108 @@ class Coins {
|
||||
$output['deleted'] = $status;
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function values(Request $request, Response $response, ModelFactory $factory, $coin_id): Response {
|
||||
$coin = $factory->find(Coin::class)->one($coin_id);
|
||||
if (!$coin) {
|
||||
return $this->withJson($response, ['coin' => null, 'values' => []]);
|
||||
}
|
||||
$values = $coin->values();
|
||||
if ($values === null) {
|
||||
return $this->withJson($response, [
|
||||
'coin' => $coin->toArray(),
|
||||
'values' => []
|
||||
]);
|
||||
}
|
||||
usort($values, function($a, $b) {
|
||||
return $a->dateTime()->timestamp - $b->dateTime()->timestamp;
|
||||
});
|
||||
$values = array_map(function($item) {
|
||||
$arr = $item->toArray();
|
||||
$arr['formatted'] = $item->unit()->format($item->value);
|
||||
return $arr;
|
||||
}, $values);
|
||||
$output = [
|
||||
'coin' => $coin->toArray(),
|
||||
'values' => $values
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function valuesMonth(Request $request, Response $response, ModelFactory $factory, $coin_id): Response {
|
||||
$coin = $factory->find(Coin::class)->one($coin_id);
|
||||
if (!$coin) {
|
||||
return $this->withJson($response, ['coin' => null, 'values' => []]);
|
||||
}
|
||||
$values = $coin->values('month');
|
||||
if ($values === null) {
|
||||
return $this->withJson($response, [
|
||||
'coin' => $coin->toArray(),
|
||||
'values' => []
|
||||
]);
|
||||
}
|
||||
usort($values, function($a, $b) {
|
||||
return $a->dateTime()->timestamp - $b->dateTime()->timestamp;
|
||||
});
|
||||
$values = array_map(function($item) {
|
||||
$arr = $item->toArray();
|
||||
$arr['formatted'] = $item->unit()->format($item->value);
|
||||
return $arr;
|
||||
}, $values);
|
||||
$output = [
|
||||
'coin' => $coin->toArray(),
|
||||
'values' => $values
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function valuesSixMonths(Request $request, Response $response, ModelFactory $factory, $coin_id): Response {
|
||||
$coin = $factory->find(Coin::class)->one($coin_id);
|
||||
if (!$coin) {
|
||||
return $this->withJson($response, ['coin' => null, 'values' => []]);
|
||||
}
|
||||
$values = $coin->values('months');
|
||||
if ($values === null) {
|
||||
return $this->withJson($response, [
|
||||
'coin' => $coin->toArray(),
|
||||
'values' => []
|
||||
]);
|
||||
}
|
||||
usort($values, function($a, $b) {
|
||||
return $a->dateTime()->timestamp - $b->dateTime()->timestamp;
|
||||
});
|
||||
$values = array_map(function($item) {
|
||||
$arr = $item->toArray();
|
||||
$arr['formatted'] = $item->unit()->format($item->value);
|
||||
return $arr;
|
||||
}, $values);
|
||||
$output = [
|
||||
'coin' => $coin->toArray(),
|
||||
'values' => $values
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function valuesYear(Request $request, Response $response, ModelFactory $factory, $coin_id): Response {
|
||||
$coin = $factory->find(Coin::class)->one($coin_id);
|
||||
if (!$coin) {
|
||||
return $this->withJson($response, ['coin' => null, 'values' => []]);
|
||||
}
|
||||
$values = $coin->values('year');
|
||||
if ($values === null) {
|
||||
return $this->withJson($response, [
|
||||
'coin' => $coin->toArray(),
|
||||
'values' => []
|
||||
]);
|
||||
}
|
||||
usort($values, function($a, $b) {
|
||||
return $a->dateTime()->timestamp - $b->dateTime()->timestamp;
|
||||
});
|
||||
$values = array_map(function($item) {
|
||||
$arr = $item->toArray();
|
||||
$arr['formatted'] = $item->unit()->format($item->value);
|
||||
return $arr;
|
||||
}, $values);
|
||||
$output = [
|
||||
'coin' => $coin->toArray(),
|
||||
'values' => $values
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user