|
@@ -160,220 +160,192 @@ def message(event):
|
|
|
alt_text="Receive",
|
|
|
template=button_template_message))
|
|
|
|
|
|
+@router.post("/push/")
|
|
|
+def push_text(user, message):
|
|
|
+ line_bot_api.push_message(user, TextSendMessage(text=message))
|
|
|
+
|
|
|
# nft collection api
|
|
|
@router.get("/collection/{userid}")
|
|
|
def collection(userid):
|
|
|
- # 連到ownership表單去找,並回傳json
|
|
|
# db connect
|
|
|
db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/arkcard?charset=utf8mb4')
|
|
|
- table1 = db['ownership']
|
|
|
+ table3 = db['nftdrops']
|
|
|
+ table2 = db['nft']
|
|
|
+ nftdrops = {}
|
|
|
nft = {}
|
|
|
+ nfts_all = {}
|
|
|
i = 0
|
|
|
- if not table1.find_one(userid=userid):
|
|
|
+ j = 0
|
|
|
+ if not table3.find_one(userid=userid) and not table2.find_one(userid=userid):
|
|
|
db.close()
|
|
|
return "error: user don't have any nft"
|
|
|
+
|
|
|
else:
|
|
|
- results = db.query("SELECT nftid, amount FROM arkcard.ownership WHERE userid IN (SELECT userid FROM arkcard.ownership WHERE userid = '"+userid+"');")
|
|
|
- for item in results:
|
|
|
- nft[i] = item
|
|
|
+ results1 = table3.find(userid=userid)
|
|
|
+ for item in results1:
|
|
|
+ nft_id = item['nftid']
|
|
|
+ nftdrops[i] = table2.find_one(id=nft_id)
|
|
|
i += 1
|
|
|
+
|
|
|
+ results2 = table2.find(userid=userid)
|
|
|
+ for item in results2:
|
|
|
+ nft[j] = item
|
|
|
+ j += 1
|
|
|
+
|
|
|
+ nfts_all[0] = nftdrops
|
|
|
+ nfts_all[1] = nft
|
|
|
+ return nfts_all
|
|
|
db.close()
|
|
|
- return nft
|
|
|
|
|
|
# receive handler
|
|
|
@router.get("/receive/{userid}")
|
|
|
def receive(userid):
|
|
|
- # 確定要傳送的對象,到user去找到它
|
|
|
# db connect
|
|
|
db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/arkcard?charset=utf8mb4')
|
|
|
table = db['users']
|
|
|
|
|
|
+ table.find_one(userid=userid)
|
|
|
if not table.find_one(userid=userid):
|
|
|
db.close()
|
|
|
return "ERROR: User Not Found"
|
|
|
else:
|
|
|
result = table.find_one(userid=userid)
|
|
|
- db.close()
|
|
|
- return {"userid": result['userid'], "address": result['address']}
|
|
|
+ return {"userid": result['userid'], "useraddress": result['useraddress']}
|
|
|
+ db.close()
|
|
|
|
|
|
# send handler
|
|
|
@router.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')
|
|
|
- table1 = db['users']
|
|
|
- table2 = db['ownership']
|
|
|
- table3 = db['transactions']
|
|
|
+ table = db['users']
|
|
|
+ table2 = db['nft']
|
|
|
+ table3 = db['nftdrops']
|
|
|
|
|
|
- # find the userid from the selected nft
|
|
|
- fromuser = userModel.fromuser
|
|
|
+ # input and find userid
|
|
|
+ nftid = userModel.nftid
|
|
|
address = userModel.address
|
|
|
- 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:
|
|
|
+ result = table.find_one(useraddress=address)
|
|
|
+
|
|
|
+ # first confirm if the user exist
|
|
|
+ if not result:
|
|
|
db.close()
|
|
|
- return {'msg': 'user not found'}
|
|
|
- # 確認發送方的nft夠不夠發放
|
|
|
+ return {'msg': 'user address not found'}
|
|
|
else:
|
|
|
- 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)
|
|
|
+ userid = result['userid']
|
|
|
+
|
|
|
+ # update nft owner
|
|
|
+ if table3.find_one(nftid=nftid):
|
|
|
+ data = dict(nftid=nftid, userid=userid)
|
|
|
+ table3.update(data, ['nftid'])
|
|
|
+
|
|
|
+ # push訊息
|
|
|
+ result3 = table2.find_one(id=nftid)
|
|
|
+ title = result3['title']
|
|
|
+ message = "您的NFT : " + title + ", 已劃轉成功!"
|
|
|
+ push_text(userid, message)
|
|
|
+
|
|
|
+ db.close()
|
|
|
+ elif table2.find_one(id=nftid):
|
|
|
+ result3 = table2.find_one(id=nftid)
|
|
|
+
|
|
|
+ pre_own = result3['userid']
|
|
|
+ data = dict(id=nftid, userid=userid)
|
|
|
+ table2.update(data, ['id'])
|
|
|
+
|
|
|
+ # push訊息
|
|
|
+ try:
|
|
|
+ title = result3['title']
|
|
|
+ fr = "您的NFT : " + title + ", 已發送成功!"
|
|
|
+ to = "您的NFT : "+title+", 已收到!"
|
|
|
+ push_text(userid, to)
|
|
|
+ push_text(pre_own, fr)
|
|
|
+ except:
|
|
|
+ return "找不到原使用者"
|
|
|
+
|
|
|
+ db.close()
|
|
|
+ else:
|
|
|
+ # push訊息
|
|
|
+ message = "交易失敗!如果有疑問,請洽網站的服務信箱!"
|
|
|
+ push_text(userid, message)
|
|
|
db.close()
|
|
|
- return {"messge: NFT 夠"}
|
|
|
+ return {'msg': 'nft not found'}
|
|
|
+ return {'msg': 'OK'}
|
|
|
|
|
|
# shop handler
|
|
|
-@router.get("/shop")
|
|
|
-def shop():
|
|
|
- # 為了顯示正確的nft網頁和數量,直接顯示nft表出來
|
|
|
+@router.get("/shop/{userid}")
|
|
|
+def shop(userid):
|
|
|
# db connect
|
|
|
db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/arkcard?charset=utf8mb4')
|
|
|
- result1 = db.query('SELECT nftid, launched, amount, type, title, context FROM arkcard.nft WHERE launched="y" AND nftid NOT IN(SELECT nftid FROM arkcard.nft_to_event);')
|
|
|
- nfts = {}
|
|
|
+
|
|
|
+ sql = 'SELECT DISTINCT(title), id, imgurl, userid FROM arkcard.nft WHERE id<1001 and userid IS NULL GROUP BY title LIMIT 5'
|
|
|
+ result = db.query(sql)
|
|
|
+ rows = {}
|
|
|
i = 0
|
|
|
- for row in result1:
|
|
|
- nfts[i] = row
|
|
|
+ for row in result:
|
|
|
+ rows[i] = row
|
|
|
i += 1
|
|
|
- return nfts
|
|
|
+ return rows
|
|
|
db.close()
|
|
|
|
|
|
@router.post("/buy")
|
|
|
-async def buy(userModel : models.BuyNft):
|
|
|
+async def buy(userModel: models.BuyNft):
|
|
|
# db connect
|
|
|
db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/arkcard?charset=utf8mb4')
|
|
|
- table1 = db['users']
|
|
|
- table2 = db['ownership']
|
|
|
- table3 = db['nft']
|
|
|
- table4 = db['transactions']
|
|
|
- shops = "shop"
|
|
|
- hash = "hash"
|
|
|
+ table2 = db['nft']
|
|
|
|
|
|
# input
|
|
|
- the_list = userModel.dic
|
|
|
- address = userModel.address
|
|
|
- # 例:
|
|
|
- # {
|
|
|
- # "address": "d04",
|
|
|
- # "dic": {"1":"2","2":"3"}
|
|
|
- # }
|
|
|
-
|
|
|
- result1 = table1.find_one(address=address)
|
|
|
+ nftid = userModel.nftid
|
|
|
+ userid = userModel.userid
|
|
|
|
|
|
- # 找不到該使用者就回報錯誤
|
|
|
- if not result1:
|
|
|
+ if not table2.find_one(id=nftid):
|
|
|
db.close()
|
|
|
- return "該用戶不存在!如果有疑問,請洽網站的服務信箱!"
|
|
|
+ # push訊息
|
|
|
+ message = "購買失敗!如果有疑問,請洽網站的服務信箱!"
|
|
|
+ push_text(userid, message)
|
|
|
+ return "該NFT商品不存在!如果有疑問,請洽網站的服務信箱!"
|
|
|
else:
|
|
|
- for nftid, amount in the_list.items():
|
|
|
- # 先user找到該使用者的userid留著備用
|
|
|
- userid = result1['userid']
|
|
|
-
|
|
|
- # 更新到ownership
|
|
|
- owner_data = [dict(userid=userid, address=address, nftid=nftid, amount=int(amount))]
|
|
|
- table2.insert_many(owner_data)
|
|
|
-
|
|
|
- # 更新nft數量
|
|
|
- result3 = table3.find_one(nftid=nftid)
|
|
|
- nft_total = result3['amount']
|
|
|
- total = int(nft_total) - int(amount)
|
|
|
- nft_data = dict(nftid=nftid, amount=int(total))
|
|
|
- table3.update(nft_data, ['nftid'])
|
|
|
-
|
|
|
- # 最後再來處理transactions
|
|
|
- now = dt.datetime.now()
|
|
|
- trans_data = dict(to_token=address, time=now, amount=int(amount), userid=userid, from_token=shops, hash=hash)
|
|
|
- table4.insert(trans_data)
|
|
|
+ user_obj = table2.find_one(id=nftid)
|
|
|
+ user_obj['userid'] = userid
|
|
|
+ table2.update(dict(user_obj), ['id'])
|
|
|
+
|
|
|
+ # push訊息
|
|
|
+ result3 = table2.find_one(id=nftid)
|
|
|
+ title = result3['title']
|
|
|
+ message = "您的NFT : " + title + ", 已購買成功!"
|
|
|
+ push_text(userid, message)
|
|
|
+
|
|
|
db.close()
|
|
|
- return "您已購買成功!"
|
|
|
+ return "您已購買成功!"
|
|
|
|
|
|
@router.post("/event")
|
|
|
async def nftdrops(userModel : models.NftDrops):
|
|
|
# db connect
|
|
|
db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/arkcard?charset=utf8mb4')
|
|
|
- table1 = db['nft_drop_list']
|
|
|
+ table3 = db['nftdrops']
|
|
|
|
|
|
- # input
|
|
|
+ # input對應
|
|
|
+ eventid = '1'
|
|
|
+ nftid = '1001'
|
|
|
+ nftid2 = '1002'
|
|
|
userid = userModel.userid
|
|
|
email = userModel.email
|
|
|
+ now = dt.datetime.now()
|
|
|
|
|
|
- the_current_event = eventPeriod()
|
|
|
-
|
|
|
- if the_current_event == 0:
|
|
|
- return "目前沒有任何進行中的活動"
|
|
|
+ # 如果userid不在db, 寫入到一個空值nft
|
|
|
+ if not table3.find_one(userid=userid):
|
|
|
+ # 新增資料
|
|
|
+ table3.insert(dict(eventid=eventid, nftid=nftid, userid=userid, email=email, time=now))
|
|
|
+ table3.insert(dict(eventid=eventid, nftid=nftid2, userid=userid, email=email, time=now))
|
|
|
+ db.close()
|
|
|
+ # push訊息
|
|
|
+ message = "已為您登記活動!"
|
|
|
+ push_text(userid, message)
|
|
|
+ return "新增成功"
|
|
|
+ # 如果userid存在,回傳通知
|
|
|
else:
|
|
|
- # 再針對有上架的 event 填寫客戶到 nftdroplist
|
|
|
- now = dt.datetime.now()
|
|
|
- result1 = table1.find_one(userid=userid)
|
|
|
- if not result1:
|
|
|
- for key, item in the_current_event.items():
|
|
|
- table1.insert(dict(eventid=item, userid=userid, email=email, time=now))
|
|
|
- db.close()
|
|
|
- return "完成加入"
|
|
|
- else:
|
|
|
- db.close()
|
|
|
- return "該資料已存在"
|
|
|
-
|
|
|
-@router.post("/eventPeriod")
|
|
|
-def eventPeriod():
|
|
|
- # db connect
|
|
|
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/arkcard?charset=utf8mb4')
|
|
|
- # 設定event始終期間,與上架與否
|
|
|
- table3 = db['event']
|
|
|
- now = dt.datetime.now()
|
|
|
- # 先去找 event 表,確認在期間之內
|
|
|
- result = db.query("SELECT * FROM arkcard.event WHERE start < NOW() and NOW() < end and launched='yes';")
|
|
|
- rows = {}
|
|
|
- i = 0
|
|
|
- for row in result:
|
|
|
- rows[i] = row['eventid']
|
|
|
- i += 1
|
|
|
- return rows
|
|
|
- db.close()
|
|
|
+ db.close()
|
|
|
+ # push訊息
|
|
|
+ message = "您已登記過活動了!"
|
|
|
+ push_text(userid, message)
|
|
|
+ return "已有資料"
|