CJYen 3 سال پیش
والد
کامیت
f22f8a2bf1
1فایلهای تغییر یافته به همراه43 افزوده شده و 27 حذف شده
  1. 43 27
      main.py

+ 43 - 27
main.py

@@ -119,14 +119,14 @@ def message(event):
                 alt_text="Receive",
                 template=button_template_message))
 
-    elif '我的NFT收藏' in event.message.text:
+    elif 'NFT商店' in event.message.text:
         button_template_message = ButtonsTemplate(
             title=' ',
-            text='點擊並打開收藏頁面,就可以查看所有的收藏品哦!',
+            text='點擊並打開NFT商品頁,就可以購買您所想要的NFT商品哦!',
             actions=[
                 URITemplateAction(
-                    label='打開我的NFT收藏頁面',
-                    uri='http://ark.cards/collect.html?' + event.source.user_id),])
+                    label='打開NFT商品頁面',
+                    uri='http://ark.cards/shop.html?' + event.source.user_id),])
         line_bot_api.reply_message(
             event.reply_token,
             TemplateSendMessage(
@@ -152,30 +152,16 @@ def message(event):
 def collection(userid):
     # db connect
     db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/arkcard?charset=utf8mb4')
-    table1 = db['users']
-    table2 = db['nft']
 
-    if table1.find_one(userid=userid) and not table2.find_one(userid=userid):
-        sql = 'SELECT * FROM nft WHERE userid IS NULL LIMIT 5'
-        result = db.query(sql)
-        rows = {}
-        i = 0
-        for row in result:
-            rows[i] = row
-            i += 1
-        return rows
-    elif not table2.find_one(userid=userid):
-        return "Error: User not found."
-    else:
-        xname = userid
-        sql = 'SELECT a.* FROM nft a INNER JOIN (SELECT userid FROM users WHERE userid="' + xname + '") b ON b.userid = a.userid'
-        result = db.query(sql)
-        rows = {}
-        i = 0
-        for row in result:
-            rows[i] = row
-            i += 1
-        return rows
+    xname = userid
+    sql = 'SELECT a.* FROM nft a INNER JOIN (SELECT userid FROM users WHERE userid="'+xname+'") b ON b.userid = a.userid'
+    result = db.query(sql)
+    rows = {}
+    i = 0
+    for row in result:
+        rows[i] = row
+        i += 1
+    return rows
 
 # receive handler
 @app.get("/receive/{userid}")
@@ -215,5 +201,35 @@ async def receive(userModel : models.TransactionNft):
         table2.update(dict(user_obj), ['id'])
     return {'msg': 'OK'}
 
+# shop handler
+@app.get("/shop/{userid}")
+def buy(userid):
+    # db connect
+    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/arkcard?charset=utf8mb4')
+
+    sql = 'SELECT * FROM nft WHERE userid IS NULL LIMIT 5'
+    result = db.query(sql)
+    rows = {}
+    i = 0
+    for row in result:
+        rows[i] = row
+        i += 1
+    return rows
+
+@app.get("/buy/{userid}/{nftid}")
+async def buy(userid,nftid):
+    # db connect
+    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/arkcard?charset=utf8mb4')
+    table2 = db['nft']
+
+    if not table2.find_one(id=nftid):
+        print("error: nft not found")
+        return {'msg': 'nft not found'}
+    else:
+        user_obj = table2.find_one(id=nftid)
+        user_obj['userid'] = userid
+        table2.update(dict(user_obj), ['id'])
+    return {'msg': 'OK'}
+
 if __name__ == '__main__':
     uvicorn.run("main:app", host="0.0.0.0", port=8228, reload=True)