add blockchain types table and connect it with transactions table

This commit is contained in:
Dario Rekowski on RockPI 2021-04-06 10:16:13 +00:00
parent 6ba6bd7180
commit 6ca4fa7931
12 changed files with 376 additions and 0 deletions

View File

@ -0,0 +1,17 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Author: einhornimmond
* Created: 06.04.2021
*/
CREATE TABLE `blockchain_types` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`text` varchar(255) NULL,
`symbol` varchar(10) NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

View File

@ -0,0 +1,5 @@
INSERT INTO `blockchain_types` (`id`, `name`, `text`, `symbol`) VALUES
(1, 'mysql', 'use mysql db as blockchain, work only with single community-server', NULL),
(2, 'hedera', 'use hedera for transactions', 'HBAR');

View File

@ -5,5 +5,6 @@ CREATE TABLE `transactions` (
`tx_hash` binary(48) DEFAULT NULL,
`memo` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`received` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`blockchain_type_id` bigint(20) unsigned NOT NULL DEFAULT 1,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

View File

@ -0,0 +1,106 @@
<?php
namespace App\Controller;
use App\Controller\AppController;
/**
* BlockchainTypes Controller
*
* @property \App\Model\Table\BlockchainTypesTable $BlockchainTypes
*
* @method \App\Model\Entity\BlockchainType[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
*/
class BlockchainTypesController extends AppController
{
/**
* Index method
*
* @return \Cake\Http\Response|null
*/
public function index()
{
$blockchainTypes = $this->paginate($this->BlockchainTypes);
$this->set(compact('blockchainTypes'));
}
/**
* View method
*
* @param string|null $id Blockchain Type id.
* @return \Cake\Http\Response|null
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
$blockchainType = $this->BlockchainTypes->get($id, [
'contain' => [],
]);
$this->set('blockchainType', $blockchainType);
}
/**
* Add method
*
* @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise.
*/
public function add()
{
$blockchainType = $this->BlockchainTypes->newEntity();
if ($this->request->is('post')) {
$blockchainType = $this->BlockchainTypes->patchEntity($blockchainType, $this->request->getData());
if ($this->BlockchainTypes->save($blockchainType)) {
$this->Flash->success(__('The blockchain type has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The blockchain type could not be saved. Please, try again.'));
}
$this->set(compact('blockchainType'));
}
/**
* Edit method
*
* @param string|null $id Blockchain Type id.
* @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function edit($id = null)
{
$blockchainType = $this->BlockchainTypes->get($id, [
'contain' => [],
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$blockchainType = $this->BlockchainTypes->patchEntity($blockchainType, $this->request->getData());
if ($this->BlockchainTypes->save($blockchainType)) {
$this->Flash->success(__('The blockchain type has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The blockchain type could not be saved. Please, try again.'));
}
$this->set(compact('blockchainType'));
}
/**
* Delete method
*
* @param string|null $id Blockchain Type id.
* @return \Cake\Http\Response|null Redirects to index.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$blockchainType = $this->BlockchainTypes->get($id);
if ($this->BlockchainTypes->delete($blockchainType)) {
$this->Flash->success(__('The blockchain type has been deleted.'));
} else {
$this->Flash->error(__('The blockchain type could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace App\Model\Entity;
use Cake\ORM\Entity;
/**
* BlockchainType Entity
*
* @property int $id
* @property string $name
* @property string|null $text
* @property string|null $symbol
*/
class BlockchainType extends Entity
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* Note that when '*' is set to true, this allows all unspecified fields to
* be mass assigned. For security purposes, it is advised to set '*' to false
* (or remove it), and explicitly make individual fields accessible as needed.
*
* @var array
*/
protected $_accessible = [
'name' => true,
'text' => true,
'symbol' => true,
];
}

View File

@ -39,6 +39,7 @@ class Transaction extends Entity
'tx_hash' => true,
'memo' => true,
'received' => true,
'blockchain_type_id' => true,
'state_group' => true,
'transaction_type' => true,
'state_created' => true,

View File

@ -0,0 +1,68 @@
<?php
namespace App\Model\Table;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
/**
* BlockchainTypes Model
*
* @method \App\Model\Entity\BlockchainType get($primaryKey, $options = [])
* @method \App\Model\Entity\BlockchainType newEntity($data = null, array $options = [])
* @method \App\Model\Entity\BlockchainType[] newEntities(array $data, array $options = [])
* @method \App\Model\Entity\BlockchainType|false save(\Cake\Datasource\EntityInterface $entity, $options = [])
* @method \App\Model\Entity\BlockchainType saveOrFail(\Cake\Datasource\EntityInterface $entity, $options = [])
* @method \App\Model\Entity\BlockchainType patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
* @method \App\Model\Entity\BlockchainType[] patchEntities($entities, array $data, array $options = [])
* @method \App\Model\Entity\BlockchainType findOrCreate($search, callable $callback = null, $options = [])
*/
class BlockchainTypesTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('blockchain_types');
$this->setDisplayField('name');
$this->setPrimaryKey('id');
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
$validator
->nonNegativeInteger('id')
->allowEmptyString('id', null, 'create');
$validator
->scalar('name')
->maxLength('name', 45)
->requirePresence('name', 'create')
->notEmptyString('name');
$validator
->scalar('text')
->maxLength('text', 255)
->allowEmptyString('text');
$validator
->scalar('symbol')
->maxLength('symbol', 10)
->allowEmptyString('symbol');
return $validator;
}
}

