Browse Source

send api revised

CJYen 3 years ago
parent
commit
31e6ae2efd
2 changed files with 64 additions and 29 deletions
  1. 63 27
      main.py
  2. 1 2
      models.py

+ 63 - 27
main.py

@@ -203,43 +203,79 @@ def receive(userid):
         db.close()
         return {"userid": result['userid'], "address": result['address']}
 
-
 # send handler
 @app.post("/send")
 async def receive(userModel : models.TransactionNft):
+    # 從網頁上選定好商品,在選定好數量後輸入收方地址;將把該商品轉到收方,並存下交易記錄
+    # 從ownership找到該userid的擁有人,再把擁有人userid改為收方,並在trans留下記錄
     # db connect
     db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/arkcard?charset=utf8mb4')
-    table = db['users']
-    table2 = db['nft']
-    table3 = db['nftdrops']
+    table1 = db['users']
+    table2 = db['ownership']
+    table3 = db['transactions']
 
-    # input and find userid
-    nftid = userModel.nftid
+    # find the userid from the selected nft
+    fromuser = userModel.fromuser
     address = userModel.address
-    result = table.find_one(useraddress=address)
-
-    # first confirm if the user exist
-    if not result:
+    result1 = table1.find_one(address=address)
+    result2 = table1.find_one(userid=fromuser)
+    # 例:
+    # {
+    #   "fromuser": "a01",
+    #   "address": "0x4cd0ea8b1bdb5ab9249d96ccf3d8a0d3ada2bc76",
+    #   "dic": {"10":"2", "20":"3"}
+    # }
+    # 第一個為nftid, 第二個為amount,可以同時輸入多組nft給同一人
+    the_list = userModel.dic
+    fr_token = "from token"
+    _hash = "hash"
+
+    # confirm if the user exist
+    # 如果沒有這個接受者,回傳錯誤
+    if not result1:
         db.close()
-        return {'msg': 'user address not found'}
+        return {'msg': 'user not found'}
+    # 確認發送方的nft夠不夠發放
     else:
-        userid = result['userid']
-
-
-    # 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'])
+        for nftid, amount in the_list.items():
+            print("nft: "+nftid+",數量:"+ amount)
+            # 直接針對取到的值去做確認數量和更新表單,最後再記錄
+            # 確認sender的總數
+            result3 = db.query('SELECT SUM(amount) AS total FROM arkcard.ownership WHERE userid="' + fromuser + '" AND nftid ="' + nftid + '";')
+
+            # 把總數撈出來,存到total
+            for row in result3:
+                rows = row
+            total = rows['total']
+
+            # 如果總數不夠,就報錯回傳
+            if total == None or total < int(amount):
+                db.close()
+                return {"messge: NFT not enough"}
+
+            # 總數夠,在ownership刪去數量,並再新增ownership一筆
+            else:
+                # 今天到這,需要找出怎麼扣除總數的方式,目前找到的總數,會更新到資料欄位,導致原本才100個的,會被更新成499個,因為全加到一個
+                # 找接收者的userid
+                to_user = result1['userid']
+                old_address = result2['address']
+                print(total)
+                new_amount = total - int(amount)
+                # 更新原user總數
+                result4 = db.query('DELETE FROM arkcard.ownership WHERE userid="' + fromuser + '" AND nftid ="' + nftid + '";')
+                result5 = db.query('UPDATE arkcard.ownership SET amount = amount - "' + amount + '" WHERE userid = "' + fromuser + '" AND nftid = "' + nftid + '" LIMIT 1;')
+                new_data1 = dict(userid=fromuser, address=old_address, nftid=nftid, amount=new_amount)
+                table2.insert(new_data1)
+                # 增加新user數量
+                new_data2 = dict(userid=to_user, address=address, nftid=nftid, amount=int(amount))
+                table2.insert(new_data2)
+
+                # 在transactions記錄from和to, 以及時間等等
+                now = dt.datetime.now()
+                trans_data = dict(to_token=address, time=now, amount=int(amount), userid=fromuser, from_token=fr_token, hash=_hash)
+                table3.insert(trans_data)
         db.close()
-    else:
-        db.close()
-        return {'msg': 'nft not found'}
-
-    return {'msg': 'OK'}
+        return {"messge: NFT 夠"}
 
 # shop handler
 @app.get("/shop/{userid}")

+ 1 - 2
models.py

@@ -1,8 +1,7 @@
 from pydantic import BaseModel
 
 class TransactionNft(BaseModel):
-    nftid: str
-    address: str
+    dic: dict
 
 class BuyNft(BaseModel):
     nftid: int