This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
/
DishIngredient.pm
120 lines (86 loc) Β· 2.97 KB
/
DishIngredient.pm
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
package Coocook::Schema::Result::DishIngredient;
use Moose;
use MooseX::MarkAsMethods autoclean => 1;
extends 'Coocook::Schema::Result';
__PACKAGE__->load_components(qw< +Coocook::Schema::Component::Result::Convertible >);
__PACKAGE__->table('dish_ingredients');
__PACKAGE__->add_columns(
id => { data_type => 'integer', is_auto_increment => 1 },
position => { data_type => 'integer', default_value => 1 },
dish_id => { data_type => 'integer' },
prepare => { data_type => 'boolean' },
article_id => { data_type => 'integer' },
unit_id => { data_type => 'integer' },
value => { data_type => 'real' },
comment => { data_type => 'text' },
item_id => { data_type => 'integer', is_nullable => 1 }, # from purchase list
);
__PACKAGE__->set_primary_key('id');
__PACKAGE__->position_column('position');
__PACKAGE__->grouping_column('dish_id');
__PACKAGE__->belongs_to( article => 'Coocook::Schema::Result::Article', 'article_id' );
__PACKAGE__->belongs_to( dish => 'Coocook::Schema::Result::Dish', 'dish_id' );
__PACKAGE__->belongs_to( unit => 'Coocook::Schema::Result::Unit', 'unit_id' );
__PACKAGE__->belongs_to(
article_unit => 'Coocook::Schema::Result::ArticleUnit',
{
'foreign.article_id' => 'self.article_id',
'foreign.unit_id' => 'self.unit_id',
}
);
__PACKAGE__->belongs_to(
item => 'Coocook::Schema::Result::Item',
'item_id', { on_delete => 'SET NULL' }
);
__PACKAGE__->meta->make_immutable;
sub assign_to_purchase_list {
my ( $self, $list ) = @_;
my $item;
$self->txn_do(
sub {
$item = $self->result_source->schema->resultset('Item')->add_or_create(
{
purchase_list_id => ref $list ? $list->id : $list, # TODO stricter interface?
article_id => $self->article_id,
unit_id => $self->unit_id,
value => $self->value,
}
);
$self->update( { item_id => $item->id } );
}
);
return $item;
}
=head2 update_on_purchase_list()
Returns boolish value indicating if there's an item that was updated
=cut
sub update_on_purchase_list {
my $self = shift;
$self->txn_do(
sub {
my $item = $self->item or return;
$item->update_from_ingredients;
}
) or return;
return 1;
}
=head2 remove_from_purchase_list()
Returns boolish value indicating if there's an item that was updated
=cut
sub remove_from_purchase_list {
my $self = shift;
$self->txn_do(
sub {
my $item = $self->item or return;
$self->update( { item_id => undef } );
if ( $item->ingredients->results_exist ) {
$item->update_from_ingredients;
}
else { # item belongs to no other ingredients
$item->delete;
}
}
) or return;
return 1;
}
1;