From 3e6d743fd442228fc958be77697b0d8939183d7a Mon Sep 17 00:00:00 2001 From: moerp Date: Thu, 4 Apr 2024 12:25:51 +0200 Subject: [PATCH] back --- src/Run.py | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/src/Run.py b/src/Run.py index 4231f7f..c75d271 100644 --- a/src/Run.py +++ b/src/Run.py @@ -24,35 +24,28 @@ app.logger.handlers = logging.getLogger('gunicorn.error').handlers datapw = 'dasrtwegdffgtewrt4335wferg' -def calculate_nutrition(food, weight, portions): +def calculate_nutrition(food, weight): try: - conn = psycopg2.connect( - host=os.environ.get('PGHOST', 'localhost'), - database=os.environ.get('PGDATABASE', 'csafdb'), - user=os.environ.get('PGUSER', 'user'), - password=os.environ.get('PGPASSWORD', 'password') - ) + conn = psycopg2.connect() with conn.cursor() as cursor: + # Abfrage der Nährwertdaten aus der Datenbank cursor.execute('SELECT kcal, EW, Fett, KH, BST, CA FROM nutrition_table WHERE name = %s', (food,)) + result = cursor.fetchone() if result: - # Teilen des Gewichts durch die Anzahl der Portionen und Aufrunden - weight_per_portion = ceil(weight / portions) - - kcal, ew, fett, kh, bst, ca = [value * weight_per_portion / 100 for value in result] - return { - "weight_per_portion": weight_per_portion, - "nutrition_values": { - "kcal": round(kcal), - "ew": round(ew, 1), - "fett": round(fett, 1), - "kh": round(kh, 1), - "bst": round(bst, 1), - "ca": round(ca) - } - } + # Runden und Berechnen der Nährwerte basierend auf dem Gewicht + kcal, ew, fett, kh, bst, ca = result + nutrition_values = [ + round(kcal * weight / 100), # kcal gerundet auf ganze Zahl + round(ew * weight / 100, 1), # EW gerundet auf eine Dezimalstelle + round(fett * weight / 100, 1), # Fett gerundet auf eine Dezimalstelle + round(kh * weight / 100, 1), # KH gerundet auf eine Dezimalstelle + round(bst * weight / 100, 1), # BST gerundet auf eine Dezimalstelle + round(ca * weight / 100) # CA gerundet auf ganze Zahl + ] + return nutrition_values else: return None finally: