浏览代码

發送api調整

CJYen 3 年之前
父节点
当前提交
14c8b1e426
共有 1 个文件被更改,包括 23 次插入11 次删除
  1. 23 11
      main.py

+ 23 - 11
main.py

@@ -221,24 +221,36 @@ def receive(userid):
 async def receive(userModel : models.TransactionNft):
     # db connect
     db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/arkcard?charset=utf8mb4')
+    table = db['users']
     table2 = db['nft']
+    table3 = db['nftdrops']
 
-    # input
+    # input and find userid
     nftid = userModel.nftid
     address = userModel.address
-    result = ""
-    if not table2.find_one(id=nftid):
+    result = table.find_one(useraddress=address)
+
+    # first confirm if the user exist
+    if not result:
         db.close()
-        print("error: nft not found")
-        return {'msg': 'nft not found'}
+        return {'msg': 'user address not found'}
     else:
-        statement = 'SELECT userid FROM users WHERE useraddress ="'+address+'"'
-        for row in db.query(statement):
-            result = row['userid']
-        user_obj = table2.find_one(id=nftid)
-        user_obj['userid'] = result
-        table2.update(dict(user_obj), ['id'])
+        userid = result['userid']
+        db.close()
+
+    # update nft owner
+    if table3.find_one(nftid=nftid):
+        data = dict(nftid=nftid, userid=userid)
+        table3.update(data, ['nftid'])
+        db.close()
+    elif table2.find_one(id=nftid):
+        data = dict(id=nftid, userid=userid)
+        table2.update(data, ['id'])
         db.close()
+    else:
+        db.close()
+        return {'msg': 'nft not found'}
+    db.close()
     return {'msg': 'OK'}
 
 # shop handler