summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcos <cos>2011-08-08 21:51:15 +0200
committercos <cos>2011-08-08 21:51:15 +0200
commitb98492e8f5c2259c3664b84be011c7ff5fafb5cf (patch)
tree1d1818e53fced598c614ad03af082a9ee420b7c9
parent5783e29a949e233957db576b5bd0431a79d94f16 (diff)
downloadmat-b98492e8f5c2259c3664b84be011c7ff5fafb5cf.zip
Added editrecipe command.
-rwxr-xr-xmat107
1 files changed, 99 insertions, 8 deletions
diff --git a/mat b/mat
index 1da3f6c..f5bcaa4 100755
--- a/mat
+++ b/mat
@@ -14,6 +14,17 @@ my @recipes;
my @ingredients;
my @contents;
+sub read_ingredients {
+ @ingredients = ();
+
+ my $all = $db->selectall_arrayref("SELECT * FROM ingredients");
+ foreach my $row (@$all) {
+ my ($id) = @$row;
+ $ingredients[$id]->{'id'} = @$row[0];
+ $ingredients[$id]->{'name'} = @$row[1];
+ }
+}
+
sub read_recipe_db {
# All recipes needs to be read to be able to pick random recipes easily
# enough.
@@ -24,14 +35,7 @@ sub read_recipe_db {
$recipes[$id]->{'uri'} = @$row[2];
}
- # This is not how to do the ingredient and content reading!
- # They should only be read for relevant recipes.
- $all = $db->selectall_arrayref("SELECT * FROM ingredients");
- foreach my $row (@$all) {
- my ($id) = @$row;
- $ingredients[$id]->{'id'} = @$row[1];
- $ingredients[$id]->{'name'} = @$row[2];
- }
+ read_ingredients;
$all = $db->selectall_arrayref("SELECT * FROM contents");
foreach my $row (@$all) {
my ($id) = @$row;
@@ -194,6 +198,90 @@ sub cmd_help() {
print "Help yet to be implemented\n";
}
+sub interactive_edit_recipe_ingredients {
+ # TODO It would be nice to be able to input ingredients in the format:
+ # <quantity> <unit> <ingredient>
+ my ( $recipe_id ) = @_;
+ my ( $ingredient, $quantity, $unit, $answer, $sql );
+
+ while (1) {
+ my @matched_ingredients;
+
+ print "Ingredient: ";
+ $ingredient = <STDIN>;
+ chomp $ingredient;
+ last if ($ingredient eq '');
+ for (@ingredients) {
+ if (grep (/$ingredient/, $_->{'name'})) {
+ print $_->{'name'}, "\n";
+ push @matched_ingredients, $_->{'id'};
+ }
+ }
+ if (length(@matched_ingredients == 0)) {
+ $sql = "INSERT INTO ingredients (name) VALUES ('$ingredient');";
+ print "$sql\n\n";
+ print "Add new ingredient? (y/n): ";
+ $answer = <STDIN>;
+ if ($answer eq "y\n") {
+ $db->do($sql);
+ read_ingredients;
+ }
+ } elsif (length(@matched_ingredients == 1)) {
+ my $ingredient_id = $matched_ingredients[0];
+ print "Amount: ";
+ $quantity = <STDIN>;
+ chomp $quantity;
+# my $default_unit = "SELECT unit FROM contents WHERE ingredient_id='"
+# .$ingredient_id."' LIMIT 1;"
+ print "Unit: ";
+ $unit = <STDIN>;
+ chomp $unit;
+
+ if (($quantity ne '') and ($unit ne '')) {
+ $sql = "INSERT INTO contents (recipe_id, ingredient_id, quantity, ".
+ "unit) VALUES ('$recipe_id', '$ingredient_id', '$quantity', ".
+ "'$unit')";
+ print "$sql\n\n";
+ print "Add row? (y/n): ";
+ $answer = <STDIN>;
+ $db->do($sql) if ($answer eq "y\n");
+ }
+ }
+ }
+ return 0;
+}
+
+sub cmd_editrecipe {
+ my ( $recipe_id ) = @_;
+ my ( $recipe_name, $recipe_uri, $answer, $sql );
+
+ return -1 unless($recipe_id and ($recipe_name = get_recipe_name($recipe_id)) ne 'NULL');
+ $recipe_uri = get_recipe_uri($recipe_id);
+ print "Recipe name: $recipe_name\n";
+ print "Recipe uri: $recipe_uri\n";
+ print "Change these? (y/n) ";
+ $answer = <STDIN>;
+ if ($answer eq "y\n") {
+ print "Recipe name: ";
+ $recipe_name = <STDIN>;
+ chomp $recipe_name;
+ print "E.g. http://stupid.domain.name/node/755, urn:isbn:9789127118348#102\n";
+ print "Recipe uri: ";
+ $recipe_uri = <STDIN>;
+ chomp $recipe_uri;
+
+ $sql = "UPDATE recipes SET name='$recipe_name', uri='$recipe_uri' WHERE id=$recipe_id;";
+ print "$sql\n\n";
+ print "Update database? (y/n): ";
+ $answer = <STDIN>;
+ if ($answer eq "y\n") {
+ $db->do($sql);
+ }
+ }
+
+ interactive_edit_recipe_ingredients($recipe_id);
+}
+
sub cmd_addrecipe {
# FIXME Make it possible to provide name and uri as command line arguments
# instead of solely interactive mode.
@@ -227,6 +315,9 @@ if ($ARGV[0]) {
if ( $ARGV[0] eq "addrecipe") {
print "Command failed!\n" unless (cmd_addrecipe() >= 0);
}
+ if ( $ARGV[0] eq "editrecipe") {
+ print "Command failed!\n" unless (cmd_editrecipe($ARGV[1]) >= 0);
+ }
if ( $ARGV[0] eq "setmeal") {
print "Command failed!\n" unless (cmd_setmeal($ARGV[1], $ARGV[2]) >= 0);
}