summaryrefslogtreecommitdiff
path: root/mat
blob: 2a60aa8e5d18e0a07e0aad4c8a6a64e36b2d97f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/perl

use warnings;
use strict;

use DBI;
use DateTime;
use DateTime::Format::ISO8601;

my $db = DBI->connect("dbi:SQLite:recipe.db", "", "", {RaiseError => 1,
    AutoCommit => 1});

my @recipes;
my @ingredients;
my @contents;

sub read_recipe_db {
  # All recipes needs to be read to be able to pick random recipes easily
  # enough.
  my $all = $db->selectall_arrayref("SELECT * FROM recipes");
  foreach my $row (@$all) {
    my ($id) = @$row;
    $recipes[$id]->{'name'} = @$row[1];
    $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];
  }
  $all = $db->selectall_arrayref("SELECT * FROM contents");
  foreach my $row (@$all) {
    my ($id) = @$row;
    $contents[$id]->{'recipe_id'} = @$row[1];
    $contents[$id]->{'ingredient_id'} = @$row[2];
    $contents[$id]->{'quantity'} = @$row[2];
    $contents[$id]->{'unit'} = @$row[2];
  }
}

sub get_random_recipe {
  return int(rand(@recipes));
}

sub get_recipe_name ($) {
  my ( $id ) = @_;

  if ($recipes[$id]->{'name'}) {
    return $recipes[$id]->{'name'};
  } else {
    return "NULL";
  }
}

sub get_recipe_uri ($) {
  my ( $id ) = @_;

  if ($recipes[$id]->{'uri'}) {
    return $recipes[$id]->{'uri'};
  } else {
    return "NULL";
  }
}

my @schedule = (
  {
    day     => "Måndag",
  },
  {
    day     => "Tisdag",
  },
  {
    day     => "Onsdag",
  },
  {
    day     => "Torsdag",
  },
  {
    day     => "Fredag",
  }
);

sub cmd_setmeal {
  my ( $date, $recipe_id ) = @_;
  my $mealtype = "lunch";

  if ($date =~ /^Mon|Tue|Wed|Thu|Fri|Sat|Sun/) {
    my $wday;
    $wday = 1 if ($date =~ /^Mon/);
    $wday = 2 if ($date =~ /^Tue/);
    $wday = 3 if ($date =~ /^Wed/);
    $wday = 4 if ($date =~ /^Thu/);
    $wday = 5 if ($date =~ /^Fri/);
    $wday = 6 if ($date =~ /^Sat/);
    $wday = 7 if ($date =~ /^Sun/);

    my $dt = DateTime->now();
    while ($dt->wday() != $wday) {
      $dt->add(days => 1);
    }

    $date = $dt->ymd();
  }
  return unless $date =~ "^[0-9]{4}-[0-9]{2}-[0-9]{2}\$";
  
  # FIXME This is a potential race condition. In theory I should be able to do
  # rollback if there are more than one entry matching this select at the end
  # of the function. Even better: add a constraint to the db if that is
  # possible.
  my $sql = "SELECT * FROM plan WHERE date='$date' and mealtype='$mealtype';";
  return -1 if ($db->selectrow_array($sql));

  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;
}

sub cmd_randmeal {
  my ( $date ) = @_;
  my $recipe_id = get_random_recipe();

  return cmd_setmeal($date, $recipe_id);
}

sub cmd_showplan {
  my ( $date ) = @_;
  my $mealtype = "lunch";
  my $dt;

  if($date) {
    return -1 unless $date =~ "^[0-9]{4}-[0-9]{2}-[0-9]{2}\$";
    $dt = DateTime::Format::ISO8601->parse_datetime( $date );
  } else {
    $dt = DateTime->now();
  }

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

sub cmd_randweek {
  for (my $i = 0; $i < 5; $i++) {
    $schedule[$i]->{'recipe'} = get_random_recipe;
  }

  for (my $i = 0; $i < 5; $i++) {
    print $schedule[$i]->{'day'}.": ";
    print get_recipe_name($schedule[$i]->{'recipe'})."\n";
    my $contents = $db->selectall_arrayref("SELECT * FROM contents WHERE ".
        "recipe_id=".$schedule[$i]->{'recipe'});
    unless (@$contents) {
      print "OBSERVERA: Ingredienslista saknas för denna rätt!\n\n";
    }
  }
}

# MAIN PROGRAM ################################################################

read_recipe_db;

if ($ARGV[0]) {
  if ( $ARGV[0] eq "setmeal") {
    print "Command failed!\n" unless (cmd_setmeal($ARGV[1], $ARGV[2]) >= 0);
  }
  if ( $ARGV[0] eq "randmeal") {
    print "Command failed!\n" unless (cmd_randmeal($ARGV[1]) >= 0);
  }
  if ( $ARGV[0] eq "showplan") {
    print "Command failed!\n" unless (cmd_showplan($ARGV[1]) >= 0);
  }
} else {
  cmd_showplan;
}