Backend coins and update

This commit is contained in:
2021-07-14 10:08:48 -04:00
parent c930ffc55e
commit 8e1e644904
7 changed files with 292 additions and 41 deletions

View File

@ -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);
}
}