diff --git a/src/db.py b/src/db.py index 58594cc..c43c03d 100644 --- a/src/db.py +++ b/src/db.py @@ -212,6 +212,11 @@ def delete_item(uuid): """, (uuid,)) cursor.execute(""" DELETE + FROM listcontent + WHERE item_uuid = %s + """, (uuid,)) + cursor.execute(""" + DELETE FROM item WHERE uuid = %s """, (uuid,)) @@ -387,6 +392,12 @@ def fetch_all(): converted_row = (row[0], row[1], str(row[2]), row[3], row[4], row[5], row[6]) converted_rows.append(converted_row) results[0] = converted_rows + # timestamp fix in table history + converted_rows = [] + for row in results[3]: + converted_row = (row[0], row[1], row[2], row[3], row[4], f'{row[5]}') + converted_rows.append(converted_row) + results[3] = converted_rows return results def restore_db(data): @@ -394,18 +405,34 @@ def restore_db(data): tables = ["item", "itemlist", "listcontent", "history"] connection = connect_db() cursor = connection.cursor() + item_uuid = {} for elem in data["item"]: # check if item already exists query = f'SELECT * FROM item WHERE itemid={str(elem["itemid"])} and skuid={str(elem["skuid"])}' cursor.execute(query) result = cursor.rowcount + # complete item uuid dict + item_uuid[elem["uuid"]] = (elem["itemid"], elem["skuid"]) if result == 0: + # if item doesn't already exist, insert it in database attributes = str(elem["attributes"]).split(',') if len(str(elem["attributes"]))>0 else "[]::text[]" query = f'INSERT INTO item (uuid, itemid, skuid, choice, attributes, image, show) VALUES (nextval(\'uuid_sequence\'), {str(elem["itemid"])}, {str(elem["skuid"])}, {str(elem["choice"])}, ARRAY{attributes}, \'{str(elem["image"])}\', {str(elem["show"])})' cursor.execute(query) - for table in tables: - for elem in data[table]: - print(elem) + for elem in data["history"]: + # check if history entry already exists + query = f'SELECT * FROM history WHERE h_timestamp=\'{elem["h_timestamp"]}\'' + cursor.execute(query) + result = cursor.rowcount + if result == 0: + # find related item + (rel_itemid, rel_skuid) = item_uuid[elem["uuid"]] + query = f'SELECT uuid FROM item WHERE itemid={rel_itemid} and skuid={rel_skuid}' + cursor.execute(query) + uuid = cursor.fetchall()[0][0] + # insert history entry + query = f'INSERT INTO history (uuid, price, currency, quantity, discount_percentage, h_timestamp) VALUES ({uuid}, \'{elem["price"]}\', \'{elem["currency"]}\', {elem["quantity"]}, {elem["discount_percentage"]}, \'{elem["h_timestamp"]}\')' + cursor.execute(query) + cursor.close() connection.commit() connection.close() diff --git a/web/restore.js b/web/restore.js index 73eaeb9..08d8666 100644 --- a/web/restore.js +++ b/web/restore.js @@ -1,4 +1,4 @@ -function handleFileUpload(event) { +function handleFileUpload() { const file = document.getElementById('inputGroupFile01').files[0]; const reader = new FileReader(); @@ -6,7 +6,6 @@ function handleFileUpload(event) { const rawData = e.target.result; var blocks = rawData.split('#####'); blocks.shift() // 1st element is empty - console.log(blocks); var data = {}; blocks.forEach(function (block) { const split = block.indexOf('\n');