Files
crypto/backend/api/common/Controller/Coins.php
2021-07-14 10:08:48 -04:00

171 lines
5.7 KiB
PHP

<?php
namespace ProVM\Crypto\Common\Controller;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use ProVM\Common\Define\Controller\JSON;
use ProVM\Common\Factory\Model as ModelFactory;
use ProVM\Crypto\Coin;
class Coins {
use JSON;
public function __invoke(Request $request, Response $response, ModelFactory $factory): Response {
$coins = $factory->find(Coin::class)->array();
usort($coins, function($a, $b) {
return strcmp($a['code'], $b['code']);
});
$output = compact('coins');
return $this->withJson($response, $output);
}
public function show(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]);
}
$output = ['coin' => $coin->toArray()];
return $this->withJson($response, $output);
}
public function add(Request $request, Response $response, ModelFactory $factory): Response {
$post = $request->getBody()->getContents();
$post = json_decode($post);
$coin = Coin::add($factory, $post);
$status = false;
if ($coin->isNew()) {
$status = $coin->save();
}
$output = [
'input' => $post,
'coin' => $coin->toArray(),
'created' => $status
];
return $this->withJson($response, $output);
}
public function edit(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]);
}
$post = json_decode($request->getBody()->getContents());
$edited = $coin->edit($post);
$output = ['input' => $post, 'coin' => $coin->toArray(), 'edited' => $edited];
return $this->withJson($response, $output);
}
public function delete(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, 'deleted' => false]);
}
$output = [
'coin' => $coin->toArray()
];
$status = $coin->delete();
$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);
}
}