Compare commits
7 Commits
9fcd8c367c
...
b8524880a5
Author | SHA1 | Date | |
---|---|---|---|
b8524880a5 | |||
b86e69b60e | |||
446834c100 | |||
3b20fbd66f | |||
f9a00578f4 | |||
3651969c33 | |||
76205250ad |
73
app/common/Controller/Aliases.php
Normal file
73
app/common/Controller/Aliases.php
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
<?php
|
||||||
|
namespace ProVM\Money\Common\Controller;
|
||||||
|
|
||||||
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||||
|
use Psr\Http\Message\ResponseInterface as Response;
|
||||||
|
use ProVM\Common\Factory\Model as ModelFactory;
|
||||||
|
use ProVM\Common\Define\Controller\Json;
|
||||||
|
use ProVM\Money\Alias;
|
||||||
|
|
||||||
|
class Aliases {
|
||||||
|
use Json;
|
||||||
|
|
||||||
|
public function __invoke(Request $request, Response $response, ModelFactory $factory): Response {
|
||||||
|
$aliases = $factory->find(Alias::class)->array();
|
||||||
|
$output = compact('aliases');
|
||||||
|
return $this->withJson($response, $output);
|
||||||
|
}
|
||||||
|
public function get(Request $request, Response $response, ModelFactory $factory, $alias_id): Response {
|
||||||
|
$alias = $factory->find(Alias::class)->one($alias_id);
|
||||||
|
$output = [
|
||||||
|
'get_data' => compact('alias_id'),
|
||||||
|
'alias' => null
|
||||||
|
];
|
||||||
|
if ($alias) {
|
||||||
|
$output['alias'] = $alias->asArray();
|
||||||
|
}
|
||||||
|
return $this->withJson($response, $output);
|
||||||
|
}
|
||||||
|
public function add(Request $request, Response $response, ModelFactory $factory): Response {
|
||||||
|
$post = json_decode($request->getBody()->getContents());
|
||||||
|
$aliases = [];
|
||||||
|
if (is_array($post)) {
|
||||||
|
foreach ($post as $obj) {
|
||||||
|
if (!is_object($obj)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$aliases []= Alias::add($factory, $obj);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$aliases []= Alias::add($factory, $post);
|
||||||
|
}
|
||||||
|
$output = [
|
||||||
|
'post_data' => $post,
|
||||||
|
'aliases' => $aliases
|
||||||
|
];
|
||||||
|
return $this->withJson($response, $output);
|
||||||
|
}
|
||||||
|
public function edit(Request $request, Response $response, ModelFactory $factory, $alias_id) {
|
||||||
|
$post = json_decode($request->getBody()->getContents());
|
||||||
|
$output = [
|
||||||
|
'get_data' => compact('alias_id'),
|
||||||
|
'post_data' => $post
|
||||||
|
];
|
||||||
|
$alias = $factory->find(Alias::class)->one($alias_id);
|
||||||
|
$edited = false;
|
||||||
|
if ($alias) {
|
||||||
|
$edited = $alias->edit($post);
|
||||||
|
$output['alias'] = $alias->asArray();
|
||||||
|
$output['edited'] = $edited;
|
||||||
|
}
|
||||||
|
return $this->withJson($response, $output);
|
||||||
|
}
|
||||||
|
public function delete(Request $request, Response $response, ModelFactory $factory, $alias_id): Response {
|
||||||
|
$alias = $factory->find(Alias::class)->one($alias_id);
|
||||||
|
$output = ['get_data' => compact('alias_id'), 'alias' => null, 'deleted' => false];
|
||||||
|
if ($alias) {
|
||||||
|
$output['alias'] = $alias->asArray();
|
||||||
|
$status = $alias->delete();
|
||||||
|
$output['deleted'] = $status;
|
||||||
|
}
|
||||||
|
return $this->withJson($response, $output);
|
||||||
|
}
|
||||||
|
}
|
@ -72,6 +72,49 @@ class Currencies {
|
|||||||
}
|
}
|
||||||
return $this->withJson($response, $output);
|
return $this->withJson($response, $output);
|
||||||
}
|
}
|
||||||
|
public function getAliases(Request $request, Response $response, ModelFactory $factory, $currency_id): Response {
|
||||||
|
$currency = $factory->find(Currency::class)->one($currency_id);
|
||||||
|
$output = [
|
||||||
|
'get_data' => compact('currency_id'),
|
||||||
|
'currency' => null,
|
||||||
|
'aliases' => []
|
||||||
|
];
|
||||||
|
if ($currency) {
|
||||||
|
$output['currency'] = $currency->asArray();
|
||||||
|
if ($currency->aliases()) {
|
||||||
|
$output['aliases'] = array_map(function($item) {
|
||||||
|
return $item->asArray();
|
||||||
|
}, $currency->aliases());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $this->withJson($response, $output);
|
||||||
|
}
|
||||||
|
public function addAliases(Request $request, Response $response, ModelFactory $factory, $currency_id): Response {
|
||||||
|
$currency = $factory->find(Currency::class)->one($currency_id);
|
||||||
|
$post = json_decode($request->getBody()->getContents());
|
||||||
|
$output = [
|
||||||
|
'get_data' => compact('currency_id'),
|
||||||
|
'post_data' => $post,
|
||||||
|
'currency' => null,
|
||||||
|
'aliases' => []
|
||||||
|
];
|
||||||
|
if ($currency) {
|
||||||
|
$output['currency'] = $currency->asArray();
|
||||||
|
$aliases = [];
|
||||||
|
if (is_array($post)) {
|
||||||
|
foreach ($post as $obj) {
|
||||||
|
if (!is_object($obj)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$aliases []= $currency->addAlias($obj);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$aliases []= $currency->addAlias($post);
|
||||||
|
}
|
||||||
|
$output['aliases'] = $aliases;
|
||||||
|
}
|
||||||
|
return $this->withJson($response, $output);
|
||||||
|
}
|
||||||
public function getValues(Request $request, Response $response, ModelFactory $factory, $currency_id): Response {
|
public function getValues(Request $request, Response $response, ModelFactory $factory, $currency_id): Response {
|
||||||
$currency = $factory->find(Currency::class)->one($currency_id);
|
$currency = $factory->find(Currency::class)->one($currency_id);
|
||||||
$output = [
|
$output = [
|
||||||
@ -173,4 +216,65 @@ class Currencies {
|
|||||||
}
|
}
|
||||||
return $this->withJson($response, $output);
|
return $this->withJson($response, $output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function find(Request $request, Response $response, ModelFactory $factory, $query): Response {
|
||||||
|
$currency = Currency::find($factory, $query);
|
||||||
|
$output = [
|
||||||
|
'get_data' => compact('query'),
|
||||||
|
'currency' => null
|
||||||
|
];
|
||||||
|
if ($currency) {
|
||||||
|
$output['currency'] = $currency->asArray();
|
||||||
|
}
|
||||||
|
return $this->withJson($response, $output);
|
||||||
|
}
|
||||||
|
public function findValues(Request $request, Response $response, ModelFactory $factory, $query): Response {
|
||||||
|
$currency = Currency::find($factory, $query);
|
||||||
|
$output = [
|
||||||
|
'get_data' => compact('query'),
|
||||||
|
'currency' => null,
|
||||||
|
'values' => []
|
||||||
|
];
|
||||||
|
if ($currency) {
|
||||||
|
$output['currency'] = $currency->asArray();
|
||||||
|
if ($currency->values()) {
|
||||||
|
$output['values'] = array_map(function($item) {
|
||||||
|
return $item->asArray();
|
||||||
|
}, $currency->values());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $this->withJson($response, $output);
|
||||||
|
}
|
||||||
|
public function findLatest(Request $request, Response $response, ModelFactory $factory, $query): Response {
|
||||||
|
$currency = Currency::find($factory, $query);
|
||||||
|
$output = [
|
||||||
|
'get_data' => compact('currency_id'),
|
||||||
|
'currency' => null,
|
||||||
|
'value' => null
|
||||||
|
];
|
||||||
|
if ($currency) {
|
||||||
|
$output['currency'] = $currency->asArray();
|
||||||
|
if ($currency->latest()) {
|
||||||
|
$output['value'] = $currency->latest()->asArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $this->withJson($response, $output);
|
||||||
|
}
|
||||||
|
public function findSources(Request $request, Response $response, ModelFactory $factory, $query): Response {
|
||||||
|
$currency = Currency::find($factory, $query);
|
||||||
|
$output = [
|
||||||
|
'get_data' => compact('query'),
|
||||||
|
'currency' => null,
|
||||||
|
'sources' => []
|
||||||
|
];
|
||||||
|
if ($currency) {
|
||||||
|
$output['currency'] = $currency->asArray();
|
||||||
|
if ($currency->sources()) {
|
||||||
|
$output['sources'] = array_map(function($item) {
|
||||||
|
return $item->asArray();
|
||||||
|
}, $currency->sources());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $this->withJson($response, $output);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,11 +21,11 @@ return
|
|||||||
],
|
],
|
||||||
'development' => [
|
'development' => [
|
||||||
'adapter' => 'mysql',
|
'adapter' => 'mysql',
|
||||||
'host' => $_ENV['DB_HOST'],
|
'host' => $_ENV['DB_HOST'] ?? 'localhost',
|
||||||
'name' => $_ENV['DB_NAME'],
|
'name' => $_ENV['DB_NAME'] ?? 'money_dev',
|
||||||
'user' => $_ENV['DB_USER'],
|
'user' => $_ENV['DB_USER'] ?? 'money',
|
||||||
'pass' => $_ENV['DB_PASSWORD'],
|
'pass' => $_ENV['DB_PASSWORD'] ?? 'money_pass',
|
||||||
'port' => '3306',
|
'port' => '3307',
|
||||||
'charset' => 'utf8',
|
'charset' => 'utf8',
|
||||||
],
|
],
|
||||||
'testing' => [
|
'testing' => [
|
||||||
|
19
app/resources/routes/api/aliases.php
Normal file
19
app/resources/routes/api/aliases.php
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
use ProVM\Money\Common\Controller\Aliases;
|
||||||
|
|
||||||
|
$app->group('/aliases', function($app) {
|
||||||
|
$app->post('/add[/]', [Aliases::class, 'add']);
|
||||||
|
$app->get('[/]', Aliases::class);
|
||||||
|
$app->options('[/]', function (Request $request, Response $response): Response {
|
||||||
|
return $response;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
$app->group('/alias/{alias_id}', function($app) {
|
||||||
|
$app->put('/edit[/]', [Aliases::class, 'edit']);
|
||||||
|
$app->delete('/delete[/]', [Aliases::class, 'delete']);
|
||||||
|
$app->get('[/]', [Aliases::class, 'get']);
|
||||||
|
$app->options('[/]', function (Request $request, Response $response): Response {
|
||||||
|
return $response;
|
||||||
|
});
|
||||||
|
});
|
@ -10,9 +10,16 @@ $app->group('/currencies', function($app) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
$app->group('/currency/{currency_id}', function($app) {
|
$app->group('/currency/{currency_id:[0-9]+}', function($app) {
|
||||||
$app->put('/edit[/]', [Currencies::class, 'edit']);
|
$app->put('/edit[/]', [Currencies::class, 'edit']);
|
||||||
$app->delete('/delete[/]', [Currencies::class, 'delete']);
|
$app->delete('/delete[/]', [Currencies::class, 'delete']);
|
||||||
|
$app->group('/aliases', function($app) {
|
||||||
|
$app->post('/add[/]', [Currencies::class, 'addAliases']);
|
||||||
|
$app->get('[/]', [Currencies::class, 'getAliases']);
|
||||||
|
$app->options('[/]', function (Request $request, Response $response): Response {
|
||||||
|
return $response;
|
||||||
|
});
|
||||||
|
});
|
||||||
$app->group('/values', function($app) {
|
$app->group('/values', function($app) {
|
||||||
$app->get('/latest[/]', [Currencies::class, 'latestValue']);
|
$app->get('/latest[/]', [Currencies::class, 'latestValue']);
|
||||||
$app->post('/add[/]', [Currencies::class, 'addValues']);
|
$app->post('/add[/]', [Currencies::class, 'addValues']);
|
||||||
@ -33,3 +40,18 @@ $app->group('/currency/{currency_id}', function($app) {
|
|||||||
return $response;
|
return $response;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$app->group('/currency/{query:[a-z]+}', function($app) {
|
||||||
|
$app->group('/values', function($app) {
|
||||||
|
$app->get('/latest[/]', [Currencies::class, 'findLatest']);
|
||||||
|
$app->get('[/]', [Currencies::class, 'findValues']);
|
||||||
|
$app->options('[/]', function (Request $request, Response $response): Response {
|
||||||
|
return $response;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
$app->get('/sources', [Currencies::class, 'findSources']);
|
||||||
|
$app->get('[/]', [Currencies::class, 'find']);
|
||||||
|
$app->options('[/]', function (Request $request, Response $response): Response {
|
||||||
|
return $response;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
@ -34,13 +34,21 @@ class CreateValues extends Phinx\Migration\AbstractMigration
|
|||||||
'signed' => false,
|
'signed' => false,
|
||||||
'after' => 'value',
|
'after' => 'value',
|
||||||
])
|
])
|
||||||
->addIndex(['currency_id'], [
|
/*->addIndex(['currency_id'], [
|
||||||
'name' => 'currency_id',
|
'name' => 'currency_id',
|
||||||
'unique' => false,
|
'unique' => false,
|
||||||
])
|
])
|
||||||
->addIndex(['base_id'], [
|
->addIndex(['base_id'], [
|
||||||
'name' => 'base_id',
|
'name' => 'base_id',
|
||||||
'unique' => false,
|
'unique' => false,
|
||||||
|
])*/
|
||||||
|
->addForeignKey(['currency_id'], 'currencies', ['id'], [
|
||||||
|
'delete' => 'CASCADE',
|
||||||
|
'update' => 'CASCADE'
|
||||||
|
])
|
||||||
|
->addForeignKey(['base_id'], 'currencies', ['id'], [
|
||||||
|
'delete' => 'CASCADE',
|
||||||
|
'update' => 'CASCADE'
|
||||||
])
|
])
|
||||||
->create();
|
->create();
|
||||||
/*$this->table('currencies', [
|
/*$this->table('currencies', [
|
||||||
|
@ -53,9 +53,13 @@ final class CreateSources extends AbstractMigration
|
|||||||
'encoding' => 'utf8mb4',
|
'encoding' => 'utf8mb4',
|
||||||
'after' => 'url',
|
'after' => 'url',
|
||||||
])
|
])
|
||||||
->addIndex(['currency_id'], [
|
/*->addIndex(['currency_id'], [
|
||||||
'name' => 'currency_id',
|
'name' => 'currency_id',
|
||||||
'unique' => false,
|
'unique' => false,
|
||||||
|
])*/
|
||||||
|
->addForeignKey(['currency_id'], 'currencies', ['id'], [
|
||||||
|
'delete' => 'CASCADE',
|
||||||
|
'update' => 'CASCADE'
|
||||||
])
|
])
|
||||||
->create();
|
->create();
|
||||||
}
|
}
|
||||||
|
59
db/migrations/20210414015145_create_aliases.php
Normal file
59
db/migrations/20210414015145_create_aliases.php
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Phinx\Migration\AbstractMigration;
|
||||||
|
|
||||||
|
final class CreateAliases extends AbstractMigration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Change Method.
|
||||||
|
*
|
||||||
|
* Write your reversible migrations using this method.
|
||||||
|
*
|
||||||
|
* More information on writing migrations is available here:
|
||||||
|
* https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
|
||||||
|
*
|
||||||
|
* Remember to call "create()" or "update()" and NOT "save()" when working
|
||||||
|
* with the Table class.
|
||||||
|
*/
|
||||||
|
public function change(): void
|
||||||
|
{
|
||||||
|
$this->table('aliases', [
|
||||||
|
'id' => false,
|
||||||
|
'primary_key' => ['id'],
|
||||||
|
'engine' => 'InnoDB',
|
||||||
|
'encoding' => 'utf8mb4',
|
||||||
|
'collation' => 'utf8mb4_general_ci',
|
||||||
|
'comment' => '',
|
||||||
|
'row_format' => 'DYNAMIC',
|
||||||
|
])
|
||||||
|
->addColumn('id', 'integer', [
|
||||||
|
'null' => false,
|
||||||
|
'limit' => '10',
|
||||||
|
'signed' => false,
|
||||||
|
'identity' => 'enable',
|
||||||
|
])
|
||||||
|
->addColumn('currency_id', 'integer', [
|
||||||
|
'null' => false,
|
||||||
|
'limit' => '10',
|
||||||
|
'signed' => false,
|
||||||
|
'after' => 'id',
|
||||||
|
])
|
||||||
|
->addColumn('alias', 'string', [
|
||||||
|
'null' => false,
|
||||||
|
'limit' => 100,
|
||||||
|
'collation' => 'utf8mb4_general_ci',
|
||||||
|
'encoding' => 'utf8mb4',
|
||||||
|
'after' => 'currency_id',
|
||||||
|
])
|
||||||
|
/*->addIndex(['currency_id'], [
|
||||||
|
'name' => 'currency_id',
|
||||||
|
'unique' => false,
|
||||||
|
])*/
|
||||||
|
->addForeignKey(['currency_id'], 'currencies', ['id'], [
|
||||||
|
'delete' => 'CASCADE',
|
||||||
|
'update' => 'CASCADE'
|
||||||
|
])
|
||||||
|
->create();
|
||||||
|
}
|
||||||
|
}
|
@ -21,6 +21,7 @@ services:
|
|||||||
ports:
|
ports:
|
||||||
- 9123:9000
|
- 9123:9000
|
||||||
app-cron:
|
app-cron:
|
||||||
|
container_name: money_cron
|
||||||
image: sleeck/crond
|
image: sleeck/crond
|
||||||
volumes:
|
volumes:
|
||||||
- ./automation/crontab:/etc/cron.d/auto-crontab
|
- ./automation/crontab:/etc/cron.d/auto-crontab
|
||||||
|
56
src/Alias.php
Normal file
56
src/Alias.php
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
<?php
|
||||||
|
namespace ProVM\Money;
|
||||||
|
|
||||||
|
use ProVM\Common\Alias\Model;
|
||||||
|
use ProVM\Common\Factory\Model as ModelFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @property int $id
|
||||||
|
* @property Currency $currency_id
|
||||||
|
* @property string $alias
|
||||||
|
*/
|
||||||
|
class Alias extends Model {
|
||||||
|
public static $_table = 'aliases';
|
||||||
|
|
||||||
|
protected $currency;
|
||||||
|
public function currency() {
|
||||||
|
if ($this->currency === null) {
|
||||||
|
$this->currency = $this->childOf(Currency::class, [Model::SELF_KEY => 'currency_id']);
|
||||||
|
}
|
||||||
|
return $this->currency;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static $fields = ['currency_id', 'alias'];
|
||||||
|
public static function add(ModelFactory $factory, $info) {
|
||||||
|
$input = array_intersect_key((array) $info, array_combine(self::$fields, self::$fields));
|
||||||
|
$alias = $factory->find(Alias::class)->where([['currency_id', $input['currency_id']], ['alias', $input['alias']]])->one();
|
||||||
|
$created = false;
|
||||||
|
$result = (object) compact('input', 'alias', 'created');
|
||||||
|
if (!$alias) {
|
||||||
|
$alias = $factory->create(Alias::class, $input);
|
||||||
|
$created = $alias->save();
|
||||||
|
$result->created = $created;
|
||||||
|
}
|
||||||
|
$result->source = $alias->asArray();
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
public function edit($info): bool {
|
||||||
|
$data = array_intersect_key((array) $info, array_combine(self::$fields, self::$fields));
|
||||||
|
$edited = false;
|
||||||
|
foreach ($data as $field => $value) {
|
||||||
|
if ($this->{$field} != $value) {
|
||||||
|
$this->{$field} = $value;
|
||||||
|
$edited = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($edited) {
|
||||||
|
$edited = $this->save();
|
||||||
|
}
|
||||||
|
return $edited;
|
||||||
|
}
|
||||||
|
public function asArray(): array {
|
||||||
|
$output = parent::asArray();
|
||||||
|
$output['currency'] = $this->currency()->asArray();
|
||||||
|
return $output;
|
||||||
|
}
|
||||||
|
}
|
@ -38,6 +38,13 @@ class Currency extends Model {
|
|||||||
}
|
}
|
||||||
return $this->sources;
|
return $this->sources;
|
||||||
}
|
}
|
||||||
|
protected $aliases;
|
||||||
|
public function aliases() {
|
||||||
|
if ($this->aliases === null) {
|
||||||
|
$this->aliases = $this->parentOf(Alias::class, [Model::CHILD_KEY => 'currency_id']);
|
||||||
|
}
|
||||||
|
return $this->aliases;
|
||||||
|
}
|
||||||
|
|
||||||
protected static $fields = ['code', 'name'];
|
protected static $fields = ['code', 'name'];
|
||||||
public static function add(ModelFactory $factory, $info) {
|
public static function add(ModelFactory $factory, $info) {
|
||||||
@ -73,6 +80,12 @@ class Currency extends Model {
|
|||||||
}
|
}
|
||||||
return $edited;
|
return $edited;
|
||||||
}
|
}
|
||||||
|
public function addAlias($info) {
|
||||||
|
$arr = (array) $info;
|
||||||
|
$arr['currency_id'] = (int) $this->id;
|
||||||
|
$result = Alias::add($this->factory, $arr);
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
public function addValue($info) {
|
public function addValue($info) {
|
||||||
$arr = (array) $info;
|
$arr = (array) $info;
|
||||||
$arr['currency_id'] = (int) $this->id;
|
$arr['currency_id'] = (int) $this->id;
|
||||||
@ -85,4 +98,27 @@ class Currency extends Model {
|
|||||||
$result = Source::add($this->factory, $arr);
|
$result = Source::add($this->factory, $arr);
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function find(ModelFactory $factory, string $query) {
|
||||||
|
$query = '%' . $query . '%';
|
||||||
|
$currency = $factory->find(Currency::class)->where([
|
||||||
|
['code', $query, 'like']
|
||||||
|
])->one();
|
||||||
|
if ($currency !== false and $currency !== null) {
|
||||||
|
return $currency;
|
||||||
|
}
|
||||||
|
$currency = $factory->find(Currency::class)->where([
|
||||||
|
['name', $query, 'like']
|
||||||
|
])->one();
|
||||||
|
if ($currency !== false and $currency !== null) {
|
||||||
|
return $currency;
|
||||||
|
}
|
||||||
|
$alias = $factory->find(Alias::class)->where([
|
||||||
|
['alias', $query, 'like']
|
||||||
|
])->one();
|
||||||
|
if ($alias !== false and $alias !== null) {
|
||||||
|
return $alias->currency();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,15 @@
|
|||||||
|
|
||||||
@section('content')
|
@section('content')
|
||||||
<h3 class="ui header c_code"></h3>
|
<h3 class="ui header c_code"></h3>
|
||||||
|
<table class="ui table">
|
||||||
|
<thead>
|
||||||
|
<th>Alias</th>
|
||||||
|
<th class="right aligned" id="add_alias">
|
||||||
|
<i class="plus icon"></i>
|
||||||
|
</th>
|
||||||
|
</thead>
|
||||||
|
<tbody id="aliases"></tbody>
|
||||||
|
</table>
|
||||||
<table class="ui table">
|
<table class="ui table">
|
||||||
<thead>
|
<thead>
|
||||||
<th>Url</th>
|
<th>Url</th>
|
||||||
@ -87,10 +96,112 @@
|
|||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="ui modal" id="add_aliases">
|
||||||
|
<div class="header">
|
||||||
|
Agregar Alias para <span class="c_name"></span>
|
||||||
|
</div>
|
||||||
|
<div class="content">
|
||||||
|
<form class="ui form">
|
||||||
|
<div class="inline field">
|
||||||
|
<label>Alias</label>
|
||||||
|
<input type="text" name="alias" />
|
||||||
|
</div>
|
||||||
|
<button class="ui button">Agregar</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
@endsection
|
@endsection
|
||||||
|
|
||||||
@push('scripts')
|
@push('scripts')
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
|
let aliases = {
|
||||||
|
id: '#aliases',
|
||||||
|
add_button: '#add_alias',
|
||||||
|
add_modal: '#add_aliases',
|
||||||
|
loading: '',
|
||||||
|
loaded: false,
|
||||||
|
aliases: [],
|
||||||
|
setup: function() {
|
||||||
|
$(this.id).hide()
|
||||||
|
$(this.add_button).css('cursor', 'pointer').click((e) => {
|
||||||
|
this.add()
|
||||||
|
})
|
||||||
|
this.buildModal()
|
||||||
|
},
|
||||||
|
get: function(currency_id, data) {
|
||||||
|
if (!this.loaded) {
|
||||||
|
this.aliases = data.aliases
|
||||||
|
this.populate(currency_id)
|
||||||
|
socket.sendMessage('currency.sources', {currency_id: currency_id})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var url = '{{$urls->api}}/currency/' + currency_id + '/aliases'
|
||||||
|
$.getJSON(url, (data) => {
|
||||||
|
this.aliases = data.aliases
|
||||||
|
this.populate(currency_id)
|
||||||
|
})
|
||||||
|
},
|
||||||
|
buildModal: function() {
|
||||||
|
$(this.add_modal).modal()
|
||||||
|
$(this.add_modal).find('form').submit((e) => {
|
||||||
|
e.preventDefault()
|
||||||
|
this.doAdd()
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
},
|
||||||
|
populate: function(currency_id) {
|
||||||
|
$(this.id).html('')
|
||||||
|
$.each(this.aliases, (i, el) => {
|
||||||
|
let row = $('<tr></tr>').append(
|
||||||
|
$('<td></td>').html(el.alias)
|
||||||
|
).append(
|
||||||
|
$('<td></td>').attr('class', 'remove_source right aligned').attr('data-id', el.id).append(
|
||||||
|
$('<i></i>').attr('class', 'minus icon')
|
||||||
|
).css('cursor', 'pointer').click((e) => {
|
||||||
|
this.remove(currency_id, $(e.currentTarget).attr('data-id'))
|
||||||
|
})
|
||||||
|
)
|
||||||
|
$(this.id).append(row)
|
||||||
|
})
|
||||||
|
$(this.id).show()
|
||||||
|
},
|
||||||
|
add: function() {
|
||||||
|
$(this.add_modal).find('form').trigger('reset')
|
||||||
|
$(this.add_modal).modal('show')
|
||||||
|
},
|
||||||
|
doAdd: function() {
|
||||||
|
let form = $(this.add_modal).find('form')
|
||||||
|
let info = {
|
||||||
|
alias: form.find("[name='alias']").val()
|
||||||
|
}
|
||||||
|
var url = '{{$urls->api}}/currency/{{$currency_id}}/aliases/add'
|
||||||
|
$(this.add_modal).modal('hide')
|
||||||
|
$(this.loading).modal('show')
|
||||||
|
$.post(url, JSON.stringify(info), (data) => {
|
||||||
|
if (data.aliases[0].created) {
|
||||||
|
this.get('{{$currency_id}}')
|
||||||
|
}
|
||||||
|
}, 'json').then(() => {
|
||||||
|
$(this.loading).modal('hide')
|
||||||
|
})
|
||||||
|
},
|
||||||
|
remove: function(id) {
|
||||||
|
var url = '{{$urls->api}}/alias/' + id + '/delete'
|
||||||
|
$(this.loading).modal('show')
|
||||||
|
$.ajax({
|
||||||
|
url: url,
|
||||||
|
method: 'DELETE',
|
||||||
|
dataType: 'json',
|
||||||
|
success: (data) => {
|
||||||
|
if (data.deleted) {
|
||||||
|
this.get()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).then(() => {
|
||||||
|
$(this.loading).modal('hide')
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
let sources = {
|
let sources = {
|
||||||
id: '#sources',
|
id: '#sources',
|
||||||
add_button: '#add_source',
|
add_button: '#add_source',
|
||||||
@ -302,6 +413,8 @@
|
|||||||
},
|
},
|
||||||
loading: '#loading',
|
loading: '#loading',
|
||||||
setup: function() {
|
setup: function() {
|
||||||
|
aliases.loading = this.loading
|
||||||
|
aliases.setup()
|
||||||
sources.loading = this.loading
|
sources.loading = this.loading
|
||||||
sources.setup()
|
sources.setup()
|
||||||
values.loading = this.loading
|
values.loading = this.loading
|
||||||
@ -317,6 +430,9 @@
|
|||||||
if (response.request.action == 'currency') {
|
if (response.request.action == 'currency') {
|
||||||
currency.get(response.body)
|
currency.get(response.body)
|
||||||
}
|
}
|
||||||
|
if (response.request.action == 'currency.aliases') {
|
||||||
|
aliases.get(response.request.body.currency_id, response.body)
|
||||||
|
}
|
||||||
if (response.request.action == 'currency.sources') {
|
if (response.request.action == 'currency.sources') {
|
||||||
sources.get(response.request.body.currency_id, response.body)
|
sources.get(response.request.body.currency_id, response.body)
|
||||||
}
|
}
|
||||||
@ -331,7 +447,7 @@
|
|||||||
$.each(this.map, (i, el) => {
|
$.each(this.map, (i, el) => {
|
||||||
$(el).html(data.currency[i])
|
$(el).html(data.currency[i])
|
||||||
})
|
})
|
||||||
socket.sendMessage('currency.sources', {currency_id: '{{$currency_id}}'})
|
socket.sendMessage('currency.aliases', {currency_id: '{{$currency_id}}'})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$(document).ready(() => {
|
$(document).ready(() => {
|
||||||
|
@ -32,7 +32,7 @@ return [
|
|||||||
return (object) $arr;
|
return (object) $arr;
|
||||||
}),
|
}),
|
||||||
'urls' => function(Container $c) {
|
'urls' => function(Container $c) {
|
||||||
$arr = ['base' => ($c->has('base_url')) ? $c->get('base_url') : ''];
|
$arr = ['base' => ($c->has('base_url')) ? $c->get('base_url') : '/'];
|
||||||
$arr['assets'] = implode('/', [
|
$arr['assets'] = implode('/', [
|
||||||
$arr['base'],
|
$arr['base'],
|
||||||
'assets'
|
'assets'
|
||||||
|
@ -37,6 +37,24 @@ class Currencies {
|
|||||||
$response->getBody()->write($output);
|
$response->getBody()->write($output);
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
public function getAliases(Request $request, Response $response, ModelFactory $factory): Response {
|
||||||
|
$currency_id = $request->getBody()->read()['currency_id'];
|
||||||
|
$currency = $factory->find(Currency::class)->one($currency_id);
|
||||||
|
$output = [
|
||||||
|
'currency' => null,
|
||||||
|
'aliases' => []
|
||||||
|
];
|
||||||
|
if ($currency) {
|
||||||
|
$output['currency'] = $currency->asArray();
|
||||||
|
if ($currency->aliases()) {
|
||||||
|
$output['aliases'] = array_map(function($item) {
|
||||||
|
return $item->asArray();
|
||||||
|
}, $currency->aliases());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$response->getBody()->write($output);
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
public function getSources(Request $request, Response $response, ModelFactory $factory): Response {
|
public function getSources(Request $request, Response $response, ModelFactory $factory): Response {
|
||||||
$currency_id = $request->getBody()->read()['currency_id'];
|
$currency_id = $request->getBody()->read()['currency_id'];
|
||||||
$currency = $factory->find(Currency::class)->one($currency_id);
|
$currency = $factory->find(Currency::class)->one($currency_id);
|
||||||
|
@ -4,6 +4,7 @@ use ProVM\Money\Common\Listener\Currencies;
|
|||||||
$controller = new Currencies();
|
$controller = new Currencies();
|
||||||
$app->add('currencies', $controller);
|
$app->add('currencies', $controller);
|
||||||
$app->add('currency', [$controller, 'get']);
|
$app->add('currency', [$controller, 'get']);
|
||||||
|
$app->add('currency.aliases', [$controller, 'getAliases']);
|
||||||
$app->add('currency.values.latest', [$controller, 'latest']);
|
$app->add('currency.values.latest', [$controller, 'latest']);
|
||||||
$app->add('currency.sources', [$controller, 'getSources']);
|
$app->add('currency.sources', [$controller, 'getSources']);
|
||||||
$app->add('currency.values', [$controller, 'getValues']);
|
$app->add('currency.values', [$controller, 'getValues']);
|
||||||
|
Reference in New Issue
Block a user