Exemple #1
0
def save_data(key):
    jsoned = trans(request.get_data())
    value = jsoned["payload"]
    res = blockchain.save_data(key, value)
    if res["success"] == True:
        # save to data_on_chain
        an_item = Data_on_chain()
        an_item.key = key
        an_item.value = value
        an_item.tx_id = res['payload']['txId']
        an_item.last_update_time = datetime.datetime.now()
        db.session.add(an_item)
        db.session.commit()
        # save to transactions
        a_tx = Transactions()
        a_tx.key = key
        a_tx.value = value
        a_tx.tx_id = res['payload']['txId']
        a_tx.operation = "create"
        a_tx.timestamp = datetime.datetime.now()
        db.session.add(a_tx)
        db.session.commit()
        db.session.refresh(a_tx)
        return json.dumps(a_tx.to_dict())
    else:
        return "error"
Exemple #2
0
def update_data(key):
    jsoned = trans(request.get_data())
    value = jsoned["payload"]
    res = blockchain.update_data(key, value)
    if res["success"] == True:
        # update data
        an_item = Data_on_chain.query.filter_by(key=key).first()
        # get last one:
        # obj = session.query(ObjectRes).order_by(ObjectRes.id.desc()).first()
        an_item.value = value
        an_item.tx_id = res['payload']['txId']
        an_item.last_update_time = datetime.datetime.now()
        db.session.add(an_item)
        db.session.commit()
        # save to transactions
        a_tx = Transactions()
        a_tx.key = key
        a_tx.value = value
        a_tx.tx_id = res['payload']['txId']
        a_tx.operation = "update"
        a_tx.timestamp = datetime.datetime.now()
        db.session.add(a_tx)
        db.session.commit()
        db.session.refresh(a_tx)

        return json.dumps(a_tx.to_dict())
    else:
        return "error"
Exemple #3
0
def delete_data(key):
    res = blockchain.delete_data(key)
    if res["success"] == True:
        # delete item in data_on_chain
        item = Data_on_chain.query.filter_by(key=key).first()
        db.session.delete(item)
        db.session.commit()
        # save to transactions
        a_tx = Transactions()
        a_tx.key = key
        a_tx.tx_id = res['payload']['txId']
        a_tx.operation = "delete"
        a_tx.timestamp = datetime.datetime.now()
        db.session.add(a_tx)
        db.session.commit()
        db.session.refresh(a_tx)
        return json.dumps(a_tx.to_dict())
    else:
        return "error"