yusufgundogdu commited on
Commit
ede986f
·
verified ·
1 Parent(s): 1ada13a

Update database.py

Browse files
Files changed (1) hide show
  1. database.py +44 -6
database.py CHANGED
@@ -27,21 +27,59 @@ def init_db(app):
27
  db = get_db()
28
  cursor = db.cursor()
29
 
 
30
  cursor.execute('''
31
  CREATE TABLE IF NOT EXISTS users (
32
  id INTEGER PRIMARY KEY AUTOINCREMENT,
33
  udid TEXT NOT NULL UNIQUE,
34
- platform TEXT DEFAULT '',
35
  premium_account BOOLEAN DEFAULT FALSE,
36
  usage_limit INTEGER DEFAULT 10,
37
- invoice TEXT DEFAULT ''
 
 
38
  )
39
  ''')
40
 
 
41
  cursor.execute('SELECT COUNT(*) FROM users')
42
  if cursor.fetchone()[0] == 0:
43
- cursor.executemany('INSERT INTO users (udid, platform, premium_account, usage_limit, invoice) VALUES (?, ?, ?, ?, ?)',
44
- [('test_udid_1', 'AND', True, 4, 'test_invoice_1'),
45
- ('test_udid_2', 'IOS', False, 4, 'test_invoice_2')])
 
 
 
 
46
 
47
- db.commit()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  db = get_db()
28
  cursor = db.cursor()
29
 
30
+ # Güncellenmiş tablo yapısı
31
  cursor.execute('''
32
  CREATE TABLE IF NOT EXISTS users (
33
  id INTEGER PRIMARY KEY AUTOINCREMENT,
34
  udid TEXT NOT NULL UNIQUE,
35
+ platform TEXT NOT NULL CHECK (platform IN ('AND', 'IOS')),
36
  premium_account BOOLEAN DEFAULT FALSE,
37
  usage_limit INTEGER DEFAULT 10,
38
+ invoice TEXT DEFAULT '',
39
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
40
+ updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
41
  )
42
  ''')
43
 
44
+ # Örnek veri eklerken platform alanını da dolduruyoruz
45
  cursor.execute('SELECT COUNT(*) FROM users')
46
  if cursor.fetchone()[0] == 0:
47
+ cursor.executemany('''
48
+ INSERT INTO users (udid, platform, premium_account, usage_limit, invoice)
49
+ VALUES (?, ?, ?, ?, ?)
50
+ ''', [
51
+ ('test_udid_1', 'AND', True, 4, 'test_invoice_1'),
52
+ ('test_udid_2', 'IOS', False, 4, 'test_invoice_2')
53
+ ])
54
 
55
+ db.commit()
56
+
57
+ def create_user(udid, platform):
58
+ """Yeni kullanıcı oluşturma fonksiyonu"""
59
+ try:
60
+ # Platform kontrolü
61
+ if platform not in ['AND', 'IOS']:
62
+ raise ValueError("Platform must be either 'AND' or 'IOS'")
63
+
64
+ db = get_db()
65
+ cursor = db.cursor()
66
+
67
+ # Yeni kullanıcıyı platform bilgisiyle birlikte ekle
68
+ cursor.execute('''
69
+ INSERT INTO users (udid, platform)
70
+ VALUES (?, ?)
71
+ ''', (udid, platform))
72
+
73
+ db.commit()
74
+
75
+ # Oluşturulan kullanıcıyı döndür
76
+ cursor.execute('SELECT * FROM users WHERE udid = ?', (udid,))
77
+ new_user = cursor.fetchone()
78
+
79
+ return dict(new_user) if new_user else None
80
+
81
+ except sqlite3.IntegrityError:
82
+ raise ValueError("User with this UDID already exists")
83
+ except Exception as e:
84
+ db.rollback()
85
+ raise e