stablediffusionapi / consume_method.py
yusufgundogdu's picture
Update consume_method.py
365b00c verified
raw
history blame
1.22 kB
from flask import jsonify
from database import get_db
def consume_user(udid):
try:
db = get_db()
user = db.execute(
'SELECT * FROM users WHERE udid = ?',
(udid,)
).fetchone()
if not user:
return jsonify({
"success": False,
"error": "Kullanıcı bulunamadı"
}), 404
if user['usage_limit'] <= 0:
return jsonify({
"success": False,
"error": "Kullanım hakkınız kalmadı"
}), 403
db.execute(
'UPDATE users SET usage_limit = usage_limit - 1 WHERE udid = ?',
(udid,)
)
db.commit()
updated_user = db.execute(
'SELECT * FROM users WHERE udid = ?',
(udid,)
).fetchone()
return jsonify({
"success": True,
"data": {
"user": dict(updated_user),
"remaining_usage": updated_user['usage_limit']
}
})
except Exception as e:
return jsonify({
"success": False,
"error": str(e)
}), 500