From 6e2fcf7096a65578a7c14b6cbfdb9ef33c3cb0ea Mon Sep 17 00:00:00 2001 From: Anton Franzluebbers Date: Thu, 27 Apr 2023 17:30:11 -0400 Subject: [PATCH] no nulls because null != null --- velconnect/CreateDB.sql | 2 +- velconnect/routes/api.py | 47 +++++++++++++++++++++++----------------- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/velconnect/CreateDB.sql b/velconnect/CreateDB.sql index ebc88b8..380017c 100644 --- a/velconnect/CreateDB.sql +++ b/velconnect/CreateDB.sql @@ -72,7 +72,7 @@ CREATE TABLE `DataBlock` ( `id` TEXT NOT NULL, -- id of the owner of this file. Ownership is not transferable because ids may collide, -- but the owner could be null for global scope - `owner_id` TEXT, + `owner_id` TEXT NOT NULL DEFAULT 'none', `visibility` TEXT CHECK( `visibility` IN ('public','private','unlisted') ) NOT NULL DEFAULT 'public', -- This is an indexable field to filter out different types of datablocks `category` TEXT, diff --git a/velconnect/routes/api.py b/velconnect/routes/api.py index 0db9cd2..d658846 100644 --- a/velconnect/routes/api.py +++ b/velconnect/routes/api.py @@ -92,7 +92,8 @@ def get_device_by_pairing_code(pairing_code: str): def get_device_by_pairing_code_dict(pairing_code: str) -> dict | None: - values = db.query("SELECT * FROM `Device` WHERE `pairing_code`=:pairing_code;", {'pairing_code': pairing_code}) + values = db.query( + "SELECT * FROM `Device` WHERE `pairing_code`=:pairing_code;", {'pairing_code': pairing_code}) if len(values) == 1: device = dict(values[0]) parse_data(device) @@ -101,7 +102,8 @@ def get_device_by_pairing_code_dict(pairing_code: str) -> dict | None: def get_user_for_device(hw_id: str) -> dict: - values = db.query("""SELECT * FROM `UserDevice` WHERE `hw_id`=:hw_id;""", {'hw_id': hw_id}) + values = db.query( + """SELECT * FROM `UserDevice` WHERE `hw_id`=:hw_id;""", {'hw_id': hw_id}) if len(values) == 1: user_id = dict(values[0])['user_id'] user = get_user_dict(user_id=user_id) @@ -149,7 +151,8 @@ def get_device_data(request: Request, response: Response, hw_id: str): room_data = get_data(response, key=room_key, user_id=user['id']) if "error" in room_data: - set_data(request, data={}, key=room_key, modified_by=None, category="room") + set_data(request, data={}, key=room_key, + modified_by=None, category="room") room_data = get_data(response, key=room_key, user_id=user['id']) return {'device': block, 'room': room_data, 'user': user} @@ -261,22 +264,24 @@ def set_data(request: fastapi.Request, data: dict, key: str = None, owner: str = if len(old_data_query) == 1: old_data: dict = json.loads(old_data_query[0]["data"]) data = {**old_data, **data} + + # add the data to the db + db.insert(""" + UPDATE `DataBlock` SET + category = :category, + modified_by = :modified_by, + data = :data, + last_modified = CURRENT_TIMESTAMP + WHERE id=:id AND owner_id = :owner_id; + """, {"id": key, "category": category, "modified_by": modified_by, "owner_id": owner, "data": json.dumps(data)}) + else: + # add the data to the db + db.insert(""" + INSERT INTO `DataBlock` (id, owner_id, category, modified_by, data, last_modified) + VALUES(:id, :owner_id, :category, :modified_by, :data, CURRENT_TIMESTAMP); + """, {"id": key, "owner_id": owner, "category": category, "modified_by": modified_by, "data": json.dumps(data)}) - # add the data to the db - db.insert(""" - REPLACE INTO `DataBlock` (id, owner_id, category, modified_by, data, last_modified) - VALUES(:id, :owner_id, :category, :modified_by, :data, CURRENT_TIMESTAMP); - """, {"id": key, "owner_id": owner, "category": category, "modified_by": modified_by, "data": json.dumps(data)}) - - # # add the data to the db - # db.insert(""" - # REPLACE INTO `DataBlock` SET - # category = :category, - # modified_by = :modified_by, - # data = :data, - # last_modified = CURRENT_TIMESTAMP - # WHERE id=:id AND owner_id = :owner_id; - # """, {"id": key, "category": category, "modified_by": modified_by, "owner_id": owner, "data": json.dumps(data)}) + return {'key': key} @@ -347,7 +352,8 @@ def get_user(response: Response, user_id: str): def get_user_dict(user_id: str) -> dict | None: - values = db.query("SELECT * FROM `User` WHERE `id`=:user_id;", {'user_id': user_id}) + values = db.query( + "SELECT * FROM `User` WHERE `id`=:user_id;", {'user_id': user_id}) if len(values) == 1: user = dict(values[0]) parse_data(user) @@ -377,7 +383,8 @@ async def upload_file(request: fastapi.Request, file: UploadFile, key: str | Non content = await file.read() # async read await out_file.write(content) # async write # add a datablock to link to the file - set_data(request, data={'filename': file.filename}, key=key, category='file', modified_by=modified_by) + set_data(request, data={'filename': file.filename}, + key=key, category='file', modified_by=modified_by) return {"filename": file.filename, 'key': key}