Periodic update of progress. #4
|
@ -3,12 +3,40 @@
|
||||||
|
|
||||||
NewRecipeDialog::NewRecipeDialog(QWidget *parent) :
|
NewRecipeDialog::NewRecipeDialog(QWidget *parent) :
|
||||||
QDialog(parent),
|
QDialog(parent),
|
||||||
ui(new Ui::NewRecipeDialog)
|
ui(new Ui::NewRecipeDialog){
|
||||||
{
|
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
NewRecipeDialog::~NewRecipeDialog()
|
NewRecipeDialog::NewRecipeDialog(RecipeDatabase *db, QWidget *parent) : NewRecipeDialog(parent){
|
||||||
{
|
this->recipeDB = db;
|
||||||
|
|
||||||
|
|
||||||
|
this->populateIngredientsBox();
|
||||||
|
this->populateUnitsBox();
|
||||||
|
}
|
||||||
|
|
||||||
|
NewRecipeDialog::~NewRecipeDialog(){
|
||||||
delete ui;
|
delete ui;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NewRecipeDialog::on_toolButton_clicked(){
|
||||||
|
ui->instructionsTextEdit->setFontItalic(!ui->instructionsTextEdit->fontItalic());
|
||||||
|
}
|
||||||
|
|
||||||
|
void NewRecipeDialog::populateIngredientsBox(){
|
||||||
|
this->ingredients = this->recipeDB->retrieveAllIngredients();
|
||||||
|
ui->ingredientNameBox->clear();
|
||||||
|
for (unsigned int i = 0; i < this->ingredients.size(); i++){
|
||||||
|
QString s = QString::fromStdString(this->ingredients[i].getName());
|
||||||
|
ui->ingredientNameBox->insertItem(i, s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void NewRecipeDialog::populateUnitsBox(){
|
||||||
|
this->units = this->recipeDB->retrieveAllUnitsOfMeasure();
|
||||||
|
ui->unitComboBox->clear();
|
||||||
|
for (unsigned int i = 0; i < this->units.size(); i++){
|
||||||
|
QString s = QString::fromStdString(this->units[i].getName());
|
||||||
|
ui->unitComboBox->insertItem(i, s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -2,6 +2,9 @@
|
||||||
#define NEWRECIPEDIALOG_H
|
#define NEWRECIPEDIALOG_H
|
||||||
|
|
||||||
#include <QDialog>
|
#include <QDialog>
|
||||||
|
#include <QTextCharFormat>
|
||||||
|
|
||||||
|
#include "model/database/recipedatabase.h"
|
||||||
|
|
||||||
namespace Ui {
|
namespace Ui {
|
||||||
class NewRecipeDialog;
|
class NewRecipeDialog;
|
||||||
|
@ -13,10 +16,21 @@ class NewRecipeDialog : public QDialog
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit NewRecipeDialog(QWidget *parent = 0);
|
explicit NewRecipeDialog(QWidget *parent = 0);
|
||||||
|
NewRecipeDialog(RecipeDatabase *db, QWidget *parent = 0);
|
||||||
~NewRecipeDialog();
|
~NewRecipeDialog();
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void on_toolButton_clicked();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Ui::NewRecipeDialog *ui;
|
Ui::NewRecipeDialog *ui;
|
||||||
|
RecipeDatabase *recipeDB;
|
||||||
|
vector<Ingredient> ingredients;
|
||||||
|
vector<UnitOfMeasure> units;
|
||||||
|
|
||||||
|
//Helper functions to fill fields.
|
||||||
|
void populateIngredientsBox();
|
||||||
|
void populateUnitsBox();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // NEWRECIPEDIALOG_H
|
#endif // NEWRECIPEDIALOG_H
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
<x>0</x>
|
<x>0</x>
|
||||||
<y>0</y>
|
<y>0</y>
|
||||||
<width>640</width>
|
<width>640</width>
|
||||||
<height>640</height>
|
<height>689</height>
|
||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
<property name="sizePolicy">
|
<property name="sizePolicy">
|
||||||
|
@ -22,26 +22,26 @@
|
||||||
<property name="windowTitle">
|
<property name="windowTitle">
|
||||||
<string>New Recipe</string>
|
<string>New Recipe</string>
|
||||||
</property>
|
</property>
|
||||||
<property name="styleSheet">
|
|
||||||
<string notr="true">font: 25 14pt "Noto Sans CJK KR";</string>
|
|
||||||
</property>
|
|
||||||
<layout class="QVBoxLayout" name="verticalLayout">
|
<layout class="QVBoxLayout" name="verticalLayout">
|
||||||
<item alignment="Qt::AlignTop">
|
<item alignment="Qt::AlignTop">
|
||||||
<widget class="QWidget" name="recipeNamePanel" native="true">
|
<widget class="QWidget" name="mainContentPanel" native="true">
|
||||||
<layout class="QVBoxLayout" name="verticalLayout_2">
|
<layout class="QVBoxLayout" name="verticalLayout_2">
|
||||||
|
<item>
|
||||||
|
<widget class="QWidget" name="recipeNamePanel" native="true">
|
||||||
|
<layout class="QVBoxLayout" name="verticalLayout_9">
|
||||||
<item>
|
<item>
|
||||||
<widget class="QLabel" name="recipeNameLabel">
|
<widget class="QLabel" name="recipeNameLabel">
|
||||||
<property name="font">
|
<property name="font">
|
||||||
<font>
|
<font>
|
||||||
<family>Noto Sans CJK KR</family>
|
<family>Noto Sans CJK KR</family>
|
||||||
<pointsize>14</pointsize>
|
<pointsize>14</pointsize>
|
||||||
<weight>3</weight>
|
<weight>50</weight>
|
||||||
<italic>false</italic>
|
<italic>false</italic>
|
||||||
<bold>false</bold>
|
<bold>false</bold>
|
||||||
</font>
|
</font>
|
||||||
</property>
|
</property>
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>New Recipe Name</string>
|
<string>Edit Recipe</string>
|
||||||
</property>
|
</property>
|
||||||
<property name="alignment">
|
<property name="alignment">
|
||||||
<set>Qt::AlignCenter</set>
|
<set>Qt::AlignCenter</set>
|
||||||
|
@ -58,6 +58,9 @@
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QWidget" name="basicInfoPanel" native="true">
|
<widget class="QWidget" name="basicInfoPanel" native="true">
|
||||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||||
|
@ -83,7 +86,7 @@
|
||||||
<second>0</second>
|
<second>0</second>
|
||||||
<year>1999</year>
|
<year>1999</year>
|
||||||
<month>12</month>
|
<month>12</month>
|
||||||
<day>29</day>
|
<day>28</day>
|
||||||
</datetime>
|
</datetime>
|
||||||
</property>
|
</property>
|
||||||
<property name="currentSection">
|
<property name="currentSection">
|
||||||
|
@ -185,7 +188,7 @@
|
||||||
<enum>QLayout::SetMaximumSize</enum>
|
<enum>QLayout::SetMaximumSize</enum>
|
||||||
</property>
|
</property>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QLabel" name="label_4">
|
<widget class="QLabel" name="ingredientsPanelLabel">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Ingredients</string>
|
<string>Ingredients</string>
|
||||||
</property>
|
</property>
|
||||||
|
@ -198,7 +201,7 @@
|
||||||
<widget class="QWidget" name="ingredientsSubPanel" native="true">
|
<widget class="QWidget" name="ingredientsSubPanel" native="true">
|
||||||
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
||||||
<item alignment="Qt::AlignLeft">
|
<item alignment="Qt::AlignLeft">
|
||||||
<widget class="QListView" name="listView">
|
<widget class="QListView" name="ingredientsListView">
|
||||||
<property name="batchSize">
|
<property name="batchSize">
|
||||||
<number>100</number>
|
<number>100</number>
|
||||||
</property>
|
</property>
|
||||||
|
@ -303,7 +306,7 @@
|
||||||
<font>
|
<font>
|
||||||
<family>Noto Sans CJK KR</family>
|
<family>Noto Sans CJK KR</family>
|
||||||
<pointsize>14</pointsize>
|
<pointsize>14</pointsize>
|
||||||
<weight>3</weight>
|
<weight>50</weight>
|
||||||
<italic>false</italic>
|
<italic>false</italic>
|
||||||
<bold>false</bold>
|
<bold>false</bold>
|
||||||
</font>
|
</font>
|
||||||
|
@ -329,12 +332,12 @@
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
</layout>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item>
|
<item>
|
||||||
<widget class="QComboBox" name="unitComboBox"/>
|
<widget class="QComboBox" name="unitComboBox"/>
|
||||||
</item>
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QLineEdit" name="commentsLineEdit">
|
<widget class="QLineEdit" name="commentsLineEdit">
|
||||||
<property name="minimumSize">
|
<property name="minimumSize">
|
||||||
|
@ -373,6 +376,39 @@
|
||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QWidget" name="instructionsPanel" native="true">
|
||||||
|
<layout class="QVBoxLayout" name="verticalLayout_8">
|
||||||
|
<item>
|
||||||
|
<widget class="QLabel" name="instructionsLabel">
|
||||||
|
<property name="text">
|
||||||
|
<string>Instructions</string>
|
||||||
|
</property>
|
||||||
|
<property name="alignment">
|
||||||
|
<set>Qt::AlignCenter</set>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QToolButton" name="toolButton">
|
||||||
|
<property name="text">
|
||||||
|
<string>italics</string>
|
||||||
|
</property>
|
||||||
|
<property name="toolButtonStyle">
|
||||||
|
<enum>Qt::ToolButtonIconOnly</enum>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QTextEdit" name="instructionsTextEdit">
|
||||||
|
<property name="placeholderText">
|
||||||
|
<string>Enter instructions here.</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
|
14
main.cpp
14
main.cpp
|
@ -16,21 +16,21 @@ int main(int argc, char *argv[])
|
||||||
RecipeDatabase recipeDB("recipes");
|
RecipeDatabase recipeDB("recipes");
|
||||||
|
|
||||||
//TESTING CODE
|
//TESTING CODE
|
||||||
// vector<RecipeIngredient> ri;
|
vector<RecipeIngredient> ri;
|
||||||
// ri.push_back(RecipeIngredient("flour", "grains", 3.0f, UnitOfMeasure("cup", "cups", "c")));
|
ri.push_back(RecipeIngredient("flour", "grains", 3.0f, UnitOfMeasure("cup", "cups", "c", UnitOfMeasure::VOLUME, 1.0), ""));
|
||||||
// ri.push_back(RecipeIngredient("Baking Powder", "Additives", 1.0f, UnitOfMeasure("Teaspoon", "Teaspoons", "Tsp")));
|
ri.push_back(RecipeIngredient("Baking Powder", "Additives", 1.0f, UnitOfMeasure("Teaspoon", "Teaspoons", "Tsp", UnitOfMeasure::VOLUME, 1.0), ""));
|
||||||
|
|
||||||
// Recipe rec("Example", ri, Instruction("<b>BOLD</b><i>iTaLiCs</i>"), QImage(), vector<RecipeTag>(), QDate::currentDate(), QTime(0, 30), QTime(0, 25), 10.0f);
|
Recipe rec("Example", ri, Instruction("<b>BOLD</b><i>iTaLiCs</i>"), QImage(), vector<RecipeTag>(), QDate::currentDate(), QTime(0, 30), QTime(0, 25), 10.0f);
|
||||||
|
|
||||||
// bool success = recipeDB.storeRecipe(rec);
|
bool success = recipeDB.storeRecipe(rec);
|
||||||
// printf("Storage successful: %d\n", success);
|
printf("Storage successful: %d\n", success);
|
||||||
|
|
||||||
Recipe reloadRec = recipeDB.retrieveRecipe("Example");
|
Recipe reloadRec = recipeDB.retrieveRecipe("Example");
|
||||||
reloadRec.print();
|
reloadRec.print();
|
||||||
|
|
||||||
w.loadFromRecipe(reloadRec);
|
w.loadFromRecipe(reloadRec);
|
||||||
|
|
||||||
NewRecipeDialog d;
|
NewRecipeDialog d(&recipeDB);
|
||||||
d.show();
|
d.show();
|
||||||
d.exec();
|
d.exec();
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,7 @@ ResultTable Database::selectFrom(string tableName, string columnNames, string co
|
||||||
if (columnNames.size() == 0 || tableName.empty()){
|
if (columnNames.size() == 0 || tableName.empty()){
|
||||||
return ResultTable();
|
return ResultTable();
|
||||||
}
|
}
|
||||||
string query = "SELECT " + columnNames + " FROM " + tableName + " WHERE " + conditions + ";";
|
string query = "SELECT " + columnNames + " FROM " + tableName + " " + conditions + ";";
|
||||||
return this->executeSQL(query);
|
return this->executeSQL(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,8 +81,7 @@ bool Database::tableExists(string tableName){
|
||||||
if (tableName.empty() || this->db == NULL || !this->dbIsOpen){
|
if (tableName.empty() || this->db == NULL || !this->dbIsOpen){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
ResultTable t = this->selectFrom("sqlite_master", "name", "type='table' AND name='"+tableName+"'");
|
ResultTable t = this->selectFrom("sqlite_master", "name", "WHERE type='table' AND name='"+tableName+"'");
|
||||||
//ResultTable t = executeSQL("SELECT name FROM sqlite_master WHERE type='table' AND name='"+tableName+"';");
|
|
||||||
return !t.isEmpty();
|
return !t.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,9 +5,9 @@ RecipeDatabase::RecipeDatabase(string filename) : Database(filename){
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RecipeDatabase::storeRecipe(Recipe recipe){
|
bool RecipeDatabase::storeRecipe(Recipe recipe){
|
||||||
///TODO: Implement this in a smart way using transaction.
|
//Store a recipe, if it doesn't already exist. This first tries to create the recipe entry, then all subsequent supporting table entries.
|
||||||
this->executeSQL("BEGIN;");
|
this->executeSQL("BEGIN;");
|
||||||
ResultTable t = this->selectFrom("recipe", "*", "name="+surroundString(recipe.getName(), "'"));
|
ResultTable t = this->selectFrom("recipe", "*", "WHERE name="+surroundString(recipe.getName(), "'"));
|
||||||
if (!t.isEmpty()){
|
if (!t.isEmpty()){
|
||||||
fprintf(stderr, "Error storing recipe: Recipe with name %s already exists.\n", recipe.getName().c_str());
|
fprintf(stderr, "Error storing recipe: Recipe with name %s already exists.\n", recipe.getName().c_str());
|
||||||
} else {
|
} else {
|
||||||
|
@ -50,17 +50,11 @@ bool RecipeDatabase::storeRecipe(Recipe recipe){
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RecipeDatabase::storeRecipeIngredient(RecipeIngredient ri, int recipeId){
|
bool RecipeDatabase::storeRecipeIngredient(RecipeIngredient ri, int recipeId){
|
||||||
//First check if the base ingredient has been added to the database. This is done within storeIngredient().
|
int ingId = this->storeIngredient(ri);
|
||||||
ResultTable t = this->selectFrom("ingredient", "ingredientId", "name="+surroundString(ri.getName(), "'"));
|
if (ingId < 0) return false;
|
||||||
int ingId = 0;
|
|
||||||
if (t.isEmpty()){
|
if (!this->storeUnitOfMeasure(ri.getUnit())) return false;
|
||||||
if (!this->insertInto("ingredient", vector<string>({"foodGroup", "name"}), vector<string>({ri.getFoodGroup(), ri.getName()}))){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
ingId = this->getLastInsertedRowId();
|
|
||||||
} else {
|
|
||||||
ingId = std::stoi(t.valueAt(0, 0));
|
|
||||||
}
|
|
||||||
return this->insertInto("recipeIngredient",
|
return this->insertInto("recipeIngredient",
|
||||||
vector<string>({
|
vector<string>({
|
||||||
"ingredientId",
|
"ingredientId",
|
||||||
|
@ -78,11 +72,41 @@ bool RecipeDatabase::storeRecipeIngredient(RecipeIngredient ri, int recipeId){
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
void RecipeDatabase::storeIngredient(Ingredient ingredient){
|
int RecipeDatabase::storeIngredient(Ingredient ingredient){
|
||||||
ResultTable t = this->selectFrom("ingredient", "*", "name="+surroundString(ingredient.getName(), "'"));
|
ResultTable t = this->selectFrom("ingredient", "*", "WHERE name="+surroundString(ingredient.getName(), "'"));
|
||||||
if (t.isEmpty()){
|
if (t.isEmpty()){
|
||||||
this->insertInto("ingredient", vector<string>({"foodGroup", "name"}), vector<string>({ingredient.getFoodGroup(), ingredient.getName()}));
|
bool success = this->insertInto("ingredient", vector<string>({"foodGroup", "name"}), vector<string>({ingredient.getFoodGroup(), ingredient.getName()}));
|
||||||
|
if (success){
|
||||||
|
return this->getLastInsertedRowId();
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
return std::stoi(t.valueAt(0, 0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RecipeDatabase::storeUnitOfMeasure(UnitOfMeasure u){
|
||||||
|
ResultTable t = this->selectFrom("unitOfMeasure", "name", "WHERE name="+surroundString(u.getName(), "'"));
|
||||||
|
if (!t.isEmpty()){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool success = this->insertInto("unitOfMeasure",
|
||||||
|
vector<string>({
|
||||||
|
"name",
|
||||||
|
"plural",
|
||||||
|
"abbreviation",
|
||||||
|
"type",
|
||||||
|
"metricCoefficient"
|
||||||
|
}),
|
||||||
|
vector<string>({
|
||||||
|
u.getName(),
|
||||||
|
u.getNamePlural(),
|
||||||
|
u.getAbbreviation(),
|
||||||
|
std::to_string(u.getType()),
|
||||||
|
std::to_string(u.getMetricCoefficient())
|
||||||
|
}));
|
||||||
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RecipeDatabase::storeInstruction(Instruction instruction, int recipeId){
|
bool RecipeDatabase::storeInstruction(Instruction instruction, int recipeId){
|
||||||
|
@ -112,7 +136,7 @@ bool RecipeDatabase::storeTags(vector<RecipeTag> tags, int recipeId){
|
||||||
}
|
}
|
||||||
|
|
||||||
Recipe RecipeDatabase::retrieveRecipe(string name){
|
Recipe RecipeDatabase::retrieveRecipe(string name){
|
||||||
ResultTable t = this->selectFrom("recipe", "*", "name="+surroundString(name, "'"));
|
ResultTable t = this->selectFrom("recipe", "*", "WHERE name="+surroundString(name, "'"));
|
||||||
if (t.isEmpty()){
|
if (t.isEmpty()){
|
||||||
fprintf(stderr, "Error: No recipe with name %s found!\n", name.c_str());
|
fprintf(stderr, "Error: No recipe with name %s found!\n", name.c_str());
|
||||||
return Recipe();
|
return Recipe();
|
||||||
|
@ -131,19 +155,49 @@ Recipe RecipeDatabase::retrieveRecipe(string name){
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<RecipeIngredient> RecipeDatabase::retrieveRecipeIngredients(int recipeId){
|
vector<RecipeIngredient> RecipeDatabase::retrieveRecipeIngredients(int recipeId){
|
||||||
ResultTable t = this->executeSQL("SELECT ingredient.name, ingredient.foodGroup, recipeIngredient.quantity, recipeIngredient.unitName, recipeIngredient.comment "
|
ResultTable t = this->executeSQL("SELECT ingredient.name, ingredient.foodGroup, "//0, 1
|
||||||
|
"recipeIngredient.quantity, recipeIngredient.unitName, recipeIngredient.comment,"//2, 3, 4
|
||||||
|
"unitOfMeasure.name, unitOfMeasure.plural, unitOfMeasure.abbreviation, unitOfMeasure.type, unitOfMeasure.metricCoefficient "//5, 6, 7, 8, 9
|
||||||
"FROM ingredient "
|
"FROM ingredient "
|
||||||
"INNER JOIN recipeIngredient "
|
"INNER JOIN recipeIngredient "
|
||||||
"ON ingredient.ingredientId = recipeIngredient.ingredientId "
|
"ON ingredient.ingredientId = recipeIngredient.ingredientId "
|
||||||
"AND recipeIngredient.recipeId = "+std::to_string(recipeId)+";");
|
"INNER JOIN unitOfMeasure "
|
||||||
|
"ON recipeIngredient.unitName = unitOfMeasure.name "
|
||||||
|
"WHERE recipeIngredient.recipeId = "+std::to_string(recipeId)+";");
|
||||||
vector<RecipeIngredient> ings;
|
vector<RecipeIngredient> ings;
|
||||||
for (unsigned int row = 0; row < t.rowCount(); row++){
|
for (unsigned int row = 0; row < t.rowCount(); row++){
|
||||||
RecipeIngredient r(t.valueAt(row, 0), t.valueAt(row, 1), std::stof(t.valueAt(row, 2)), UnitOfMeasure(t.valueAt(row, 3)));
|
RecipeIngredient r(t.valueAt(row, 0),
|
||||||
|
t.valueAt(row, 1),
|
||||||
|
std::stof(t.valueAt(row, 2)),
|
||||||
|
UnitOfMeasure(t.valueAt(row, 5), t.valueAt(row, 6), t.valueAt(row, 7), std::stoi(t.valueAt(row, 8)), std::stod(t.valueAt(row, 9))),
|
||||||
|
t.valueAt(row, 4));
|
||||||
ings.push_back(r);
|
ings.push_back(r);
|
||||||
}
|
}
|
||||||
return ings;
|
return ings;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vector<Ingredient> RecipeDatabase::retrieveAllIngredients(){
|
||||||
|
ResultTable t = this->selectFrom("ingredient", "*", "ORDER BY name");
|
||||||
|
vector<Ingredient> ings;
|
||||||
|
for (unsigned int row = 0; row < t.rowCount(); row++){
|
||||||
|
Ingredient i(t.valueAt(row, 2), t.valueAt(row, 1));
|
||||||
|
ings.push_back(i);
|
||||||
|
}
|
||||||
|
return ings;
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<UnitOfMeasure> RecipeDatabase::retrieveAllUnitsOfMeasure(){
|
||||||
|
ResultTable t = this->selectFrom("unitOfMeasure", "*", "ORDER BY name");
|
||||||
|
vector<UnitOfMeasure> units;
|
||||||
|
if (!t.isEmpty()){
|
||||||
|
for (unsigned int row = 0; row < t.rowCount(); row++){
|
||||||
|
UnitOfMeasure u(t.valueAt(row, 0), t.valueAt(row, 1), t.valueAt(row, 2), std::stoi(t.valueAt(row, 3)), std::stod(t.valueAt(row, 4)));
|
||||||
|
units.push_back(u);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return units;
|
||||||
|
}
|
||||||
|
|
||||||
void RecipeDatabase::ensureTablesExist(){
|
void RecipeDatabase::ensureTablesExist(){
|
||||||
//Make sure that foreign keys are enabled.
|
//Make sure that foreign keys are enabled.
|
||||||
this->executeSQL("PRAGMA foreign_keys = ON;");
|
this->executeSQL("PRAGMA foreign_keys = ON;");
|
||||||
|
@ -154,6 +208,13 @@ void RecipeDatabase::ensureTablesExist(){
|
||||||
"ingredientId INTEGER PRIMARY KEY,"
|
"ingredientId INTEGER PRIMARY KEY,"
|
||||||
"foodGroup varchar,"
|
"foodGroup varchar,"
|
||||||
"name varchar UNIQUE);");
|
"name varchar UNIQUE);");
|
||||||
|
//Unit of Measure table.
|
||||||
|
this->executeSQL("CREATE TABLE IF NOT EXISTS unitOfMeasure("
|
||||||
|
"name varchar UNIQUE PRIMARY KEY,"
|
||||||
|
"plural varchar,"
|
||||||
|
"abbreviation varchar,"
|
||||||
|
"type int,"
|
||||||
|
"metricCoefficient real);");
|
||||||
//Recipe table. Each recipe can have at most one instruction, and one image.
|
//Recipe table. Each recipe can have at most one instruction, and one image.
|
||||||
this->executeSQL("CREATE TABLE IF NOT EXISTS recipe("
|
this->executeSQL("CREATE TABLE IF NOT EXISTS recipe("
|
||||||
"recipeId INTEGER PRIMARY KEY,"
|
"recipeId INTEGER PRIMARY KEY,"
|
||||||
|
@ -175,6 +236,7 @@ void RecipeDatabase::ensureTablesExist(){
|
||||||
"unitName varchar,"
|
"unitName varchar,"
|
||||||
"comment varchar,"
|
"comment varchar,"
|
||||||
"FOREIGN KEY (ingredientId) REFERENCES ingredient(ingredientId),"
|
"FOREIGN KEY (ingredientId) REFERENCES ingredient(ingredientId),"
|
||||||
"FOREIGN KEY (recipeId) REFERENCES recipe(recipeId));");
|
"FOREIGN KEY (recipeId) REFERENCES recipe(recipeId),"
|
||||||
|
"FOREIGN KEY (unitName) REFERENCES unitOfMeasure(name));");
|
||||||
this->executeSQL("COMMIT;");
|
this->executeSQL("COMMIT;");
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,14 +22,19 @@ class RecipeDatabase : public Database
|
||||||
bool storeRecipe(Recipe recipe);
|
bool storeRecipe(Recipe recipe);
|
||||||
|
|
||||||
//SQL Helper methods.
|
//SQL Helper methods.
|
||||||
|
//Storage.
|
||||||
bool storeRecipeIngredient(RecipeIngredient ri, int recipeId);
|
bool storeRecipeIngredient(RecipeIngredient ri, int recipeId);
|
||||||
void storeIngredient(Ingredient ingredient);
|
int storeIngredient(Ingredient ingredient);
|
||||||
|
bool storeUnitOfMeasure(UnitOfMeasure u);
|
||||||
bool storeInstruction(Instruction instruction, int recipeId);
|
bool storeInstruction(Instruction instruction, int recipeId);
|
||||||
bool storeImage(QImage image, int recipeId);
|
bool storeImage(QImage image, int recipeId);
|
||||||
bool storeTags(vector<RecipeTag> tags, int recipeId);
|
bool storeTags(vector<RecipeTag> tags, int recipeId);
|
||||||
|
|
||||||
|
//Retrieval.
|
||||||
Recipe retrieveRecipe(string name);
|
Recipe retrieveRecipe(string name);
|
||||||
vector<RecipeIngredient> retrieveRecipeIngredients(int recipeId);
|
vector<RecipeIngredient> retrieveRecipeIngredients(int recipeId);
|
||||||
|
vector<Ingredient> retrieveAllIngredients();
|
||||||
|
vector<UnitOfMeasure> retrieveAllUnitsOfMeasure();
|
||||||
private:
|
private:
|
||||||
|
|
||||||
//Utility methods.
|
//Utility methods.
|
||||||
|
|
|
@ -1,15 +1,17 @@
|
||||||
#include "model/recipe/ingredients/recipeingredient.h"
|
#include "model/recipe/ingredients/recipeingredient.h"
|
||||||
|
|
||||||
RecipeIngredient::RecipeIngredient(string name, string foodGroup, float quantity, UnitOfMeasure unit) : Ingredient(name, foodGroup){
|
RecipeIngredient::RecipeIngredient(string name, string foodGroup, float quantity, UnitOfMeasure unit, string comment) : Ingredient(name, foodGroup){
|
||||||
setQuantity(quantity);
|
setQuantity(quantity);
|
||||||
setUnit(unit);
|
setUnit(unit);
|
||||||
|
setComment(comment);
|
||||||
}
|
}
|
||||||
|
|
||||||
RecipeIngredient::RecipeIngredient(Ingredient i, float quantity, UnitOfMeasure unit){
|
RecipeIngredient::RecipeIngredient(Ingredient i, float quantity, UnitOfMeasure unit, string comment){
|
||||||
setName(i.getName());
|
setName(i.getName());
|
||||||
setFoodGroup(i.getFoodGroup());
|
setFoodGroup(i.getFoodGroup());
|
||||||
setQuantity(quantity);
|
setQuantity(quantity);
|
||||||
setUnit(unit);
|
setUnit(unit);
|
||||||
|
setComment(comment);
|
||||||
}
|
}
|
||||||
|
|
||||||
RecipeIngredient::RecipeIngredient(){
|
RecipeIngredient::RecipeIngredient(){
|
||||||
|
|
|
@ -16,9 +16,9 @@ class RecipeIngredient : public Ingredient
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
//Constructor for new RecipeIngredient without starting child ingredient.
|
//Constructor for new RecipeIngredient without starting child ingredient.
|
||||||
RecipeIngredient(string name, string foodGroup, float quantity, UnitOfMeasure unit);
|
RecipeIngredient(string name, string foodGroup, float quantity, UnitOfMeasure unit, string comment);
|
||||||
//Constructor using data from a child ingredient.
|
//Constructor using data from a child ingredient.
|
||||||
RecipeIngredient(Ingredient i, float quantity, UnitOfMeasure unit);
|
RecipeIngredient(Ingredient i, float quantity, UnitOfMeasure unit, string comment);
|
||||||
RecipeIngredient();
|
RecipeIngredient();
|
||||||
|
|
||||||
//Getters
|
//Getters
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
#include "unitofmeasure.h"
|
#include "unitofmeasure.h"
|
||||||
|
|
||||||
UnitOfMeasure::UnitOfMeasure(string name, string plural, string abbreviation, int type){
|
UnitOfMeasure::UnitOfMeasure(string name, string plural, string abbreviation, int type, double coef){
|
||||||
this->name = name;
|
this->name = name;
|
||||||
this->plural = plural;
|
this->plural = plural;
|
||||||
this->abbreviation = abbreviation;
|
this->abbreviation = abbreviation;
|
||||||
this->type = type;
|
this->type = type;
|
||||||
|
this->metricCoefficient = coef;
|
||||||
}
|
}
|
||||||
|
|
||||||
UnitOfMeasure::UnitOfMeasure(string name){
|
UnitOfMeasure::UnitOfMeasure(string name){
|
||||||
|
@ -12,10 +13,11 @@ UnitOfMeasure::UnitOfMeasure(string name){
|
||||||
this->plural = name + "s";
|
this->plural = name + "s";
|
||||||
this->abbreviation = "NULL";
|
this->abbreviation = "NULL";
|
||||||
this->type = MISC;
|
this->type = MISC;
|
||||||
|
this->metricCoefficient = 1;
|
||||||
///TODO: Make actual guessing of this stuff.
|
///TODO: Make actual guessing of this stuff.
|
||||||
}
|
}
|
||||||
|
|
||||||
UnitOfMeasure::UnitOfMeasure() : UnitOfMeasure::UnitOfMeasure("", "", "", MISC){
|
UnitOfMeasure::UnitOfMeasure() : UnitOfMeasure::UnitOfMeasure("", "", "", MISC, 1.0){
|
||||||
//Default constructor initializes all fields to empty strings.
|
//Default constructor initializes all fields to empty strings.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,3 +36,7 @@ string UnitOfMeasure::getAbbreviation() const{
|
||||||
int UnitOfMeasure::getType() const{
|
int UnitOfMeasure::getType() const{
|
||||||
return this->type;
|
return this->type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
double UnitOfMeasure::getMetricCoefficient() const{
|
||||||
|
return this->metricCoefficient;
|
||||||
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ public:
|
||||||
static const int MISC = 4;
|
static const int MISC = 4;
|
||||||
|
|
||||||
//Full constructor.
|
//Full constructor.
|
||||||
UnitOfMeasure(string name, string plural, string abbreviation, int type);
|
UnitOfMeasure(string name, string plural, string abbreviation, int type, double coef);
|
||||||
//Attempt to guess unit from just a string.
|
//Attempt to guess unit from just a string.
|
||||||
UnitOfMeasure(string name);
|
UnitOfMeasure(string name);
|
||||||
//Constructor with default values.
|
//Constructor with default values.
|
||||||
|
@ -30,6 +30,7 @@ public:
|
||||||
string getNamePlural() const;
|
string getNamePlural() const;
|
||||||
string getAbbreviation() const;
|
string getAbbreviation() const;
|
||||||
int getType() const;
|
int getType() const;
|
||||||
|
double getMetricCoefficient() const;
|
||||||
private:
|
private:
|
||||||
string name; //The name of the unit of measure.
|
string name; //The name of the unit of measure.
|
||||||
string plural; //The plural name.
|
string plural; //The plural name.
|
||||||
|
|
Loading…
Reference in New Issue