Explorar o código

collection api: add purchase from 5 unsold nft

CJYen %!s(int64=3) %!d(string=hai) anos
pai
achega
8b545b55fa
Modificáronse 1 ficheiros con 23 adicións e 9 borrados
  1. 23 9
      main.py

+ 23 - 9
main.py

@@ -152,16 +152,30 @@ 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']
 
-    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
+    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
 
 # receive handler
 @app.get("/receive/{userid}")