diff --git a/Dockerfile b/Dockerfile index fa8d670..fea81ad 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,8 @@ RUN \ apk add --no-cache build-base libpq-dev && \ pip install -r requirements.txt && \ if [ "${VERSION_ID2}" != "" ]; then VERSION_ID=${VERSION_ID2}; else VERSION_ID=${VERSION_ID1}; fi && \ - sed -i -e 's/VERSION_ID/'$VERSION_ID'/' ${APP_DIR}/templates/index.html + sed -i -e 's/VERSION_ID/'$VERSION_ID'/' ${APP_DIR}/templates/index.html \ + sed -i -e 's/VERSION_ID/'$VERSION_ID'/' ${APP_DIR}/templates/nutrition.html EXPOSE 8080 diff --git a/src/Run.py b/src/Run.py index 95adf91..5e9d058 100644 --- a/src/Run.py +++ b/src/Run.py @@ -175,8 +175,30 @@ def get_database_entries(): if conn: conn.close() + +@app.route('/delete_nutrition', methods=['POST']) +@oidc.accept_token(['openid']) +def delete_nutrition(): + data = request.get_json() + foodNames = data['foodNames'] + + if not foodNames: + return jsonify({'error': 'Keine Lebensmittel zum Löschen angegeben'}), 400 + + try: + conn = psycopg2.connect() + with conn.cursor() as cursor: + query = "DELETE FROM nutrition_table WHERE name = ANY(%s)" + cursor.execute(query, (foodNames,)) + conn.commit() + return jsonify({'message': 'Erfolgreich gelöscht'}), 200 + except Exception as e: + return jsonify({'error': str(e)}), 500 + finally: + if conn: + conn.close() + + exposed_app = ProxyFix(app, x_for=1, x_host=1) - - diff --git a/src/templates/nutrition.html b/src/templates/nutrition.html index 235ad6a..0b6872b 100644 --- a/src/templates/nutrition.html +++ b/src/templates/nutrition.html @@ -70,14 +70,34 @@ function deleteSelectedRows() { const table = document.getElementById('database-nutrition-table'); - Array.from(table.rows).forEach(row => { - if (row.classList.contains('selected')) { - // Logik zum Löschen der Zeile aus der Datenbank - table.deleteRow(row.rowIndex); - } - }); + const selectedRows = Array.from(table.querySelectorAll('tr.selected')); + const foodNames = selectedRows.map(row => row.cells[0].innerText); // Annahme, der Name des Lebensmittels befindet sich in der ersten Zelle + + if (foodNames.length > 0) { + // Holen des Tokens und Senden einer Anfrage an das Backend, um die Einträge zu löschen + getBearerToken(token => { + fetch('/delete_nutrition', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer ' + token + }, + body: JSON.stringify({foodNames: foodNames}) + }).then(response => { + if (response.ok) { + // Erfolgreich gelöscht, entferne Zeilen aus der Tabelle + selectedRows.forEach(row => { + table.deleteRow(row.rowIndex); + }); + } else { + console.error('Fehler beim Löschen der Datenbank-Einträge'); + } + }).catch(error => console.error('Fehler:', error)); + }); + } } + function showPasswordPrompt() { document.getElementById('delete-row-button').style.display = 'none'; document.getElementById('password-prompt').style.display = 'block';