v0.1.0
This commit is contained in:
66
backend/api/common/Controller/Coins.php
Normal file
66
backend/api/common/Controller/Coins.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user