]> Nutra Git (v1) - nutratech/gui.git/commitdiff
read CSV recipes, too. lint.
authorShane Jaroch <chown_tee@proton.me>
Mon, 26 Jan 2026 10:47:13 +0000 (05:47 -0500)
committerShane Jaroch <chown_tee@proton.me>
Mon, 26 Jan 2026 10:47:13 +0000 (05:47 -0500)
.github/workflows/ci-full.yml
.github/workflows/ubuntu-24.04.yml
include/db/reciperepository.h
src/db/reciperepository.cpp
src/mainwindow.cpp

index 943a8cf69ba2cb18d2b048e7daa44a3553c8e7bf..2ae5b294e6b8a53ae2540ac585386ae51b43a2fd 100644 (file)
@@ -37,4 +37,6 @@ jobs:
         run: make release
 
       - name: Test
+        env:
+          QT_QPA_PLATFORM: offscreen
         run: make test
index c20728dba98eedcfc0a5ee70c1ebc7a3098358b8..81ff1269314766f219fda6153ffad19175e825d0 100644 (file)
@@ -30,6 +30,8 @@ jobs:
         run: make release
 
       - name: Test
+        env:
+          QT_QPA_PLATFORM: offscreen
         run: make test
 
       - name: Upload Artifact
index 57914e57afbcec84f003720d8bae5594c84e183b..df4312d2eda43075c278b0dda90b72470952a44e 100644 (file)
@@ -33,6 +33,8 @@ public:
     std::vector<RecipeItem> getAllRecipes();
     RecipeItem getRecipe(int id);
 
+    void loadCsvRecipes(const QString& directory);
+
     // Ingredients
     bool addIngredient(int recipeId, int foodId, double amount);
     bool removeIngredient(int recipeId, int foodId);
index bfc715f123efa0bd10324726195d8983436e5cf6..31fc8598a2c3e9ac38af0af136d6087a7c43a84f 100644 (file)
@@ -178,3 +178,60 @@ std::vector<RecipeIngredient> RecipeRepository::getIngredients(int recipeId) {
     }
     return ingredients;
 }
+
+#include <QDir>
+#include <QFile>
+
+void RecipeRepository::loadCsvRecipes(const QString& directory) {
+    QDir dir(directory);
+    if (!dir.exists()) return;
+
+    QStringList filters;
+    filters << "*.csv";
+    QFileInfoList fileList = dir.entryInfoList(filters, QDir::Files);
+
+    for (const auto& fileInfo : fileList) {
+        QFile file(fileInfo.absoluteFilePath());
+        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) continue;
+
+        while (!file.atEnd()) {
+            QString line = file.readLine().trimmed();
+            if (line.isEmpty() || line.startsWith("#")) continue;
+
+            QStringList parts = line.split(',');
+            if (parts.size() < 4) continue;
+
+            QString recipeName = parts[0].trimmed();
+            QString instructions = parts[1].trimmed();
+            int foodId = parts[2].toInt();
+            double amount = parts[3].toDouble();
+
+            if (foodId <= 0 || amount <= 0) continue;
+
+            // Check if recipe exists or create it
+            int recipeId = -1;
+
+            // Inefficient check, but works for now.
+            // Better: Cache existing recipe names -> IDs or add getRecipeByName
+            auto existingRecipes = getAllRecipes();
+            for (const auto& r : existingRecipes) {
+                if (r.name == recipeName) {
+                    recipeId = r.id;
+                    break;
+                }
+            }
+
+            if (recipeId == -1) {
+                recipeId = createRecipe(recipeName, instructions);
+            }
+
+            if (recipeId != -1) {
+                // Check if ingredient exists?
+                // Or just try insert? database might error on duplicate PK if defined.
+                // Assuming (recipe_id, food_id) unique constraint?
+                addIngredient(recipeId, foodId, amount);
+            }
+        }
+        file.close();
+    }
+}
index c61c043818089ffca475d570f6858de341fad4da..d908ef801879e32e6950ef19d03238ea8c77c206 100644 (file)
@@ -25,6 +25,11 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) {
     }
     setupUi();
     updateRecentFileActions();
+
+    // Load CSV Recipes on startup
+    RecipeRepository repo;  // Temporary instance, or use shared if managed differently.
+    // RecipeRepository uses DatabaseManager singleton so creating an instance is fine.
+    repo.loadCsvRecipes(QDir::homePath() + "/.nutra/recipes");
 }
 
 MainWindow::~MainWindow() = default;