From 1151c3e9a2a97f32303f01b9788fa778ba1a74ce Mon Sep 17 00:00:00 2001 From: Sam Hadow Date: Sun, 11 Feb 2024 17:38:17 +0100 Subject: [PATCH] list create / add item, no interactivity yet --- src/app.py | 16 ++++++++++++ src/db.py | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++ web/app.html | 11 ++++++++ web/app.js | 17 +++++++++++++ 4 files changed, 116 insertions(+) diff --git a/src/app.py b/src/app.py index e53c085..046d8eb 100644 --- a/src/app.py +++ b/src/app.py @@ -60,6 +60,22 @@ def toggle_item_show(): toggle_show(uuid) return jsonify({'toggled show/hide ': uuid}), 200 +@app.route('/create-list', methods=['POST']) +def createList(): + data = request.get_json() + name = data.get('name') + description = data.get('description') + create_list(name, description) + return jsonify({'created list ': name}), 200 + +@app.route('/add-to-list', methods=['POST']) +def addToList(): + date = request.get_json() + list_uuid = data.get('list_uuid') + item_uuid = data.get('item_uuid') + add_to_list(list_uuid, item_uuid) + return jsonify({'list': list_uuid, 'item': item_uuid}), 200 + @app.route('/datahistory',methods = ['GET']) def data_history_request(): if request.method == 'GET': diff --git a/src/db.py b/src/db.py index 7b793d0..dc4f89d 100644 --- a/src/db.py +++ b/src/db.py @@ -161,6 +161,35 @@ def delete_item(uuid): connection.commit() connection.close() +def create_list(name, description): + ''' + initialize an itemlist in the database + ''' + if len(name) <= 50: + connection = connect_db() + cursor = connection.cursor() + cursor.execute(""" + INSERT INTO itemlist (uuid, name, description) + VALUES (nextval('uuid_list_sequence'), %s, %s) + """, (name, description)) + connection.commit() + connection.close() + return 1 + else: + return 0 + +def add_to_list(list_uuid, item_uuid): + ''' + add an item to an itemlist + ''' + connection = connect_db() + cursor = connection.cursor() + cursor.execute(""" + INSERT INTO listcontent (list_uuid, item_uuid) + VALUES (%s, %s) + """, (list_uuid, item_uuid)) + connection.commit() + connection.close() def export_csv(): '''join item and history data from database and export it in ./output.csv''' @@ -196,14 +225,28 @@ def initialize(): DROP TABLE IF EXISTS item """) cursor.execute(""" + DROP TABLE IF EXISTS itemlist + """) + cursor.execute(""" + DROP TABLE IF EXISTS listcontent + """) + cursor.execute(""" DROP SEQUENCE IF EXISTS uuid_sequence """) cursor.execute(""" + DROP SEQUENCE IF EXISTS uuid_list_sequence + """) + cursor.execute(""" CREATE SEQUENCE uuid_sequence INCREMENT BY 1 START WITH 1 """) cursor.execute(""" + CREATE SEQUENCE uuid_list_sequence + INCREMENT BY 1 + START WITH 1 + """) + cursor.execute(""" CREATE TABLE item ( uuid int, @@ -217,6 +260,25 @@ def initialize(): ) """) cursor.execute(""" + CREATE TABLE itemlist + ( + uuid int, + name varchar(50), + description text, + primary key (uuid) + ) + """) + cursor.execute(""" + CREATE TABLE listcontent + ( + list_uuid int, + item_uuid int, + foreign key (item_uuid) references item(uuid), + foreign key (list_uuid) references itemlist(uuid), + primary key (list_uuid, item_uuid) + ) + """) + cursor.execute(""" CREATE TABLE history ( uuid int, @@ -248,6 +310,16 @@ def check_schema(): select * from pg_tables where tablename = 'history' + ) + and exists( + select * + from pg_tables + where tablename = 'itemlist' + ) + and exists( + select * + from pg_tables + where tablename = 'listcontent' ); """) result = cursor.fetchall() diff --git a/web/app.html b/web/app.html index 70cd18d..b2b79d4 100644 --- a/web/app.html +++ b/web/app.html @@ -18,9 +18,20 @@ + + + +