| 1 | package Web::App::Lib::EntityRecord; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Class::Easy; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 0 | ▶ | sub info { | ||||||||||||||||||||||||||
|
Subroutine
info: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 8 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 9 | 0 | my $app = shift; | |||||||||||||||||||||||||||
| 10 | 0 | my $params = shift; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 0 | my $entity_type = delete $params->{entity}; | |||||||||||||||||||||||||||
| 13 | # deprecated | ||||||||||||||||||||||||||||
| 14 | $entity_type = delete $params->{entity_type} | ||||||||||||||||||||||||||||
| 15 | 0 | ▶ | unless defined $entity_type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 0 | ▶ | critical "no entity type defined by controller param entity(_type)?" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | unless defined $entity_type; | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | my $entity = $app->project->entity ($entity_type); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | my @fieldset = (); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | if ($params->{fieldset}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 0 | my $method = "fieldset_$params->{fieldset}"; | |||||||||||||||||||||||||||
| 26 | 0 | ▶ | critical "can't use fieldset $params->{fieldset} because no ${entity}->$method method available" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | unless $entity->can ($method); | ||||||||||||||||||||||||||||
| 28 | 0 | push @fieldset, $entity->$method; | |||||||||||||||||||||||||||
| 29 | } else { | ||||||||||||||||||||||||||||
| 30 | 0 | my $method = "fieldset_default"; | |||||||||||||||||||||||||||
| 31 | 0 | ▶ | push @fieldset, $entity->$method | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | if $entity->can ($method); | ||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | my $filter = {}; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # allowed: filter.* => where, group_by, sort.(field|order) => sort_(field|order) | ||||||||||||||||||||||||||||
| 38 | # limit, offset | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | foreach my $k (%$params) { | |||||||||||||||||||||||||||
| 41 | 0 | ▶ | if ($k =~ /^filter\.(.*)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 0 | $filter->{$1} = $params->{$k}; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | my $record; | |||||||||||||||||||||||||||
| 47 | 0 | ▶ | if ($params->{fuzzy}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | $record = $entity->fetch ($filter, @fieldset); | |||||||||||||||||||||||||||
| 49 | } else { | ||||||||||||||||||||||||||||
| 50 | 0 | my $record_id = $params->{'filter.id'}; | |||||||||||||||||||||||||||
| 51 | 0 | $record = $entity->fetch_by_id ($record_id, @fieldset); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | return [$record || {}]; | ||||||||||||||||||||||||||
|
Condition0%0/2
$record || {}
MC/DC0%0/1
$record || {}
$record
Truth table0%0/2
A
A= $record
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | sub store { | ||||||||||||||||||||||||||
|
Subroutine
store: not called CC=14 SCAR=50.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 58 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 59 | 0 | my $app = shift; | |||||||||||||||||||||||||||
| 60 | 0 | my $params = shift; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | my $req = $app->request; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | my $record_id = $params->{entity_id}; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 67 | 0 | debug Dumper $req->params; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | my $entity = $app->project->entity ($params->{entity_type}); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | my $fields = $entity->fields; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | my @field_names = keys %$fields; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | my @required_params; | |||||||||||||||||||||||||||
| 76 | my @allowed_params; | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | foreach (@field_names) { | |||||||||||||||||||||||||||
| 79 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 80 | 0 | warn Dumper $fields->{$_}; | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | next if defined $fields->{$_}->{X_IS_PK}; # look: special processing | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if ($fields->{$_}->{nullable} == 0 and ! defined $fields->{$_}->{default}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$fields->{$_}{"nullable"} == 0 and not defined $fields->{$_}{"default"}
MC/DC0%0/2
$fields->{$_}{"nullable"} == 0 and not defined $fields->{$_}{"default"}
$fields->{$_}{"nullable"} == 0
not defined $fields->{$_}{"default"}
Truth table0%0/3
A and not(B)
A= $fields->{$_}{"nullable"} == 0B= not defined $fields->{$_}{"default"}
| |||||||||||||||||||||||||||||
| 84 | 0 | push @required_params, $_; | |||||||||||||||||||||||||||
| 85 | 0 | next; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | push @allowed_params, $_; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | my $record = {}; | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | $record->{$entity->_pk_} = $record_id | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $record_id and $record_id =~ /^\d+$/
MC/DC0%0/2
defined $record_id and $record_id =~ /^\d+$/
defined $record_id
$record_id =~ /^\d+$/
Truth table0%0/3
A and B
A= defined $record_idB= $record_id =~ /^\d+$/
| |||||||||||||||||||||||||||||
| 93 | if defined $record_id and $record_id =~ /^\d+$/; | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | foreach my $param (@required_params) { | |||||||||||||||||||||||||||
| 96 | 0 | my $param_value = $app->request->param ($param); | |||||||||||||||||||||||||||
| 97 | # TODO: send an error mesage | ||||||||||||||||||||||||||||
| 98 | 0 | ▶ | unless (defined $param_value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | $app->var->{msg} = "обязательный параметр $param не задан"; | |||||||||||||||||||||||||||
| 100 | 0 | return; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | $record->{$param} = $param_value; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | foreach my $param (@allowed_params) { | |||||||||||||||||||||||||||
| 107 | 0 | my $param_value = $app->request->param ($param); | |||||||||||||||||||||||||||
| 108 | 0 | ▶ | $record->{$param} = $param_value | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | if defined $param_value; | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # TODO: check for booleans | ||||||||||||||||||||||||||||
| 113 | #my @checkboxes = qw(week is_active); | ||||||||||||||||||||||||||||
| 114 | # | ||||||||||||||||||||||||||||
| 115 | #foreach my $param (@checkboxes) { | ||||||||||||||||||||||||||||
| 116 | # $news_item->{$param} = $app->request->param ($param) ? 1 : 0; | ||||||||||||||||||||||||||||
| 117 | #} | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | my $item = $entity->new ($record); | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | my $result; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | if ($record->{$entity->_pk_} and $record->{$entity->_pk_} =~ /^\d+$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$$record{$entity->_pk_} and $$record{$entity->_pk_} =~ /^\d+$/
MC/DC0%0/2
$$record{$entity->_pk_} and $$record{$entity->_pk_} =~ /^\d+$/
$$record{$entity->_pk_}
$$record{$entity->_pk_} =~ /^\d+$/
Truth table0%0/3
A and B
A= $$record{$entity->_pk_}B= $$record{$entity->_pk_} =~ /^\d+$/
| |||||||||||||||||||||||||||||
| 124 | 0 | $result = $item->update; | |||||||||||||||||||||||||||
| 125 | } else { | ||||||||||||||||||||||||||||
| 126 | 0 | $result = $item->create; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | if ($result) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | $app->var->{success} = 'ok'; | |||||||||||||||||||||||||||
| 131 | 0 | $app->var->{entity_id} = $item->id; | |||||||||||||||||||||||||||
| 132 | } else { | ||||||||||||||||||||||||||||
| 133 | 0 | debug Dumper $record; | |||||||||||||||||||||||||||
| 134 | 0 | $app->var->{msg} = "невозможно сохранить — обратитесь к разработчику"; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | return; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 1 | 1; | |||||||||||||||||||||||||||