Update get_methods.py
Browse files- get_methods.py +17 -14
get_methods.py
CHANGED
|
@@ -1,36 +1,39 @@
|
|
| 1 |
-
from flask import jsonify
|
| 2 |
from database import get_db
|
| 3 |
|
| 4 |
-
def
|
| 5 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
try:
|
| 7 |
-
if not request.json or "udid" not in request.json:
|
| 8 |
-
return jsonify({"error": "UDID zorunlu alan"}), 400
|
| 9 |
-
|
| 10 |
db = get_db()
|
| 11 |
cursor = db.cursor()
|
| 12 |
|
| 13 |
user = cursor.execute(
|
| 14 |
'SELECT * FROM users WHERE udid = ?',
|
| 15 |
-
(
|
| 16 |
).fetchone()
|
| 17 |
|
| 18 |
if user:
|
| 19 |
return jsonify(dict(user))
|
| 20 |
-
|
|
|
|
| 21 |
cursor.execute(
|
| 22 |
'INSERT INTO users (udid, name, premium_account, usage_limit, invoice) VALUES (?, ?, ?, ?, ?)',
|
| 23 |
-
(
|
| 24 |
-
request.json.get("name"),
|
| 25 |
-
request.json.get("premium_account", False),
|
| 26 |
-
request.json.get("usage_limit", 4),
|
| 27 |
-
request.json.get("invoice"))
|
| 28 |
)
|
| 29 |
db.commit()
|
| 30 |
|
| 31 |
new_user = cursor.execute(
|
| 32 |
'SELECT * FROM users WHERE udid = ?',
|
| 33 |
-
(
|
| 34 |
).fetchone()
|
| 35 |
|
| 36 |
return jsonify(dict(new_user)), 201
|
|
|
|
| 1 |
+
from flask import jsonify
|
| 2 |
from database import get_db
|
| 3 |
|
| 4 |
+
def get_users():
|
| 5 |
+
"""Tüm kullanıcıları getirir"""
|
| 6 |
+
try:
|
| 7 |
+
db = get_db()
|
| 8 |
+
users = db.execute('SELECT * FROM users').fetchall()
|
| 9 |
+
return jsonify([dict(user) for user in users])
|
| 10 |
+
except Exception as e:
|
| 11 |
+
return jsonify({"error": str(e)}), 500
|
| 12 |
+
|
| 13 |
+
def get_user(udid):
|
| 14 |
+
"""UDID ile kullanıcıyı getirir (yoksa oluşturur)"""
|
| 15 |
try:
|
|
|
|
|
|
|
|
|
|
| 16 |
db = get_db()
|
| 17 |
cursor = db.cursor()
|
| 18 |
|
| 19 |
user = cursor.execute(
|
| 20 |
'SELECT * FROM users WHERE udid = ?',
|
| 21 |
+
(udid,)
|
| 22 |
).fetchone()
|
| 23 |
|
| 24 |
if user:
|
| 25 |
return jsonify(dict(user))
|
| 26 |
+
|
| 27 |
+
# Kullanıcı yoksa otomatik oluştur
|
| 28 |
cursor.execute(
|
| 29 |
'INSERT INTO users (udid, name, premium_account, usage_limit, invoice) VALUES (?, ?, ?, ?, ?)',
|
| 30 |
+
(udid, 'guest', False, 3, 'empty')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
)
|
| 32 |
db.commit()
|
| 33 |
|
| 34 |
new_user = cursor.execute(
|
| 35 |
'SELECT * FROM users WHERE udid = ?',
|
| 36 |
+
(udid,)
|
| 37 |
).fetchone()
|
| 38 |
|
| 39 |
return jsonify(dict(new_user)), 201
|