Browse Source

add some seed data

Eren Yilmaz 6 years ago
parent
commit
b57354b539
4 changed files with 69 additions and 12 deletions
  1. 4 2
      controller.py
  2. 1 0
      game.py
  3. 58 8
      model.py
  4. 6 2
      server.py

+ 4 - 2
controller.py

@@ -34,8 +34,10 @@ def register():
     password = request.json['password']
     password = request.json['password']
     if model.user_exists(username):
     if model.user_exists(username):
         return forbidden('User already exists.')
         return forbidden('User already exists.')
-    model.register(username, password)
-    return {'message': "successfully registered user"}
+    if model.register(username, password):
+        return {'message': "successfully registered user"}
+    else:
+        bad_request('registration not successful')
 
 
 
 
 def not_found(msg=None):
 def not_found(msg=None):

+ 1 - 0
game.py

@@ -0,0 +1 @@
+money_amount = 1000000

+ 58 - 8
model.py

@@ -2,6 +2,8 @@ import sqlite3 as db
 import sys
 import sys
 import uuid
 import uuid
 
 
+from game import money_amount
+
 connection = None
 connection = None
 cursor = None
 cursor = None
 
 
@@ -30,21 +32,24 @@ def setup():
     print('Database setup...')
     print('Database setup...')
 
 
     replace = True
     replace = True
+
     if replace:
     if replace:
-        cursor.execute("DROP TABLE users")
-        cursor.execute("DROP TABLE stocks")
-        cursor.execute("DROP TABLE ownership")
-        cursor.execute("DROP TABLE sessions")
+        print('  Dropping old tables...')
+        cursor.execute("DROP TABLE IF EXISTS users")
+        cursor.execute("DROP TABLE IF EXISTS ownables")
+        cursor.execute("DROP TABLE IF EXISTS ownership")
+        cursor.execute("DROP TABLE IF EXISTS sessions")
 
 
+    print('  Creating tables...')
     cursor.execute('''
     cursor.execute('''
                 CREATE TABLE IF NOT EXISTS users(
                 CREATE TABLE IF NOT EXISTS users(
                     username VARCHAR(10) UNIQUE NOT NULL, 
                     username VARCHAR(10) UNIQUE NOT NULL, 
                     password VARCHAR(6) NOT NULL)
                     password VARCHAR(6) NOT NULL)
                 ''')
                 ''')
     cursor.execute('''
     cursor.execute('''
-                CREATE TABLE IF NOT EXISTS stocks(
+                CREATE TABLE IF NOT EXISTS ownables(
                     name VARCHAR(10) UNIQUE NOT NULL, 
                     name VARCHAR(10) UNIQUE NOT NULL, 
-                    total_available INTEGER NOT NULL)
+                    total_amount INTEGER NOT NULL)
                 ''')
                 ''')
     cursor.execute('''
     cursor.execute('''
                 CREATE TABLE IF NOT EXISTS ownership(
                 CREATE TABLE IF NOT EXISTS ownership(
@@ -52,7 +57,7 @@ def setup():
                     stock_id INTEGER NOT NULL,
                     stock_id INTEGER NOT NULL,
                     amount INTEGER NOT NULL DEFAULT 0,
                     amount INTEGER NOT NULL DEFAULT 0,
                     FOREIGN KEY (user_id) REFERENCES users(rowid),
                     FOREIGN KEY (user_id) REFERENCES users(rowid),
-                    FOREIGN KEY (stock_id) REFERENCES stocks(rowid),
+                    FOREIGN KEY (stock_id) REFERENCES ownables(rowid),
                     UNIQUE (user_id, stock_id)
                     UNIQUE (user_id, stock_id)
                 )
                 )
                 ''')
                 ''')
@@ -64,10 +69,43 @@ def setup():
                 )
                 )
                 ''')
                 ''')
 
 
+    print('  Adding initial data...')
+    cursor.execute('''
+                INSERT INTO users
+                (username, password)
+                VALUES ("bank", "")
+                ''')
+    cursor.execute('''
+                SELECT rowid 
+                FROM users
+                WHERE username = "bank"
+                ''')
+    bank_id = cursor.fetchone()[0]
+    cursor.execute('''
+                INSERT INTO ownables
+                (name, total_amount)
+                VALUES ("Kollar", ?)
+                ''', (money_amount,))
+    cursor.execute('''
+                SELECT rowid 
+                FROM users
+                WHERE username = "bank"
+                ''')
+    kollar_id = cursor.fetchone()[0]
+    cursor.execute('''
+                INSERT INTO ownership
+                (user_id, stock_id, amount)
+                VALUES (?, ?, ?)
+                ''', (bank_id, kollar_id, money_amount))
+
 
 
 def login(username, password):
 def login(username, password):
     connect()
     connect()
 
 
+    # do not allow login as bank
+    if password == '':
+        return None
+
     cursor.execute('''
     cursor.execute('''
                 SELECT rowid
                 SELECT rowid
                 FROM users
                 FROM users
@@ -83,12 +121,16 @@ def login(username, password):
 
 
 def register(username, password):
 def register(username, password):
     connect()
     connect()
-
+    if username == '':
+        return False
+    if password == '':
+        return False
     cursor.execute('''
     cursor.execute('''
                 INSERT INTO users 
                 INSERT INTO users 
                 (username, password)
                 (username, password)
                 VALUES (? , ?)
                 VALUES (? , ?)
                 ''', (username, password))
                 ''', (username, password))
+    return True
 
 
 
 
 def new_session(user_id):
 def new_session(user_id):
@@ -119,6 +161,8 @@ def drop_old_sessions():
 
 
 
 
 def user_exists(username):
 def user_exists(username):
+    connect()
+
     cursor.execute('''
     cursor.execute('''
                 SELECT rowid
                 SELECT rowid
                 FROM users
                 FROM users
@@ -129,3 +173,9 @@ def user_exists(username):
         return True
         return True
     else:
     else:
         return False
         return False
+
+
+def move_money(username):
+    connect()
+
+    cursor.execute()

+ 6 - 2
server.py

@@ -1,3 +1,5 @@
+import sqlite3
+
 import connection
 import connection
 import controller
 import controller
 import model
 import model
@@ -19,7 +21,9 @@ if __name__ == '__main__':
             return not_found()
             return not_found()
         response.content_type = 'application/json'
         response.content_type = 'application/json'
         method_to_call = getattr(controller, path)
         method_to_call = getattr(controller, path)
-        return method_to_call()
-
+        try:
+            return method_to_call()
+        except sqlite3.IntegrityError:
+            return controller.bad_request('action violates database constraints')
 
 
     run(host='localhost', port=connection.port, debug=True)
     run(host='localhost', port=connection.port, debug=True)