summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcos <cos>2011-08-08 18:31:24 +0200
committercos <cos>2011-08-08 18:31:24 +0200
commitc2ed5b9cd216c036e182bac00344faf100db9c47 (patch)
treef84c2d8d93243bd39c5d72d4f4405aa2a4f72237
parenta3268ac510cb7e323677e9072a491d9195bda1e2 (diff)
downloadmat-c2ed5b9cd216c036e182bac00344faf100db9c47.zip
Added a crued first version showplan command.
-rwxr-xr-xmat58
1 files changed, 51 insertions, 7 deletions
diff --git a/mat b/mat
index 49d4b2e..392d93b 100755
--- a/mat
+++ b/mat
@@ -5,6 +5,7 @@ use strict;
use DBI;
use DateTime;
+use DateTime::Format::ISO8601;
my $db = DBI->connect("dbi:SQLite:recipe.db", "", "", {RaiseError => 1,
AutoCommit => 1});
@@ -55,6 +56,16 @@ sub get_recipe_name ($) {
}
}
+sub get_recipe_uri ($) {
+ my ( $id ) = @_;
+
+ if ($recipes[$id]->{'uri'}) {
+ return $recipes[$id]->{'uri'};
+ } else {
+ return "NULL";
+ }
+}
+
my @schedule = (
{
day => "MÃ¥ndag",
@@ -104,23 +115,41 @@ sub cmd_setmeal {
my $sql = "SELECT * FROM plan WHERE date='$date' and mealtype='$mealtype';";
return -1 if ($db->selectrow_array($sql));
- return unless $recipe_id =~ "^[0-9]+\$";
+ return -1 unless $recipe_id =~ "^[0-9]+\$";
if (get_recipe_name($recipe_id) ne 'NULL') {
$sql = "INSERT INTO plan (date, mealtype, recipe_id) VALUES ('$date',
'$mealtype', $recipe_id);";
$db->do($sql);
}
+
+ return 0;
}
-# MAIN PROGRAM ################################################################
+sub cmd_showplan {
+ my $mealtype = "lunch";
+ my $dt;
-read_recipe_db;
+ if($ARGV[1]) {
+ my $date = $ARGV[1];
+ return -1 unless $date =~ "^[0-9]{4}-[0-9]{2}-[0-9]{2}\$";
+ $dt = DateTime::Format::ISO8601->parse_datetime( $date );
+ } else {
+ $dt = DateTime->now();
+ }
-if ($ARGV[0]) {
- if ( $ARGV[0] eq "setmeal") {
- print "Command failed!\n" unless (cmd_setmeal >= 0);
+ for (my $i = 0; $i < 7; $i++) {
+ my $sql = "SELECT recipe_id FROM plan WHERE date='".$dt->ymd().
+ "' and mealtype='$mealtype';";
+ my @ids = $db->selectrow_array($sql);
+ if ($ids[0]) {
+ print $dt->ymd(), " ", get_recipe_name($ids[0]), ", ",
+ get_recipe_uri($ids[0]), "\n";
+ }
+ $dt->add(days => 1);
}
-} else {
+}
+
+sub cmd_randweek {
for (my $i = 0; $i < 5; $i++) {
$schedule[$i]->{'recipe'} = get_random_recipe;
}
@@ -135,3 +164,18 @@ if ($ARGV[0]) {
}
}
}
+
+# MAIN PROGRAM ################################################################
+
+read_recipe_db;
+
+if ($ARGV[0]) {
+ if ( $ARGV[0] eq "setmeal") {
+ print "Command failed!\n" unless (cmd_setmeal >= 0);
+ }
+ if ( $ARGV[0] eq "showplan") {
+ print "Command failed!\n" unless (cmd_showplan >= 0);
+ }
+} else {
+ cmd_showplan;
+}