delete in db
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
moerp 2024-02-01 16:43:31 +01:00
parent 5c8f057843
commit ce38d6cf24
3 changed files with 52 additions and 9 deletions

View File

@ -13,7 +13,8 @@ RUN \
apk add --no-cache build-base libpq-dev && \ apk add --no-cache build-base libpq-dev && \
pip install -r requirements.txt && \ pip install -r requirements.txt && \
if [ "${VERSION_ID2}" != "" ]; then VERSION_ID=${VERSION_ID2}; else VERSION_ID=${VERSION_ID1}; fi && \ 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 EXPOSE 8080

View File

@ -175,8 +175,30 @@ def get_database_entries():
if conn: if conn:
conn.close() 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) exposed_app = ProxyFix(app, x_for=1, x_host=1)

View File

@ -70,14 +70,34 @@
function deleteSelectedRows() { function deleteSelectedRows() {
const table = document.getElementById('database-nutrition-table'); const table = document.getElementById('database-nutrition-table');
Array.from(table.rows).forEach(row => { const selectedRows = Array.from(table.querySelectorAll('tr.selected'));
if (row.classList.contains('selected')) { const foodNames = selectedRows.map(row => row.cells[0].innerText); // Annahme, der Name des Lebensmittels befindet sich in der ersten Zelle
// Logik zum Löschen der Zeile aus der Datenbank
table.deleteRow(row.rowIndex); 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() { function showPasswordPrompt() {
document.getElementById('delete-row-button').style.display = 'none'; document.getElementById('delete-row-button').style.display = 'none';
document.getElementById('password-prompt').style.display = 'block'; document.getElementById('password-prompt').style.display = 'block';