Compare commits

...

9 Commits

Author SHA1 Message Date
180157ea91 Merge branch 'develop' into master 2021-04-13 23:18:58 -04:00
78b58d4900 Cors 2021-04-13 23:18:34 -04:00
b8524880a5 Merge branch 'develop' into master 2021-04-13 23:08:39 -04:00
b86e69b60e Alias UI 2021-04-13 23:07:38 -04:00
446834c100 Alias WebSocket 2021-04-13 23:07:38 -04:00
3b20fbd66f Alias API 2021-04-13 23:07:37 -04:00
f9a00578f4 Migration foreign keys 2021-04-13 23:07:37 -04:00
3651969c33 Alias models 2021-04-13 23:07:37 -04:00
76205250ad Name cron 2021-04-13 23:07:37 -04:00
17 changed files with 529 additions and 12 deletions

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

View File

@ -72,6 +72,49 @@ class Currencies {
}
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 {
$currency = $factory->find(Currency::class)->one($currency_id);
$output = [
@ -173,4 +216,65 @@ class Currencies {
}
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);
}
}

View File

@ -15,7 +15,7 @@ class Cors {
$response = $handler->handle($request);
$response = $response->withHeader('Access-Control-Allow-Origin', 'http://localhost:8080,http://localhost:8081');
$response = $response->withHeader('Access-Control-Allow-Origin', '*');
$response = $response->withHeader('Access-Control-Allow-Methods', implode(',', $methods));
$response = $response->withHeader('Access-Control-Allow-Headers', $requestHeaders);
//$response = $response->withHeader('Access-Control-Allow-Credentials', 'true');

View File

@ -10,7 +10,7 @@ server {
try_files $uri /index.php$is_args$args;
}
add_header 'Access-Control-Allow-Origin' 'http://localhost:8080';
add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'Access-Control-Allow-Methods' 'GET,POST,PUT,DELETE,OPTIONS';
add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept";
add_header 'Content-Type' 'application/json';

View File

@ -21,11 +21,11 @@ return
],
'development' => [
'adapter' => 'mysql',
'host' => $_ENV['DB_HOST'],
'name' => $_ENV['DB_NAME'],
'user' => $_ENV['DB_USER'],
'pass' => $_ENV['DB_PASSWORD'],
'port' => '3306',
'host' => $_ENV['DB_HOST'] ?? 'localhost',
'name' => $_ENV['DB_NAME'] ?? 'money_dev',
'user' => $_ENV['DB_USER'] ?? 'money',
'pass' => $_ENV['DB_PASSWORD'] ?? 'money_pass',
'port' => '3307',
'charset' => 'utf8',
],
'testing' => [

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

View File

@ -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->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->get('/latest[/]', [Currencies::class, 'latestValue']);
$app->post('/add[/]', [Currencies::class, 'addValues']);
@ -33,3 +40,18 @@ $app->group('/currency/{currency_id}', function($app) {
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;
});
});

View File

@ -34,13 +34,21 @@ class CreateValues extends Phinx\Migration\AbstractMigration
'signed' => false,
'after' => 'value',
])
->addIndex(['currency_id'], [
/*->addIndex(['currency_id'], [
'name' => 'currency_id',
'unique' => false,
])
->addIndex(['base_id'], [
'name' => 'base_id',
'unique' => false,
])*/
->addForeignKey(['currency_id'], 'currencies', ['id'], [
'delete' => 'CASCADE',
'update' => 'CASCADE'
])
->addForeignKey(['base_id'], 'currencies', ['id'], [
'delete' => 'CASCADE',
'update' => 'CASCADE'
])
->create();
/*$this->table('currencies', [

View File

@ -53,9 +53,13 @@ final class CreateSources extends AbstractMigration
'encoding' => 'utf8mb4',
'after' => 'url',
])
->addIndex(['currency_id'], [
/*->addIndex(['currency_id'], [
'name' => 'currency_id',
'unique' => false,
])*/
->addForeignKey(['currency_id'], 'currencies', ['id'], [
'delete' => 'CASCADE',
'update' => 'CASCADE'
])
->create();
}

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

View File

@ -21,6 +21,7 @@ services:
ports:
- 9123:9000
app-cron:
container_name: money_cron
image: sleeck/crond
volumes:
- ./automation/crontab:/etc/cron.d/auto-crontab

56
src/Alias.php Normal file
View 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;
}
}

View File

@ -38,6 +38,13 @@ class Currency extends Model {
}
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'];
public static function add(ModelFactory $factory, $info) {
@ -73,6 +80,12 @@ class Currency extends Model {
}
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) {
$arr = (array) $info;
$arr['currency_id'] = (int) $this->id;
@ -85,4 +98,27 @@ class Currency extends Model {
$result = Source::add($this->factory, $arr);
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;
}
}

View File

@ -6,6 +6,15 @@
@section('content')
<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">
<thead>
<th>Url</th>
@ -87,10 +96,112 @@
</form>
</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
@push('scripts')
<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 = {
id: '#sources',
add_button: '#add_source',
@ -302,6 +413,8 @@
},
loading: '#loading',
setup: function() {
aliases.loading = this.loading
aliases.setup()
sources.loading = this.loading
sources.setup()
values.loading = this.loading
@ -317,6 +430,9 @@
if (response.request.action == 'currency') {
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') {
sources.get(response.request.body.currency_id, response.body)
}
@ -331,7 +447,7 @@
$.each(this.map, (i, el) => {
$(el).html(data.currency[i])
})
socket.sendMessage('currency.sources', {currency_id: '{{$currency_id}}'})
socket.sendMessage('currency.aliases', {currency_id: '{{$currency_id}}'})
}
}
$(document).ready(() => {

View File

@ -32,7 +32,7 @@ return [
return (object) $arr;
}),
'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['base'],
'assets'

View File

@ -37,6 +37,24 @@ class Currencies {
$response->getBody()->write($output);
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 {
$currency_id = $request->getBody()->read()['currency_id'];
$currency = $factory->find(Currency::class)->one($currency_id);

View File

@ -4,6 +4,7 @@ use ProVM\Money\Common\Listener\Currencies;
$controller = new Currencies();
$app->add('currencies', $controller);
$app->add('currency', [$controller, 'get']);
$app->add('currency.aliases', [$controller, 'getAliases']);
$app->add('currency.values.latest', [$controller, 'latest']);
$app->add('currency.sources', [$controller, 'getSources']);
$app->add('currency.values', [$controller, 'getValues']);