This commit is contained in:
2021-06-28 23:15:13 -04:00
parent 0061a3d920
commit f4a8db56ff
93 changed files with 2422 additions and 0 deletions

View File

@ -0,0 +1,46 @@
<?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;
class API {
use JSON;
public function __invoke(Request $request, Response $response): Response {
$output = [
'version' => '1.0.0',
'routes' => [
'/coins' => [
'/' => 'List all coins',
'/add' => 'Add coin'
],
'/coin/{coin_id}' => [
'/' => 'Show coin information',
'/edit' => 'Edit coin',
'/delete' => 'Delete coin'
],
'/locations' => [
'/' => 'List all locations',
'/add' => 'Add location'
],
'/location/{location_id}' => [
'/' => 'Show location information',
'/edit' => 'Edit location',
'/delete' => 'Delete location'
],
'/wallets' => [
'/' => 'List all wallets',
'/add' => 'Add wallet'
],
'/wallet/{wallet_id}' => [
'/' => 'Show wallet information',
'/edit' => 'Edit wallet',
'/delete' => 'Delete wallet'
]
]
];
return $this->withJson($response, $output);
}
}

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

View File

@ -0,0 +1,70 @@
<?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\Location;
class Locations {
use JSON;
public function __invoke(Request $request, Response $response, ModelFactory $factory): Response {
$locations = $factory->find(Location::class)->array();
$output = compact('locations');
return $this->withJson($response, $output);
}
public function show(Request $request, Response $response, ModelFactory $factory, $location_id): Response {
$location = $factory->find(Location::class)->one($location_id);
if (!$location) {
return $this->withJson($response, ['location' => null]);
}
$output = ['location' => $location->asArray()];
return $this->withJson($response, $output);
}
public function add(Request $request, Response $response, ModelFactory $factory): Response {
$post = json_decode($request->getBody()->getContents());
$fields = [
'name' => 'name',
'description' => 'description'
];
$data = array_combine($fields, array_merge(array_intersect_key((array) $post, $fields), array_fill_keys(array_keys(array_diff_key($fields, (array) $post)), null)));
$location = $factory->find(Location::class)->where([
['name', $data['name']]
])->one();
$status = true;
if (!$location) {
$location = $factory->create(Location::class, $data);
$status = $location->save();
}
$output = [
'information_provided' => $post,
'used_data' => $data,
'location' => $location->asArray(),
'saved' => $status
];
return $this->withJson($response, $output);
}
public function edit(Request $request, Response $response, ModelFactory $factory, $location_id): Response {
$location = $factory->find(Location::class)->one($location_id);
if (!$location) {
return $this->withJson($response, ['location' => null]);
}
$post = json_decode($request->getBody()->getContents());
$output = compact('location');
return $this->withJson($response, $output);
}
public function delete(Request $request, Response $response, ModelFactory $factory, $location_id): Response {
$location = $factory->find(Location::class)->one($location_id);
if (!$location) {
return $this->withJson($response, ['location' => null, 'deleted' => false]);
}
$output = [
'location' => $location->asArray()
];
$status = $location->delete();
$output['deleted'] = $status;
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,27 @@
<?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\Crypto\Common\Service\Update as Updater;
class Update {
use JSON;
public function __invoke(Request $request, Response $response, Updater $updater) {
$result = $updater->run();
$output = [
'result' => $result
];
return $this->withJson($response, $output);
}
public function register(Request $request, Response $response, Updater $updater, $coin_id, $type) {
$result = $updater->register($coin_id, $type);
$output = [
'input' => ['coin_id' => $coin_id, 'type' => $type],
'result' => $result
];
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,71 @@
<?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\Wallet;
class Wallets {
use JSON;
public function __invoke(Request $request, Response $response, ModelFactory $factory): Response {
$wallets = $factory->find(Wallet::class)->array();
$output = compact('wallets');
return $this->withJson($response, $output);
}
public function show(Request $request, Response $response, ModelFactory $factory, $wallet_id): Response {
$wallet = $factory->find(Wallet::class)->one($wallet_id);
if (!$wallet) {
return $this->withJson($response, ['wallet' => null]);
}
$output = ['wallet' => $wallet->asArray()];
return $this->withJson($response, $output);
}
public function add(Request $request, Response $response, ModelFactory $factory): Response {
$post = json_decode($request->getBody()->getContents());
$fields = [
'name' => 'name',
'location' => 'location_id',
'address' => 'public_address'
];
$data = array_combine($fields, array_merge(array_intersect_key((array) $post, $fields), array_fill_keys(array_keys(array_diff_key($fields, (array) $post)), null)));
$wallet = $factory->find(Wallet::class)->where([
['name', $data['name']]
])->one();
$status = true;
if (!$wallet) {
$wallet = $factory->create(Wallet::class, $data);
$status = $wallet->save();
}
$output = [
'information_provided' => $post,
'used_data' => $data,
'wallet' => $wallet->asArray(),
'saved' => $status
];
return $this->withJson($response, $output);
}
public function edit(Request $request, Response $response, ModelFactory $factory, $wallet_id): Response {
$wallet = $factory->find(Wallet::class)->one($wallet_id);
if (!$wallet) {
return $this->withJson($response, ['wallet' => null]);
}
$post = json_decode($request->getBody()->getContents());
$output = compact('wallet');
return $this->withJson($response, $output);
}
public function delete(Request $request, Response $response, ModelFactory $factory, $wallet_id): Response {
$wallet = $factory->find(Wallet::class)->one($wallet_id);
if (!$wallet) {
return $this->withJson($response, ['wallet' => null, 'deleted' => false]);
}
$output = [
'wallet' => $wallet->asArray()
];
$status = $wallet->delete();
$output['deleted'] = $status;
return $this->withJson($response, $output);
}
}