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);
}
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();
+ }
+}
}
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;