mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
Add last_tx option in query module
Signed-off-by: David Dashyan <mail@davie.li>
This commit is contained in:
parent
55ad023172
commit
eed23c2885
@ -120,29 +120,19 @@ def store_block(conn, block):
|
|||||||
|
|
||||||
|
|
||||||
@register_query(LocalMongoDBConnection)
|
@register_query(LocalMongoDBConnection)
|
||||||
def get_txids_filtered(conn, asset_id, operation=None):
|
def get_txids_filtered(conn, asset_id, operation=None, last_tx=None):
|
||||||
match_create = {
|
|
||||||
'operation': 'CREATE',
|
|
||||||
'id': asset_id
|
|
||||||
}
|
|
||||||
match_transfer = {
|
|
||||||
'operation': 'TRANSFER',
|
|
||||||
'asset.id': asset_id
|
|
||||||
}
|
|
||||||
|
|
||||||
if operation == Transaction.CREATE:
|
match = {
|
||||||
match = match_create
|
Transaction.CREATE: {'operation': 'CREATE', 'id': asset_id},
|
||||||
elif operation == Transaction.TRANSFER:
|
Transaction.TRANSFER: {'operation': 'TRANSFER', 'asset.id': asset_id},
|
||||||
match = match_transfer
|
None: {'$or': [{'asset.id': asset_id}, {'id': asset_id}]},
|
||||||
else:
|
}[operation]
|
||||||
match = {'$or': [match_create, match_transfer]}
|
|
||||||
|
cursor = conn.run(conn.collection('transactions').find(match))
|
||||||
|
|
||||||
|
if last_tx:
|
||||||
|
cursor = cursor.sort({'$natural': DESCENDING}).limit(1)
|
||||||
|
|
||||||
pipeline = [
|
|
||||||
{'$match': match}
|
|
||||||
]
|
|
||||||
cursor = conn.run(
|
|
||||||
conn.collection('transactions')
|
|
||||||
.aggregate(pipeline))
|
|
||||||
return (elem['id'] for elem in cursor)
|
return (elem['id'] for elem in cursor)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -265,7 +265,7 @@ class BigchainDB(object):
|
|||||||
"""Get a list of transactions filtered on some criteria
|
"""Get a list of transactions filtered on some criteria
|
||||||
"""
|
"""
|
||||||
txids = backend.query.get_txids_filtered(self.connection, asset_id,
|
txids = backend.query.get_txids_filtered(self.connection, asset_id,
|
||||||
operation)
|
operation, last_tx)
|
||||||
for txid in txids:
|
for txid in txids:
|
||||||
yield self.get_transaction(txid)
|
yield self.get_transaction(txid)
|
||||||
|
|
||||||
|
|||||||
@ -53,17 +53,8 @@ class TransactionListApi(Resource):
|
|||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
with current_app.config['bigchain_pool']() as bigchain:
|
with current_app.config['bigchain_pool']() as bigchain:
|
||||||
txs = bigchain.get_transactions_filtered(**args)
|
txs = bigchain.get_transactions_filtered(**args)
|
||||||
if args['last_tx'] and args['last_tx'] is True:
|
|
||||||
lastTX = None
|
|
||||||
|
|
||||||
for x in txs:
|
return [tx.to_dict() for tx in txs]
|
||||||
lastTX = x
|
|
||||||
if lastTX:
|
|
||||||
return [lastTX.to_dict()]
|
|
||||||
else:
|
|
||||||
return []
|
|
||||||
else:
|
|
||||||
return [tx.to_dict() for tx in txs]
|
|
||||||
|
|
||||||
def post(self):
|
def post(self):
|
||||||
"""API endpoint to push transactions to the Federation.
|
"""API endpoint to push transactions to the Federation.
|
||||||
|
|||||||
@ -391,6 +391,12 @@ def test_transactions_get_list_good(client):
|
|||||||
['last_tx', None],
|
['last_tx', None],
|
||||||
['operation', 'CREATE']
|
['operation', 'CREATE']
|
||||||
]
|
]
|
||||||
|
url = TX_ENDPOINT + '?asset_id=' + asset_id + '&last_tx=true'
|
||||||
|
assert client.get(url).json == [
|
||||||
|
['asset_id', asset_id],
|
||||||
|
['last_tx', True],
|
||||||
|
['operation', None]
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def test_transactions_get_list_bad(client):
|
def test_transactions_get_list_bad(client):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user