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)
|
||||
def get_txids_filtered(conn, asset_id, operation=None):
|
||||
match_create = {
|
||||
'operation': 'CREATE',
|
||||
'id': asset_id
|
||||
}
|
||||
match_transfer = {
|
||||
'operation': 'TRANSFER',
|
||||
'asset.id': asset_id
|
||||
}
|
||||
def get_txids_filtered(conn, asset_id, operation=None, last_tx=None):
|
||||
|
||||
if operation == Transaction.CREATE:
|
||||
match = match_create
|
||||
elif operation == Transaction.TRANSFER:
|
||||
match = match_transfer
|
||||
else:
|
||||
match = {'$or': [match_create, match_transfer]}
|
||||
match = {
|
||||
Transaction.CREATE: {'operation': 'CREATE', 'id': asset_id},
|
||||
Transaction.TRANSFER: {'operation': 'TRANSFER', 'asset.id': asset_id},
|
||||
None: {'$or': [{'asset.id': asset_id}, {'id': asset_id}]},
|
||||
}[operation]
|
||||
|
||||
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)
|
||||
|
||||
|
||||
|
||||
@ -265,7 +265,7 @@ class BigchainDB(object):
|
||||
"""Get a list of transactions filtered on some criteria
|
||||
"""
|
||||
txids = backend.query.get_txids_filtered(self.connection, asset_id,
|
||||
operation)
|
||||
operation, last_tx)
|
||||
for txid in txids:
|
||||
yield self.get_transaction(txid)
|
||||
|
||||
|
||||
@ -53,17 +53,8 @@ class TransactionListApi(Resource):
|
||||
args = parser.parse_args()
|
||||
with current_app.config['bigchain_pool']() as bigchain:
|
||||
txs = bigchain.get_transactions_filtered(**args)
|
||||
if args['last_tx'] and args['last_tx'] is True:
|
||||
lastTX = None
|
||||
|
||||
for x in txs:
|
||||
lastTX = x
|
||||
if lastTX:
|
||||
return [lastTX.to_dict()]
|
||||
else:
|
||||
return []
|
||||
else:
|
||||
return [tx.to_dict() for tx in txs]
|
||||
return [tx.to_dict() for tx in txs]
|
||||
|
||||
def post(self):
|
||||
"""API endpoint to push transactions to the Federation.
|
||||
|
||||
@ -391,6 +391,12 @@ def test_transactions_get_list_good(client):
|
||||
['last_tx', None],
|
||||
['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):
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user