balance get api

This commit is contained in:
bton 2024-02-20 17:33:56 +01:00
parent 55124040ae
commit d55b7c5aea
2 changed files with 35 additions and 22 deletions

View file

@ -249,29 +249,41 @@ def create_app(test_config=None):
socketio.emit("error", "418", to=session[id]) socketio.emit("error", "418", to=session[id])
leave_room(session[id]) leave_room(session[id])
#api @app.route("/api/balance", methods=['POST', 'GET'])
@app.route("/api/balance", methods=['POST'])
def api_change(): def api_change():
db = get_db() if request.method == 'POST':
c = db.cursor() db = get_db()
userid = request.form["id"] c = db.cursor()
c.execute("SELECT * FROM users WHERE id=?", [userid]) userid = request.form["id"]
user_list = c.fetchall() c.execute("SELECT * FROM users WHERE id=?", [userid])
if user_list != []: user_list = c.fetchall()
user = user_list[0] if user_list != []:
try: user = user_list[0]
change = int(request.args.get("change")) try:
except: change = int(request.args.get("change"))
change = -1.5 except:
c.execute(f"UPDATE users SET balance = balance + {change*100} WHERE id={user[0]}") change = -1.5
db.commit() c.execute(f"UPDATE users SET balance = balance + {change*100} WHERE id={user[0]}")
c.execute(f"SELECT * FROM users WHERE id = {userid}") db.commit()
user_new = c.fetchone() c.execute(f"SELECT * FROM users WHERE id = {userid}")
log(type="balance", userid=user[0], before=user[2], after=user_new[2]) user_new = c.fetchone()
socketio.emit("update", "update") log(type="balance", userid=user[0], before=user[2], after=user_new[2])
return make_response(json.dumps({"mode":"balance", "username":user[1], "balance":user_new[2]})) socketio.emit("update", "update")
else: return make_response(json.dumps({"mode":"balance", "username":user[1], "balance":user_new[2]}))
return make_response(json.dumps({"mode":"error","error":"043"})) 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']) @app.route("/api/tag_id", methods=['POST'])

View file

@ -29,6 +29,7 @@
<p> <p>
If change = None or NaN the change will be -1 If change = None or NaN the change will be -1
</p> </p>
<p>Get method ?id=user_id</p>
<p> <p>
Response: Response:
{"mode":"error" "error":"{error}"} or {"mode":"error" "error":"{error}"} or