line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Cookieville::Write; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Cookieville::Write - Controller for changing data in a result |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=cut |
8
|
|
|
|
|
|
|
|
9
|
4
|
|
|
4
|
|
19194
|
use Mojo::Base 'Mojolicious::Controller'; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
25
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 METHODS |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head2 delete |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
Delete data from the database. |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=cut |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub delete { |
20
|
8
|
|
|
8
|
1
|
16650
|
my $self = shift; |
21
|
8
|
|
|
|
|
16
|
my $rs = eval { $self->db->resultset($self->stash('source')) }; |
|
8
|
|
|
|
|
224
|
|
22
|
8
|
|
|
|
|
12973
|
my $row; |
23
|
|
|
|
|
|
|
|
24
|
8
|
100
|
|
|
|
52
|
unless ($rs) { |
25
|
3
|
|
|
|
|
20
|
return $self->render(json => {message => 'No source by that name.'}, status => 404); |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
5
|
|
|
|
|
35
|
$row = $rs->find($self->stash('id')); |
29
|
5
|
100
|
|
|
|
25214
|
$row->delete if $row; |
30
|
|
|
|
|
|
|
|
31
|
5
|
100
|
|
|
|
50638
|
$self->render(json => {n => $row ? 1 : 0}); |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head2 patch |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
Used to patch a record in the database. |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=cut |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub patch { |
41
|
10
|
|
|
10
|
1
|
23064
|
my $self = shift; |
42
|
10
|
|
|
|
|
39
|
my $source = $self->stash('source'); |
43
|
10
|
|
|
|
|
122
|
my $rs = eval { $self->db->resultset($source) }; |
|
10
|
|
|
|
|
295
|
|
44
|
10
|
|
|
|
|
16384
|
my $data = $self->req->json; |
45
|
10
|
|
|
|
|
4951
|
my $row; |
46
|
|
|
|
|
|
|
|
47
|
10
|
100
|
|
|
|
43
|
unless ($data) { |
48
|
1
|
|
|
|
|
40
|
return $self->render(json => {message => 'Invalid JSON body.'}, status => 400); |
49
|
|
|
|
|
|
|
} |
50
|
9
|
100
|
|
|
|
202
|
unless ($rs) { |
51
|
3
|
|
|
|
|
20
|
return $self->render(json => {message => 'No source by that name.'}, status => 404); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
6
|
|
|
|
|
23
|
$row = $rs->find($self->stash('id')); |
55
|
|
|
|
|
|
|
|
56
|
6
|
100
|
|
|
|
25277
|
unless ($row) { |
57
|
2
|
|
|
|
|
26
|
return $self->render(json => {message => qq(No such record in $source source.)}, status => 404); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
4
|
|
|
|
|
67
|
$row->set_column($_ => $data->{$_}) for keys %$data; |
61
|
4
|
|
|
|
|
744
|
$row->update; |
62
|
4
|
|
|
|
|
50459
|
$self->render(json => {data => {$row->get_columns}}); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head2 update_or_insert |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
Used to update or insert a record in the database. |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=cut |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub update_or_insert { |
72
|
9
|
|
|
9
|
1
|
17233
|
my $self = shift; |
73
|
9
|
|
|
|
|
33
|
my $source = $self->stash('source'); |
74
|
9
|
|
|
|
|
79
|
my $rs = eval { $self->db->resultset($source) }; |
|
9
|
|
|
|
|
270
|
|
75
|
9
|
|
|
|
|
14472
|
my $data = $self->req->json; |
76
|
9
|
|
|
|
|
5079
|
my ($in_storage, $row); |
77
|
|
|
|
|
|
|
|
78
|
9
|
100
|
|
|
|
39
|
unless ($data) { |
79
|
1
|
|
|
|
|
42
|
return $self->render(json => {message => 'Invalid JSON body.'}, status => 400); |
80
|
|
|
|
|
|
|
} |
81
|
8
|
100
|
|
|
|
33
|
unless ($rs) { |
82
|
3
|
|
|
|
|
19
|
return $self->render(json => {message => 'No source by that name.'}, status => 404); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
5
|
100
|
|
|
|
48
|
if ($self->_can_find($data, $rs->result_source)) { |
86
|
4
|
|
|
|
|
21
|
$row = $rs->find_or_new($data); |
87
|
4
|
|
|
|
|
23368
|
$in_storage = $row->in_storage; |
88
|
4
|
100
|
|
|
|
400
|
$row->in_storage ? $row->update($data) : $row->insert; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
else { |
91
|
1
|
|
|
|
|
5
|
$row = $rs->create($data); |
92
|
1
|
|
|
|
|
12159
|
$row->discard_changes; |
93
|
1
|
|
|
|
|
5883
|
$in_storage = 0; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
5
|
100
|
|
|
|
56665
|
$self->render(json => {inserted => $in_storage ? 0 : 1, data => {$row->get_columns},},); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub _can_find { |
100
|
5
|
|
|
5
|
|
140
|
my ($self, $data, $source) = @_; |
101
|
5
|
|
|
|
|
21
|
my %unique_constraints = $source->unique_constraints; |
102
|
|
|
|
|
|
|
|
103
|
5
|
|
|
|
|
57
|
for my $c (values %unique_constraints) { |
104
|
8
|
100
|
|
|
|
16
|
return 1 if @$c == grep { exists $data->{$_} } @$c; |
|
8
|
|
|
|
|
45
|
|
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
1
|
|
|
|
|
5
|
return 0; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 AUTHOR |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Jan Henning Thorsen - C |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=cut |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
1; |