summaryrefslogtreecommitdiff
path: root/mat
blob: e1de21dc0c4c9883f3bd139627e08ed62a8dc7b7 (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
#!/usr/bin/perl

use warnings;
use strict;

use DBI;

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_recipie_name ($) {
  my ( $id ) = @_;

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

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

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

read_recipe_db;

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