From cfd4179664855819d0845b1364ab86aaf86c71e5 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Fri, 2 Mar 2018 10:11:26 +0100 Subject: [PATCH] Created load and save instruction methods in file utils. --- RecipeDB.pro | 3 +-- main.cpp | 5 +++- utils/fileutils.cpp | 5 ---- utils/fileutils.h | 56 ++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 55 insertions(+), 14 deletions(-) delete mode 100644 utils/fileutils.cpp diff --git a/RecipeDB.pro b/RecipeDB.pro index 2c957a5..61f3bf6 100644 --- a/RecipeDB.pro +++ b/RecipeDB.pro @@ -24,8 +24,7 @@ SOURCES += model/recipe/instruction.cpp \ model/recipe/tags/recipetag.cpp \ SQLite/sqlite3.c \ model/database/resulttable.cpp \ - model/database/recipedatabase.cpp \ - utils/fileutils.cpp + model/database/recipedatabase.cpp HEADERS += model/recipe/instruction.h \ model/recipe/recipe.h \ diff --git a/main.cpp b/main.cpp index 085d24a..f83a2c0 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,7 @@ #include "model/database/database.h" #include "model/database/recipedatabase.h" +#include "utils/fileutils.h" int main(int argc, char *argv[]) { @@ -20,7 +21,9 @@ int main(int argc, char *argv[]) RecipeDatabase recipeDB("recipes"); recipeDB.storeIngredient(Ingredient("Apple", "Fruit")); recipeDB.storeIngredient(Ingredient("Corn", "Vegetable")); - recipeDB.executeSQL("SELECT ingredientId FROM ingredient;").printData(); + recipeDB.executeSQL("SELECT * FROM ingredient;").printData(); + + FileUtils::saveInstruction(1, Instruction("This is some plain text with no HTML markup.")); return a.exec(); } diff --git a/utils/fileutils.cpp b/utils/fileutils.cpp deleted file mode 100644 index 0fa0670..0000000 --- a/utils/fileutils.cpp +++ /dev/null @@ -1,5 +0,0 @@ -#include "fileutils.h" - -fileUtils::fileUtils(){ - -} diff --git a/utils/fileutils.h b/utils/fileutils.h index 76bdfc2..ed23f19 100644 --- a/utils/fileutils.h +++ b/utils/fileutils.h @@ -1,11 +1,55 @@ #ifndef FILEUTILS_H #define FILEUTILS_H +#include +#include +#include +#include -class fileUtils -{ - public: - fileUtils(); -}; +#include "model/recipe/instruction.h" -#endif // FILEUTILS_H \ No newline at end of file +namespace FileUtils { + + QString appDataPath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/.recipeDB/"; + + void ensureAppDataFolderExists(){ + QDir folder(appDataPath); + if (!folder.exists()){ + folder.mkpath("."); + } + } + + void saveInstruction(int nr, Instruction instruction){ + ensureAppDataFolderExists(); + QString filename = appDataPath + QString::fromStdString(std::to_string(nr)) +".html"; + QFile file(filename); + if (file.open(QIODevice::WriteOnly)){ + QTextStream stream(&file); + stream<