1

Improve demo and error handling

This commit is contained in:
2018-05-05 10:05:23 +03:00
parent c2fda1ffaf
commit b51414e177
3 changed files with 49 additions and 5 deletions

View File

@@ -31,7 +31,13 @@ class TransactionManagerService implements Service {
} }
def builder = new TransactionBuilder() def builder = new TransactionBuilder()
try{
queryBuilder.build(builder) queryBuilder.build(builder)
} catch (Exception e) {
log.error('Failed to build transaction', e)
return Promise.error(e)
}
def transaction = builder.build() def transaction = builder.build()
log.info("Sending transaction $transaction.id to ledger at $config.ledger.remoteUrl") log.info("Sending transaction $transaction.id to ledger at $config.ledger.remoteUrl")
@@ -39,6 +45,7 @@ class TransactionManagerService implements Service {
spec.body.text(mapper.writeValueAsString(transaction)) spec.body.text(mapper.writeValueAsString(transaction))
}).onError { }).onError {
log.error("Failed to send transaction $transaction.id to ledger $config.ledger.remoteUrl") log.error("Failed to send transaction $transaction.id to ledger $config.ledger.remoteUrl")
return Promise.error(it)
}.map { response -> }.map { response ->
if(response.status == Status.OK) { if(response.status == Status.OK) {
mapper.readTree(response.body.text) mapper.readTree(response.body.text)
@@ -68,7 +75,14 @@ class TransactionManagerService implements Service {
log.info("Updating transaction $transactionId content with new query") log.info("Updating transaction $transactionId content with new query")
def builder = new TransactionBuilder(oldTransaction) def builder = new TransactionBuilder(oldTransaction)
try {
queryBuilder.build(builder) queryBuilder.build(builder)
} catch (Exception e) {
log.error("Failed to update transaction", e)
return Promise.error(e)
}
def transaction = builder.build() def transaction = builder.build()
if(transaction.id != transactionId) { if(transaction.id != transactionId) {
@@ -80,6 +94,7 @@ class TransactionManagerService implements Service {
spec.body.text(mapper.writeValueAsString(transaction)) spec.body.text(mapper.writeValueAsString(transaction))
}).onError { }).onError {
log.error("Failed to send transaction $transaction.id to ledger $config.ledger.remoteUrl") log.error("Failed to send transaction $transaction.id to ledger $config.ledger.remoteUrl")
return Promise.error(it)
} }
}.map { response -> }.map { response ->
mapper.readTree(response.body.text) mapper.readTree(response.body.text)
@@ -100,16 +115,21 @@ class TransactionManagerService implements Service {
this.transaction = transaction this.transaction = transaction
} }
void query(String sql){ TransactionBuilder query(String sql){
if(sql == null || sql == '' || sql.trim() == '') {
throw new IllegalArgumentException("SQL statement cannot be null")
}
queries << sql queries << sql
this
} }
String id() { String id() {
transaction.id transaction.id
} }
void complete() { TransactionBuilder complete() {
transaction.end() transaction.end()
this
} }
private BlockTransaction build() { private BlockTransaction build() {

View File

@@ -72,5 +72,17 @@ ratpack {
} }
} }
} }
post('executeQuery') {
def transactionManager = get(TransactionManagerService)
parse(Form).then { Form form ->
def query = form.get('query')
transactionManager.execute {
it.query(query).complete()
} .then {
response.send(it.toString())
}
}
}
} }
} }

View File

@@ -19,6 +19,7 @@
<input type="hidden" name="transactionId" id="transactionId" /> <input type="hidden" name="transactionId" id="transactionId" />
<input name="query" style="width:300px" placeholder="Add query here..." /> <input name="query" style="width:300px" placeholder="Add query here..." />
<button type="submit">Submit Query</button> <button type="submit">Submit Query</button>
<button id="submitAndExecute">Submit&Execute Query</button>
</form> </form>
<form id="executeTransactionForm" action='/'> <form id="executeTransactionForm" action='/'>
<input type="hidden" name="transactionId" id="transactionId" /> <input type="hidden" name="transactionId" id="transactionId" />
@@ -42,7 +43,7 @@
function refreshLedger() { function refreshLedger() {
$.get('/ledger', function(data) { $.get('/ledger', function(data) {
editor.setValue(JSON.stringify(data, null, 2)); editor.setValue(JSON.stringify(data.reverse(), null, 2));
}); });
} }
@@ -68,9 +69,20 @@
}); });
} }
function executeSingleQueryTransaction(event) {
event.preventDefault();
var queryData = $('form#addQueryForm').serialize();
$.post( '/executeQuery', queryData, function(data) {
$('input#transactionId').val(null);
$('button#executeTransactionButton').hide();
refreshLedger();
}, 'json');
}
$( "#addQueryForm" ).submit(addQueryToTransaction); $( "#addQueryForm" ).submit(addQueryToTransaction);
$( "#executeTransactionForm" ).submit(executeTransaction); $( "#executeTransactionForm" ).submit(executeTransaction);
$('button#executeTransactionButton').hide(); $('button#executeTransactionButton').hide();
$('button#submitAndExecute').click(executeSingleQueryTransaction);
window.onload = refreshLedger(); window.onload = refreshLedger();
</script> </script>
</body> </body>