View File

@ -52,6 +52,10 @@ class TransactionsTable extends Table
'foreignKey' => 'transaction_type_id',
'joinType' => 'INNER'
]);
$this->belongsTo('BlockchainTypes', [
'foreignKey' => 'blockchain_type_id',
'joinType' => 'INNER'
]);
$this->hasMany('StateCreated', [
'foreignKey' => 'transaction_id'
]);
@ -114,6 +118,7 @@ class TransactionsTable extends Table
{
$rules->add($rules->existsIn(['state_group_id'], 'StateGroups'));
$rules->add($rules->existsIn(['transaction_type_id'], 'TransactionTypes'));
$rules->add($rules->existsIn(['blockchain_type_id'], 'BlockchainTypes'));
return $rules;
}

View File

@ -0,0 +1,25 @@
<?php
/**
* @var \App\View\AppView $this
* @var \App\Model\Entity\BlockchainType $blockchainType
*/
?>
<nav class="large-3 medium-4 columns" id="actions-sidebar">
<ul class="side-nav">
<li class="heading"><?= __('Actions') ?></li>
<li><?= $this->Html->link(__('List Blockchain Types'), ['action' => 'index']) ?></li>
</ul>
</nav>
<div class="blockchainTypes form large-9 medium-8 columns content">
<?= $this->Form->create($blockchainType) ?>
<fieldset>
<legend><?= __('Add Blockchain Type') ?></legend>
<?php
echo $this->Form->control('name');
echo $this->Form->control('text');
echo $this->Form->control('symbol');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>

View File

@ -0,0 +1,31 @@
<?php
/**
* @var \App\View\AppView $this
* @var \App\Model\Entity\BlockchainType $blockchainType
*/
?>
<nav class="large-3 medium-4 columns" id="actions-sidebar">
<ul class="side-nav">
<li class="heading"><?= __('Actions') ?></li>
<li><?= $this->Form->postLink(
__('Delete'),
['action' => 'delete', $blockchainType->id],
['confirm' => __('Are you sure you want to delete # {0}?', $blockchainType->id)]
)
?></li>
<li><?= $this->Html->link(__('List Blockchain Types'), ['action' => 'index']) ?></li>
</ul>
</nav>
<div class="blockchainTypes form large-9 medium-8 columns content">
<?= $this->Form->create($blockchainType) ?>
<fieldset>
<legend><?= __('Edit Blockchain Type') ?></legend>
<?php
echo $this->Form->control('name');
echo $this->Form->control('text');
echo $this->Form->control('symbol');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>

