diff --git a/Lab2/Lab2_TDDD97.pdf b/Lab2/Lab2_TDDD97.pdf deleted file mode 100644 index 8741618203cc393fa3aeda5664eb76499b611d55..0000000000000000000000000000000000000000 Binary files a/Lab2/Lab2_TDDD97.pdf and /dev/null differ diff --git a/Lab2/__pycache__/database_helper.cpython-310.pyc b/Lab2/__pycache__/database_helper.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..d37e5e85171172afe6ad9b5abdbe258c244d477d Binary files /dev/null and b/Lab2/__pycache__/database_helper.cpython-310.pyc differ diff --git a/Lab2/__pycache__/server.cpython-310.pyc b/Lab2/__pycache__/server.cpython-310.pyc index d4a802448d06f2ba5d488e43c0f83b48d17c9578..643957fffe8a2fd38c2dbc2da828d3507eca7cf5 100644 Binary files a/Lab2/__pycache__/server.cpython-310.pyc and b/Lab2/__pycache__/server.cpython-310.pyc differ diff --git a/Lab2/database.db b/Lab2/database.db index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..9ba997756462131c9a44e42d231628fd95e249f4 100644 Binary files a/Lab2/database.db and b/Lab2/database.db differ diff --git a/Lab2/database_helper.py b/Lab2/database_helper.py index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..66853cf3a74ce0253651396574ddd3173ef02fe5 100644 --- a/Lab2/database_helper.py +++ b/Lab2/database_helper.py @@ -0,0 +1,27 @@ +import sqlite3 +from flask import g + +DATABASE_URI = "database.db" + +def get_db(): + db = getattr(g, 'db', None) + if db is None: + db = g.db = sqlite3.connect(DATABASE_URI) + + return db + + +def create_user(email, password, firstname, familyname, gender, city, country): + try: + get_db().execute("INSERT into USERS values(?, ?, ?, ?, ?, ?, ?);", [firstname, familyname, gender, city, country, email, password]); + get_db().commit() + return True + except: + return False + +def clean_db(): + try: + get_db().execute("DROP table USERS;") + return True + except: + return False diff --git a/Lab2/schema.sql b/Lab2/schema.sql index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..9f6c426264f830a4ae354985724eefc000dfa322 100644 --- a/Lab2/schema.sql +++ b/Lab2/schema.sql @@ -0,0 +1,10 @@ +CREATE TABLE USERS( + FIRST_NAME VARCHAR (25) NOT NULL, + FAMILY_NAME VARCHAR (25) NOT NULL, + GENDER VARCHAR(6) NOT NULL, + CITY VARCHAR(20) NOT NULL, + COUNTRY VARCHAR(20) NOT NULL, + EMAIL VARCHAR(20) NOT NULL, + PASSWORD VARCHAR(20) NOT NULL, + PRIMARY KEY (EMAIL) +); diff --git a/Lab2/server.py b/Lab2/server.py index 8c946cfe393c3e27475adccbc3fb9d71cee2fcaa..0f2b6708536ab16eff13e4613b63154c3afa9b08 100644 --- a/Lab2/server.py +++ b/Lab2/server.py @@ -1,7 +1,50 @@ -from flask import Flask +from flask import Flask, request, jsonify + +import database_helper app = Flask(__name__) -@app.route("/") +@app.route("/", methods = ['GET']) def hello_world(): return "<p>Hello, Lab_2!</p>", 200 + + +@app.route("/sign_up", methods = ['POST']) +def sign_up(): + data = request.get_json() + if ('email' in data and isinstance(data['email'], str) + and 'password' in data and isinstance(data['password'], str) + and 'firstname' in data and isinstance(data['firstname'], str) + and 'familyname' in data and isinstance(data['familyname'], str) + and 'gender' in data and isinstance(data['gender'], str) + and 'city' in data and isinstance(data['city'], str) + and 'country' in data and isinstance(data['country'], str) + ): + if(len(data['password']) > 5 and len(data['password']) < 21 and len(data['email']) > 0 + and len(data['firstname']) > 0 and len(data['familyname']) > 0 and len(data['gender']) > 0 + and len(data['city']) > 0 and len(data['country']) > 0 ): + resp = database_helper.create_user(data['email'], data['password'], data['firstname'], + data['familyname'], data['gender'], data['city'], data['country']) + if resp: + return "", 201 + else: + return "", 409 + else: + return "", 400 + else: + return "test", 400 + + + +@app.route("/delete", methods = ['GET']) +def delete(): + resp = database_helper.clean_db() + if resp: + return "", 200 + else: + return "", 409 + + +if __name__ == '__main__': + app.debug = True + app.run()