balance get api
This commit is contained in:
parent
55124040ae
commit
d55b7c5aea
2 changed files with 35 additions and 22 deletions
|
@ -249,29 +249,41 @@ def create_app(test_config=None):
|
|||
socketio.emit("error", "418", to=session[id])
|
||||
leave_room(session[id])
|
||||
|
||||
#api
|
||||
@app.route("/api/balance", methods=['POST'])
|
||||
@app.route("/api/balance", methods=['POST', 'GET'])
|
||||
def api_change():
|
||||
db = get_db()
|
||||
c = db.cursor()
|
||||
userid = request.form["id"]
|
||||
c.execute("SELECT * FROM users WHERE id=?", [userid])
|
||||
user_list = c.fetchall()
|
||||
if user_list != []:
|
||||
user = user_list[0]
|
||||
try:
|
||||
change = int(request.args.get("change"))
|
||||
except:
|
||||
change = -1.5
|
||||
c.execute(f"UPDATE users SET balance = balance + {change*100} WHERE id={user[0]}")
|
||||
db.commit()
|
||||
c.execute(f"SELECT * FROM users WHERE id = {userid}")
|
||||
user_new = c.fetchone()
|
||||
log(type="balance", userid=user[0], before=user[2], after=user_new[2])
|
||||
socketio.emit("update", "update")
|
||||
return make_response(json.dumps({"mode":"balance", "username":user[1], "balance":user_new[2]}))
|
||||
else:
|
||||
return make_response(json.dumps({"mode":"error","error":"043"}))
|
||||
if request.method == 'POST':
|
||||
db = get_db()
|
||||
c = db.cursor()
|
||||
userid = request.form["id"]
|
||||
c.execute("SELECT * FROM users WHERE id=?", [userid])
|
||||
user_list = c.fetchall()
|
||||
if user_list != []:
|
||||
user = user_list[0]
|
||||
try:
|
||||
change = int(request.args.get("change"))
|
||||
except:
|
||||
change = -1.5
|
||||
c.execute(f"UPDATE users SET balance = balance + {change*100} WHERE id={user[0]}")
|
||||
db.commit()
|
||||
c.execute(f"SELECT * FROM users WHERE id = {userid}")
|
||||
user_new = c.fetchone()
|
||||
log(type="balance", userid=user[0], before=user[2], after=user_new[2])
|
||||
socketio.emit("update", "update")
|
||||
return make_response(json.dumps({"mode":"balance", "username":user[1], "balance":user_new[2]}))
|
||||
else:
|
||||
return make_response(json.dumps({"mode":"error","error":"043"}))
|
||||
|
||||
elif request.method == 'GET':
|
||||
db = get_db()
|
||||
c = db.cursor()
|
||||
userid = reqest.args.get("id")
|
||||
c.execute("SELECT * FROM users WHERE id=?", [userid])
|
||||
user = c.fetchone()
|
||||
if user != None:
|
||||
return make_response(json.dumps({"mode":"balance", "username":user[1], "balance":user[2]}))
|
||||
else:
|
||||
return make_response(json.dumps({"mode":"error", "error":"043"}))
|
||||
|
||||
|
||||
|
||||
@app.route("/api/tag_id", methods=['POST'])
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
<p>
|
||||
If change = None or NaN the change will be -1
|
||||
</p>
|
||||
<p>Get method ?id=user_id</p>
|
||||
<p>
|
||||
Response:
|
||||
{"mode":"error" "error":"{error}"} or
|
||||
|
|
Loading…
Reference in a new issue