View File

@ -0,0 +1,51 @@
<?php
/**
* @var \App\View\AppView $this
* @var \App\Model\Entity\BlockchainType[]|\Cake\Collection\CollectionInterface $blockchainTypes
*/
?>
<nav class="large-3 medium-4 columns" id="actions-sidebar">
<ul class="side-nav">
<li class="heading"><?= __('Actions') ?></li>
<li><?= $this->Html->link(__('New Blockchain Type'), ['action' => 'add']) ?></li>
</ul>
</nav>
<div class="blockchainTypes index large-9 medium-8 columns content">
<h3><?= __('Blockchain Types') ?></h3>
<table cellpadding="0" cellspacing="0">
<thead>
<tr>
<th scope="col"><?= $this->Paginator->sort('id') ?></th>
<th scope="col"><?= $this->Paginator->sort('name') ?></th>
<th scope="col"><?= $this->Paginator->sort('text') ?></th>
<th scope="col"><?= $this->Paginator->sort('symbol') ?></th>
<th scope="col" class="actions"><?= __('Actions') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($blockchainTypes as $blockchainType): ?>
<tr>
<td><?= $this->Number->format($blockchainType->id) ?></td>
<td><?= h($blockchainType->name) ?></td>
<td><?= h($blockchainType->text) ?></td>
<td><?= h($blockchainType->symbol) ?></td>
<td class="actions">
<?= $this->Html->link(__('View'), ['action' => 'view', $blockchainType->id]) ?>
<?= $this->Html->link(__('Edit'), ['action' => 'edit', $blockchainType->id]) ?>
<?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $blockchainType->id], ['confirm' => __('Are you sure you want to delete # {0}?', $blockchainType->id)]) ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<div class="paginator">
<ul class="pagination">
<?= $this->Paginator->first('<< ' . __('first')) ?>
<?= $this->Paginator->prev('< ' . __('previous')) ?>
<?= $this->Paginator->numbers() ?>
<?= $this->Paginator->next(__('next') . ' >') ?>
<?= $this->Paginator->last(__('last') . ' >>') ?>
</ul>
<p><?= $this->Paginator->counter(['format' => __('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')]) ?></p>
</div>
</div>

View File

@ -0,0 +1,36 @@
<?php
/**
* @var \App\View\AppView $this
* @var \App\Model\Entity\BlockchainType $blockchainType
*/
?>
<nav class="large-3 medium-4 columns" id="actions-sidebar">
<ul class="side-nav">
<li class="heading"><?= __('Actions') ?></li>
<li><?= $this->Html->link(__('Edit Blockchain Type'), ['action' => 'edit', $blockchainType->id]) ?> </li>
<li><?= $this->Form->postLink(__('Delete Blockchain Type'), ['action' => 'delete', $blockchainType->id], ['confirm' => __('Are you sure you want to delete # {0}?', $blockchainType->id)]) ?> </li>
<li><?= $this->Html->link(__('List Blockchain Types'), ['action' => 'index']) ?> </li>
<li><?= $this->Html->link(__('New Blockchain Type'), ['action' => 'add']) ?> </li>
</ul>
</nav>
<div class="blockchainTypes view large-9 medium-8 columns content">
<h3><?= h($blockchainType->name) ?></h3>
<table class="vertical-table">
<tr>
<th scope="row"><?= __('Name') ?></th>
<td><?= h($blockchainType->name) ?></td>
</tr>
<tr>
<th scope="row"><?= __('Text') ?></th>
<td><?= h($blockchainType->text) ?></td>
</tr>
<tr>
<th scope="row"><?= __('Symbol') ?></th>
<td><?= h($blockchainType->symbol) ?></td>
</tr>
<tr>
<th scope="row"><?= __('Id') ?></th>
<td><?= $this->Number->format($blockchainType->id) ?></td>
</tr>
</table>
</div>