From 760ba8b1ed4c80fc7e0d7d8195ff0f7ddab7e01f Mon Sep 17 00:00:00 2001 From: Shane Jaroch Date: Mon, 26 Jan 2026 05:47:13 -0500 Subject: [PATCH] read CSV recipes, too. lint. --- .github/workflows/ci-full.yml | 2 ++ .github/workflows/ubuntu-24.04.yml | 2 ++ include/db/reciperepository.h | 2 ++ src/db/reciperepository.cpp | 57 ++++++++++++++++++++++++++++++ src/mainwindow.cpp | 5 +++ 5 files changed, 68 insertions(+) diff --git a/.github/workflows/ci-full.yml b/.github/workflows/ci-full.yml index 943a8cf..2ae5b29 100644 --- a/.github/workflows/ci-full.yml +++ b/.github/workflows/ci-full.yml @@ -37,4 +37,6 @@ jobs: run: make release - name: Test + env: + QT_QPA_PLATFORM: offscreen run: make test diff --git a/.github/workflows/ubuntu-24.04.yml b/.github/workflows/ubuntu-24.04.yml index c20728d..81ff126 100644 --- a/.github/workflows/ubuntu-24.04.yml +++ b/.github/workflows/ubuntu-24.04.yml @@ -30,6 +30,8 @@ jobs: run: make release - name: Test + env: + QT_QPA_PLATFORM: offscreen run: make test - name: Upload Artifact diff --git a/include/db/reciperepository.h b/include/db/reciperepository.h index 57914e5..df4312d 100644 --- a/include/db/reciperepository.h +++ b/include/db/reciperepository.h @@ -33,6 +33,8 @@ public: std::vector 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); diff --git a/src/db/reciperepository.cpp b/src/db/reciperepository.cpp index bfc715f..31fc859 100644 --- a/src/db/reciperepository.cpp +++ b/src/db/reciperepository.cpp @@ -178,3 +178,60 @@ std::vector RecipeRepository::getIngredients(int recipeId) { } return ingredients; } + +#include +#include + +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(); + } +} diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index c61c043..d908ef8 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -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; -- 2.52.0