diff --git a/src/app.py b/src/app.py index ab229d1..a9a20f0 100644 --- a/src/app.py +++ b/src/app.py @@ -36,17 +36,31 @@ def update_hist(): fill_db(settings["db"], check_items(settings["item"])) return 'Hello, World!' -@app.route('/add',methods = ['POST']) +@app.route('/add', methods=['POST']) def add_item(): print("adding item") - itemid = request.form['itemid'] - if len(request.form['attributes'])>0: - attributes = request.form['attributes'].split(',') - else: - attributes = [] + + data = request.get_json() + + itemid = data.get('itemid') + attributes = data.get('attributes', '').split(',') if data.get('attributes') else [] + new_item = [(itemid, attributes)] extr = check_items(new_item) - return str(extr) + + if len(extr) > 0: + settings = get_conf() + skuid = list(extr.values())[0]["skuid"] + if check_exist(settings["db"], itemid, skuid): + # item already exists + return jsonify({'status': 3}), 400 + else: + # item is valid + fill_db(settings["db"], extr) + return jsonify({'status': 0}), 200 + else: + # item not valid or can't be parsed + return jsonify({'status': 1}), 400 @app.route('/datahistory',methods = ['POST', 'GET']) def data_history_request(): diff --git a/src/db.py b/src/db.py index 61bc0c8..f24e345 100644 --- a/src/db.py +++ b/src/db.py @@ -31,14 +31,7 @@ def add_history_entry(db_settings, itemid, skuid, choice, attributes, image, pri connection = connect_db(db_settings) cursor = connection.cursor() - cursor.execute(""" - SELECT uuid - FROM item - WHERE itemid = %s - AND skuid = %s - """, (itemid, skuid)) - - if cursor.rowcount == 0: + if not check_exist(db_settings, itemid, skuid): add_item(db_settings, itemid, skuid, choice, attributes, image) cursor.execute(""" SELECT uuid @@ -82,6 +75,27 @@ def get_item(db_settings): connection.close() return results +def check_exist(db_settings, itemid, skuid): + '''check if an item is already in the database''' + connection = connect_db(db_settings) + cursor = connection.cursor() + + cursor.execute(""" + SELECT uuid + FROM item + WHERE itemid = %s + AND skuid = %s + """, (itemid, skuid)) + + result = cursor.rowcount + cursor.close() + connection.close() + + if result == 0: + return False + else: + return True + def export_csv(db_settings): '''join item and history data from database and export it in ./output.csv''' connection = connect_db(db_settings) diff --git a/web/app.html b/web/app.html index 3cffd63..49dd6a6 100644 --- a/web/app.html +++ b/web/app.html @@ -1,16 +1,25 @@ - + + + + + + Aliexpress price tracker + + - -
+ + - - + +
+ + diff --git a/web/app.js b/web/app.js index 4650fd6..cbd4d12 100644 --- a/web/app.js +++ b/web/app.js @@ -23,13 +23,16 @@ async function fetch_history() { try { const response = await fetch(`${currentUrl}app/datahistory`); const rawData = await response.json(); + var dateFormat = d3.timeParse("%a, %d %b %Y %H:%M:%S GMT"); + console.log(rawData) // SELECT uuid, quantity, discount_percentage, price, currency, h_timestamp let historyData = rawData.map(d => ({ uuid: d[0], value: parseFloat(d[3].replace('$', '')), currency: d[4], - date: new Date(d[5].replace(' ', 'T')), + date: dateFormat(d[5]), })); + console.log(historyData) return historyData; } catch (error) { console.error('Error fetching data history: ', error); @@ -281,3 +284,38 @@ fetch_history().then(async function(data) { }); }) + + +// add item +document.getElementById("addbutton").addEventListener("click", addItem); + +async function addItem() { + + const apiUrl = `${currentUrl}app/add`; + + const postData = { + itemid: document.getElementById("additemid").value, + attributes: document.getElementById("addattributes").value + }; + + const requestOptions = { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(postData) + }; + + const response = await fetch(apiUrl, requestOptions) + .catch(error => { + console.error('Error during POST request:', error); + }); + + + if (response.ok) { + console.log(response); + } else { + throw new Error('Error in server response'); + } + +} diff --git a/web/style.css b/web/style.css new file mode 100644 index 0000000..3d26938 --- /dev/null +++ b/web/style.css @@ -0,0 +1,18 @@ +body { + margin: 0; + padding: 0; +} + +.banner { + background-color: #177013; /* background color */ + color: #fff; /* text color */ + padding: 10px; + position: fixed; + top: 0; + width: 100%; +} + +.content { + margin-top: 100px; + padding: 0px; +}