| 1 | package CatalystX::CRUD::Controller::REST; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Data::Dump qw( dump ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | extends qw( Catalyst::Controller::REST CatalystX::CRUD ); | |||||||||||||||||||||||||||||||||||||||||
| 9 | } | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | our $VERSION = '0.005'; | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | __PACKAGE__->mk_accessors( | |||||||||||||||||||||||||||||||||||||||||
| 14 | qw( | ||||||||||||||||||||||||||||||||||||||||||
| 15 | model_adapter | ||||||||||||||||||||||||||||||||||||||||||
| 16 | model_name | ||||||||||||||||||||||||||||||||||||||||||
| 17 | model_meta | ||||||||||||||||||||||||||||||||||||||||||
| 18 | primary_key | ||||||||||||||||||||||||||||||||||||||||||
| 19 | naked_results | ||||||||||||||||||||||||||||||||||||||||||
| 20 | page_size | ||||||||||||||||||||||||||||||||||||||||||
| 21 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 3 | with 'CatalystX::CRUD::ControllerRole'; | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 3 | ▶ | use CatalystX::CRUD::Results; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | CatalystX::CRUD::Controller::REST - Catalyst::Controller::REST with CRUD | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | package MyApp::Controller::Foo; | ||||||||||||||||||||||||||||||||||||||||||
| 35 | use Moose; | ||||||||||||||||||||||||||||||||||||||||||
| 36 | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | BEGIN { extends 'CatalystX::CRUD::Controller::REST' } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | __PACKAGE__->config( | ||||||||||||||||||||||||||||||||||||||||||
| 41 | model_name => 'Foo', | ||||||||||||||||||||||||||||||||||||||||||
| 42 | primary_key => 'id', | ||||||||||||||||||||||||||||||||||||||||||
| 43 | page_size => 50, | ||||||||||||||||||||||||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 1; | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | # now you can manage Foo objects with URIs like: | ||||||||||||||||||||||||||||||||||||||||||
| 49 | # POST /foo -> create new Foo record | ||||||||||||||||||||||||||||||||||||||||||
| 50 | # GET /foo -> list all Foo records | ||||||||||||||||||||||||||||||||||||||||||
| 51 | # PUT /foo/<pk> -> create or update Foo record (idempotent) | ||||||||||||||||||||||||||||||||||||||||||
| 52 | # DELETE /foo/<pk> -> delete Foo record | ||||||||||||||||||||||||||||||||||||||||||
| 53 | # GET /foo/<pk> -> view Foo record | ||||||||||||||||||||||||||||||||||||||||||
| 54 | # GET /foo/<pk>/bar -> view Bar object(s) related to Foo | ||||||||||||||||||||||||||||||||||||||||||
| 55 | # POST /foo/<pk>/bar -> create Bar object related to Foo | ||||||||||||||||||||||||||||||||||||||||||
| 56 | # GET /foo/<pk>/bar/<pk2> -> view Bar with id <pk2> related to Foo with <pk> | ||||||||||||||||||||||||||||||||||||||||||
| 57 | # POST /foo/<pk>/bar/<pk2> -> create relationship between Foo <pk> and Bar <pk2> | ||||||||||||||||||||||||||||||||||||||||||
| 58 | # DELETE /foo/<pk>/bar/<pk2> -> sever Bar object relationship to Foo | ||||||||||||||||||||||||||||||||||||||||||
| 59 | # PUT /foo/<pk>/bar/<pk2> -> create/update Bar object related to Foo (idempotent) | ||||||||||||||||||||||||||||||||||||||||||
| 60 | # GET /foo/search -> search for Foo objects | ||||||||||||||||||||||||||||||||||||||||||
| 61 | # GET /foo/count -> search for Foo objects, returning count only | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | Subclass of Catalyst::Controller::REST for use with CatalystX::CRUD. | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | =head1 DISCLAIMERS | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | This module is B<not> to be confused with CatalystX::CRUD::REST. | ||||||||||||||||||||||||||||||||||||||||||
| 70 | This is not a drop-in replacement for existing CatalystX::CRUD::Controllers. | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | This module extends Catalyst::Controller::REST to work with the | ||||||||||||||||||||||||||||||||||||||||||
| 73 | CatalystX::CRUD::Controller API. It is designed for web services, | ||||||||||||||||||||||||||||||||||||||||||
| 74 | not managing CRUD actions via HTML forms. | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | This is B<not> a subclass of CatalystX::CRUD::Controller. | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | ############################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 85 | # Local actions | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | =head2 search_objects | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | Registers URL space for B<search>. | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 1 | ▶ | sub search_objects : Path('search') : Args(0) : ActionClass('REST') { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
search_objects: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 3 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | =head2 search_objects_GET | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | Query the model and return results. See do_search(). | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | ▶ | sub search_objects_GET { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
search_objects_GET: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 1 | my ( $self, $c ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 1 | $self->do_search($c); | |||||||||||||||||||||||||||||||||||||||||
| 105 | 1 | ▶ | if ( !blessed( $c->stash->{results} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | $self->status_bad_request( $c, | |||||||||||||||||||||||||||||||||||||||||
| 107 | message => 'Must provide search parameters' ); | ||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 1 | $self->status_ok( $c, entity => $c->stash->{results}->serialize ); | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | =head2 count_objects | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | Registers URL space for B<count>. | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 1 | ▶ | sub count_objects : Path('count') : Args(0) : ActionClass('REST') { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
count_objects: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 3 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | =head2 count_objects_GET | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | Like search_objects_GET() but does not set result values, only a total count. | ||||||||||||||||||||||||||||||||||||||||||
| 126 | Useful for AJAX-y types of situations where you want to query for a total | ||||||||||||||||||||||||||||||||||||||||||
| 127 | number of matches and create a pager but not actually retrieve any data. | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 1 | ▶ | sub count_objects_GET { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
count_objects_GET: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 1 | my ( $self, $c ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 1 | $c->stash( fetch_no_results => 1 ); # optimize a little | |||||||||||||||||||||||||||||||||||||||||
| 134 | 1 | $self->do_search($c); | |||||||||||||||||||||||||||||||||||||||||
| 135 | 1 | ▶ | if ( !blessed( $c->stash->{results} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | $self->status_bad_request( $c, | |||||||||||||||||||||||||||||||||||||||||
| 137 | message => 'Must provide search parameters' ); | ||||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||
| 139 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 140 | 1 | $self->status_ok( $c, entity => $c->stash->{results}->serialize ); | |||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | ############################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 145 | # REST actions | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | =head2 zero_args | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | Registers URL space for 0 path arguments. | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 3 | ▶ | sub zero_args : Path('') : Args(0) : ActionClass('REST') { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
zero_args: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 3 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | =head2 zero_args_GET( I<ctx> ) | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | GET /foo -> list objects of type foo. | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | Calls do_search(). | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | 3 | ▶ | sub zero_args_GET { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
zero_args_GET: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | 3 | my ( $self, $c ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 166 | 3 | $self->do_search($c); | |||||||||||||||||||||||||||||||||||||||||
| 167 | 3 | $self->status_ok( $c, entity => $c->stash->{results}->serialize ); | |||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | =head2 zero_args_POST( I<ctx> ) | ||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | POST /foo -> create object of type foo. | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | ▶ | sub zero_args_POST { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
zero_args_POST: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | my ( $self, $c ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | ▶ | unless ( $self->can_write($c) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | $self->status_forbidden( $c, message => 'Permission denied' ); | |||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | $c->stash( object => $self->do_model( $c, 'fetch' ) ); | |||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | ▶ | if ( my $obj = $self->save_object($c) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | my $pk = $self->make_primary_key_string($obj); | |||||||||||||||||||||||||||||||||||||||||
| 187 | $self->status_created( | ||||||||||||||||||||||||||||||||||||||||||
| 188 | $c, | ||||||||||||||||||||||||||||||||||||||||||
| 189 | location => $c->uri_for($pk), | ||||||||||||||||||||||||||||||||||||||||||
| 190 | entity => $c->stash->{object}->serialize | ||||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||
| 193 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | # TODO msg | ||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | $self->status_bad_request( $c, message => 'Failed to create' ); | |||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | =head2 one_arg | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | Registers URL space for 1 path argument. | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | 9 | ▶ | sub one_arg : Path('') : Args(1) : ActionClass('REST::ForBrowsers') { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
one_arg: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | 9 | my ( $self, $c, $id ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 208 | 9 | $self->fetch( $c, $id ); | |||||||||||||||||||||||||||||||||||||||||
| 209 | 3 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | =head2 one_arg_GET( I<ctx>, I<pk> ) | ||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | GET /foo/<pk> -> retrieve object for I<pk>. | ||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | 4 | ▶ | sub one_arg_GET { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
one_arg_GET: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | 4 | my ( $self, $c, $id ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 219 | 4 | ▶ | return if $c->stash->{fetch_failed}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | 4 | ▶ | return if $c->stash->{object}->is_new; # 404 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | 2 | $self->status_ok( $c, entity => $c->stash->{object}->serialize ); | |||||||||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | =head2 one_arg_PUT( I<ctx>, I<pk> ) | ||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | PUT /foo/<pk> -> create or update the object for I<pk>. | ||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | This method must be idempotent. POST is not. | ||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | 3 | ▶ | sub one_arg_PUT { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
one_arg_PUT: called CC=6 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | 3 | my ( $self, $c, $id ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 234 | 3 | ▶ | return if $c->stash->{fetch_failed}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | # remember if we're creating or updating | ||||||||||||||||||||||||||||||||||||||||||
| 237 | 3 | my $obj_is_new = $c->stash->{object}->is_new; | |||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | 3 | ▶ | unless ( $self->can_write($c) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | $self->status_forbidden( $c, message => 'Permission denied' ); | |||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | 3 | ▶ | if ( my $obj = $self->save_object($c) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 245 | 3 | ▶ | if ( !$obj_is_new ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | 1 | $self->status_ok( $c, entity => $obj->serialize ); | |||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||
| 248 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 249 | 2 | my $loc = $c->uri_for($id); | |||||||||||||||||||||||||||||||||||||||||
| 250 | 2 | ▶ | $c->log->debug("PUT location=$loc") if $c->debug; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 251 | 2 | $self->status_created( | |||||||||||||||||||||||||||||||||||||||||
| 252 | $c, | ||||||||||||||||||||||||||||||||||||||||||
| 253 | location => $loc, | ||||||||||||||||||||||||||||||||||||||||||
| 254 | entity => $obj->serialize, | ||||||||||||||||||||||||||||||||||||||||||
| 255 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||
| 258 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||
| 260 | # TODO msg | ||||||||||||||||||||||||||||||||||||||||||
| 261 | 0 | $self->status_bad_request( $c, message => 'Failed to update' ); | |||||||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | =head2 one_arg_DELETE( I<ctx>, I<pk> ) | ||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | DELETE /foo/<pk> -> delete the object for I<pk>. | ||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | 2 | ▶ | sub one_arg_DELETE { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
one_arg_DELETE: called CC=4 SCAR=16.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | 2 | my ( $self, $c, $id ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 273 | 2 | ▶ | return if $c->stash->{fetch_failed}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | 2 | ▶ | unless ( $self->can_write($c) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | $self->status_forbidden( $c, message => 'Permission denied' ); | |||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||
| 280 | 2 | ▶ | if ( $self->delete_object($c) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 281 | 2 | $self->status_no_content($c); | |||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||
| 283 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | # TODO msg | ||||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | $self->status_bad_request( $c, message => 'Failed to delete' ); | |||||||||||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | =head2 two_args | ||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | Registers URL space for 2 path arguments. | ||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | 1 | ▶ | sub two_args : Path('') : Args(2) : ActionClass('REST::ForBrowsers') { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
two_args: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 297 | 1 | my ( $self, $c, $id, $rel ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 298 | 1 | $c->stash( rel_name => $rel ); | |||||||||||||||||||||||||||||||||||||||||
| 299 | 1 | $self->fetch( $c, $id ); | |||||||||||||||||||||||||||||||||||||||||
| 300 | 3 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | =head2 two_args_GET( I<ctx>, I<pk>, I<rel> ) | ||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | GET /foo/<pk>/bar -> a list of objects of type bar related to foo. | ||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | 1 | ▶ | sub two_args_GET { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
two_args_GET: called CC=4 SCAR=15.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 309 | 1 | my ( $self, $c, $id, $rel ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 310 | 1 | ▶ | return if $c->stash->{fetch_failed}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | my $results | ||||||||||||||||||||||||||||||||||||||||||
| 312 | 1 | = $self->do_model( $c, 'iterator_related', $c->stash->{object}, $rel, | |||||||||||||||||||||||||||||||||||||||||
| 313 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 314 | 1 | ▶ | if ( $self->has_errors($c) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 315 | 1 | my $err = $c->error->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 316 | 1 | ▶ | if ( $err =~ m/^(unsupported relationship name: (\S+))/i ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 317 | 1 | $self->status_not_found( $c, message => $1 ); | |||||||||||||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||||||||||||
| 319 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 320 | 0 | $self->status_bad_request( $c, message => $err ); | |||||||||||||||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||||||||||||||||
| 322 | 1 | $c->clear_errors; | |||||||||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||||||||||
| 324 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | $self->status_ok( $c, entity => $results->serialize ); | |||||||||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | =head2 two_args_POST( I<ctx>, I<pk>, I<rel> ) | ||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||
| 331 | POST /foo/<pk>/bar -> create relationship between foo and bar. | ||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | B<TODO> This method calls a not-yet-implemented create_related() | ||||||||||||||||||||||||||||||||||||||||||
| 334 | action in the CXC::Model. | ||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | ▶ | sub two_args_POST { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
two_args_POST: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | my ( $self, $c, $id, $rel ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 340 | 0 | ▶ | return if $c->stash->{fetch_failed}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | ▶ | unless ( $self->can_write($c) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | $self->status_forbidden( $c, message => 'Permission denied' ); | |||||||||||||||||||||||||||||||||||||||||
| 343 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||
| 346 | 0 | ▶ | if ( !$self->model_can( $c, 'create_related' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 347 | 0 | $self->status_bad_request( $c, | |||||||||||||||||||||||||||||||||||||||||
| 348 | message => | ||||||||||||||||||||||||||||||||||||||||||
| 349 | 'This server does not yet implement the required method create_related' | ||||||||||||||||||||||||||||||||||||||||||
| 350 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 351 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||
| 354 | my $rel_obj | ||||||||||||||||||||||||||||||||||||||||||
| 355 | 0 | = $self->do_model( $c, 'create_related', $c->stash->{object}, $rel, ); | |||||||||||||||||||||||||||||||||||||||||
| 356 | 0 | ▶ | if ($rel_obj) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||
| 358 | # this controller doesn't know anything about the PK for $rel, | ||||||||||||||||||||||||||||||||||||||||||
| 359 | # so assume the object can give us a PK. | ||||||||||||||||||||||||||||||||||||||||||
| 360 | 0 | my $rel_id = $rel_obj->primary_key_uri_escaped; | |||||||||||||||||||||||||||||||||||||||||
| 361 | 0 | $self->status_created( | |||||||||||||||||||||||||||||||||||||||||
| 362 | $c, | ||||||||||||||||||||||||||||||||||||||||||
| 363 | location => | ||||||||||||||||||||||||||||||||||||||||||
| 364 | $c->uri_for( sprintf( "%s/%s/%s", $id, $rel, $rel_id ) ), | ||||||||||||||||||||||||||||||||||||||||||
| 365 | entity => $rel_obj->serialize | ||||||||||||||||||||||||||||||||||||||||||
| 366 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||||||||||||||||
| 368 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||
| 370 | # TODO msg | ||||||||||||||||||||||||||||||||||||||||||
| 371 | 0 | $self->status_bad_request( $c, message => 'Failed to create' ); | |||||||||||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||
| 376 | =head2 three_args | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | Registers the URL space for 3 path arguments. | ||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||
| 382 | 2 | ▶ | sub three_args : Path('') : Args(3) : ActionClass('REST::ForBrowsers') { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
three_args: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 383 | 2 | my ( $self, $c, $id, $rel, $rel_id ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 384 | 2 | $c->stash( rel_name => $rel ); | |||||||||||||||||||||||||||||||||||||||||
| 385 | 2 | $c->stash( foreign_pk_value => $rel_id ); | |||||||||||||||||||||||||||||||||||||||||
| 386 | 2 | $self->fetch( $c, $id ); | |||||||||||||||||||||||||||||||||||||||||
| 387 | 3 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||
| 389 | =head2 three_args_GET( I<ctx>, I<pk>, I<rel>, I<rel_id> ) | ||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||
| 391 | GET /foo/<pk>/<re>/<pk2> | ||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||
| 393 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||
| 395 | 0 | ▶ | sub three_args_GET { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
three_args_GET: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 396 | 0 | my ( $self, $c, $id, $rel, $rel_id ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 397 | 0 | ▶ | return if $c->stash->{fetch_failed}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 398 | my $result = $self->do_model( $c, 'find_related', $c->stash->{object}, | ||||||||||||||||||||||||||||||||||||||||||
| 399 | 0 | $rel, $rel_id, ); | |||||||||||||||||||||||||||||||||||||||||
| 400 | 0 | ▶ | if ( !$result or ( ref $result eq 'ARRAY' and !@$result ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
not $result or ref $result eq "ARRAY" and not @$result
ref $result eq "ARRAY" and not @$result
MC/DC0%0/3
not $result or ref $result eq "ARRAY" and not @$result
not $result
ref $result eq "ARRAY"
not @$result
Truth table0%0/4
not(A) or B and not(C)
A= not $resultB= ref $result eq "ARRAY"C= not @$result
| |||||||||||||||||||||||||||||||||||||||||||
| 401 | 0 | my $err_msg = sprintf( "No such %s with id '%s'", $rel, $rel_id ); | |||||||||||||||||||||||||||||||||||||||||
| 402 | 0 | $self->status_not_found( $c, message => $err_msg ); | |||||||||||||||||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||||||||||||||||
| 404 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||
| 406 | # coerce $result into an array ref for consistency | ||||||||||||||||||||||||||||||||||||||||||
| 407 | 0 | ▶ | if ( ref $result ne 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 408 | 0 | $result = [$result]; | |||||||||||||||||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||
| 411 | 0 | my @entity; | |||||||||||||||||||||||||||||||||||||||||
| 412 | 0 | for my $r (@$result) { | |||||||||||||||||||||||||||||||||||||||||
| 413 | 0 | push @entity, $r->serialize; | |||||||||||||||||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||||||||||||||||
| 415 | 0 | $self->status_ok( $c, entity => \@entity ); | |||||||||||||||||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||
| 419 | =head2 three_args_DELETE( I<ctx>, I<pk>, I<rel>, I<rel_pk> ) | ||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||
| 421 | DELETE /foo/<pk>/bar/<pk2> -> sever 'bar' object relationship to 'foo' | ||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||
| 423 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||
| 425 | 1 | ▶ | sub three_args_DELETE { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
three_args_DELETE: called CC=4 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 426 | 1 | my ( $self, $c, $id, $rel, $rel_id ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 427 | 1 | ▶ | return if $c->stash->{fetch_failed}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 428 | 1 | ▶ | unless ( $self->can_write($c) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 429 | 0 | $self->status_forbidden( $c, message => 'Permission denied' ); | |||||||||||||||||||||||||||||||||||||||||
| 430 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||
| 433 | my $rt = $self->do_model( $c, 'rm_related', $c->stash->{object}, | ||||||||||||||||||||||||||||||||||||||||||
| 434 | 1 | $rel, $rel_id, ); | |||||||||||||||||||||||||||||||||||||||||
| 435 | 1 | ▶ | if ($rt) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 436 | 1 | $self->status_no_content($c); | |||||||||||||||||||||||||||||||||||||||||
| 437 | } | ||||||||||||||||||||||||||||||||||||||||||
| 438 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||
| 440 | # TODO msg | ||||||||||||||||||||||||||||||||||||||||||
| 441 | 0 | $self->status_bad_request( $c, | |||||||||||||||||||||||||||||||||||||||||
| 442 | message => 'Failed to remove relationship' ); | ||||||||||||||||||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||
| 446 | =head2 three_args_POST( I<ctx>, I<pk>, I<rel>, I<rel_pk> ) | ||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||
| 448 | POST /foo/<pk>/bar/<pk2> -> create relationship between 'foo' and 'bar' | ||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||
| 450 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||||||||||||
| 452 | 0 | ▶ | sub three_args_POST { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
three_args_POST: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 453 | 0 | my ( $self, $c, $id, $rel, $rel_id ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 454 | 0 | ▶ | return if $c->stash->{fetch_failed}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 455 | my $rt = $self->do_model( $c, 'add_related', $c->stash->{object}, | ||||||||||||||||||||||||||||||||||||||||||
| 456 | 0 | $rel, $rel_id, ); | |||||||||||||||||||||||||||||||||||||||||
| 457 | 0 | ▶ | if ($rt) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 458 | 0 | $self->status_no_content($c); | |||||||||||||||||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||||||||||||||||
| 460 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||
| 462 | # TODO msg | ||||||||||||||||||||||||||||||||||||||||||
| 463 | 0 | $self->status_bad_request( $c, | |||||||||||||||||||||||||||||||||||||||||
| 464 | message => 'Failed to create relationship' ); | ||||||||||||||||||||||||||||||||||||||||||
| 465 | } | ||||||||||||||||||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||
| 468 | =head2 three_args_PUT( I<ctx>, I<pk>, I<rel>, I<rel_pk> ) | ||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||
| 470 | PUT /foo/<pk>/bar/<pk2> -> create/update 'bar' object related to 'foo' | ||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||
| 472 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||||
| 474 | 1 | ▶ | sub three_args_PUT { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
three_args_PUT: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 475 | 1 | my ( $self, $c, $id, $rel, $rel_id ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 476 | 1 | ▶ | return if $c->stash->{fetch_failed}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 477 | my $rt = $self->do_model( $c, 'put_related', $c->stash->{object}, | ||||||||||||||||||||||||||||||||||||||||||
| 478 | 1 | $rel, $rel_id, ); | |||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||
| 480 | 1 | ▶ | if ($rt) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 481 | 1 | $self->status_no_content($c); | |||||||||||||||||||||||||||||||||||||||||
| 482 | } | ||||||||||||||||||||||||||||||||||||||||||
| 483 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||
| 485 | # TODO msg | ||||||||||||||||||||||||||||||||||||||||||
| 486 | 0 | $self->status_bad_request( $c, | |||||||||||||||||||||||||||||||||||||||||
| 487 | message => 'Failed to PUT relationship' ); | ||||||||||||||||||||||||||||||||||||||||||
| 488 | } | ||||||||||||||||||||||||||||||||||||||||||
| 489 | } | ||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||
| 491 | ########################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 492 | # CRUD methods | ||||||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||||||
| 494 | =head2 save_object( I<ctx> ) | ||||||||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||||||||
| 496 | Calls can_write(), inflate_object(), precommit(), create_or_update_object() | ||||||||||||||||||||||||||||||||||||||||||
| 497 | and postcommit(). | ||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||
| 499 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||
| 501 | 3 | ▶ | sub save_object { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
save_object: called CC=5 SCAR=20.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 502 | 3 | my ( $self, $c ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 503 | 3 | ▶ | unless ( $self->can_write($c) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 504 | 0 | $self->status_forbidden( $c, message => 'Permission denied' ); | |||||||||||||||||||||||||||||||||||||||||
| 505 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||||||||
| 508 | # get a valid object | ||||||||||||||||||||||||||||||||||||||||||
| 509 | 3 | my $obj = $self->inflate_object($c); | |||||||||||||||||||||||||||||||||||||||||
| 510 | 3 | ▶ | if ( !$obj ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 511 | 0 | ▶ | $c->log->debug("inflate_object() returned false") if $c->debug; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 512 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||||||
| 515 | # write our changes | ||||||||||||||||||||||||||||||||||||||||||
| 516 | 3 | ▶ | unless ( $self->precommit( $c, $obj ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 517 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 518 | } | ||||||||||||||||||||||||||||||||||||||||||
| 519 | 3 | $self->create_or_update_object( $c, $obj ); | |||||||||||||||||||||||||||||||||||||||||
| 520 | 3 | $self->postcommit( $c, $obj ); | |||||||||||||||||||||||||||||||||||||||||
| 521 | 3 | return $obj; | |||||||||||||||||||||||||||||||||||||||||
| 522 | } | ||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||
| 524 | =head2 create_or_update_object( I<ctx>, I<object> ) | ||||||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||||||
| 526 | Calls the update() or create() method on the I<object> (or model_adapter()), | ||||||||||||||||||||||||||||||||||||||||||
| 527 | picking the method based on whether C<object_id> in stash() | ||||||||||||||||||||||||||||||||||||||||||
| 528 | evaluates true (update) or false (create). | ||||||||||||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||||||||||||
| 530 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||||||||
| 532 | 3 | ▶ | sub create_or_update_object { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create_or_update_object: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 533 | 3 | my ( $self, $c, $obj ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 534 | 3 | ▶ | my $method = $obj->is_new ? 'create' : 'update'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 535 | 3 | ▶ | $c->log->debug("object->$method") if $c->debug; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 536 | 3 | ▶ | if ( $self->model_adapter ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 537 | 0 | $self->model_adapter->$method( $c, $obj ); | |||||||||||||||||||||||||||||||||||||||||
| 538 | } | ||||||||||||||||||||||||||||||||||||||||||
| 539 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 540 | 3 | $obj->$method; | |||||||||||||||||||||||||||||||||||||||||
| 541 | } | ||||||||||||||||||||||||||||||||||||||||||
| 542 | } | ||||||||||||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||||||||||||
| 544 | =head2 delete_object( I<ctx> ) | ||||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||
| 546 | Checks can_write(), precommit(), and if both true, | ||||||||||||||||||||||||||||||||||||||||||
| 547 | calls the delete() method on the B<object> in the stash(). | ||||||||||||||||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||||||||||||||||
| 549 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||||||||||||||||
| 551 | 2 | ▶ | sub delete_object { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete_object: called CC=4 SCAR=16.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 552 | 2 | my ( $self, $c ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 553 | 2 | ▶ | unless ( $self->can_write($c) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 554 | 0 | $self->status_forbidden( $c, message => 'Permission denied' ); | |||||||||||||||||||||||||||||||||||||||||
| 555 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 556 | } | ||||||||||||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||||||||||||
| 558 | 2 | my $o = $c->stash->{object}; | |||||||||||||||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||||||||||||||||
| 560 | 2 | ▶ | unless ( $self->precommit( $c, $o ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 561 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 562 | } | ||||||||||||||||||||||||||||||||||||||||||
| 563 | 2 | ▶ | if ( $self->model_adapter ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 564 | 0 | $self->model_adapter->delete( $c, $o ); | |||||||||||||||||||||||||||||||||||||||||
| 565 | } | ||||||||||||||||||||||||||||||||||||||||||
| 566 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 567 | 2 | $o->delete; | |||||||||||||||||||||||||||||||||||||||||
| 568 | } | ||||||||||||||||||||||||||||||||||||||||||
| 569 | 2 | $self->postcommit( $c, $o ); | |||||||||||||||||||||||||||||||||||||||||
| 570 | 2 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 571 | } | ||||||||||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||||||||||
| 573 | =head2 inflate_object( I<ctx> ) | ||||||||||||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||||||||||||
| 575 | Returns the object from stash() initialized with the request data. | ||||||||||||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||||||||||||
| 577 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||||||||||||||||
| 579 | 3 | ▶ | sub inflate_object { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
inflate_object: called CC=5 SCAR=17.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 580 | 3 | my ( $self, $c ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 581 | 3 | my $object = $c->stash->{object}; | |||||||||||||||||||||||||||||||||||||||||
| 582 | 3 | ▶ | if ( !$object ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 583 | 0 | $self->throw_error("object not set in stash"); | |||||||||||||||||||||||||||||||||||||||||
| 584 | } | ||||||||||||||||||||||||||||||||||||||||||
| 585 | 3 | my $req_data = $c->req->data; | |||||||||||||||||||||||||||||||||||||||||
| 586 | 3 | ▶ | if ( !$req_data ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 587 | 0 | $self->status_bad_request( $c, message => 'Missing request data' ); | |||||||||||||||||||||||||||||||||||||||||
| 588 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 589 | } | ||||||||||||||||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||||||||||||||||
| 591 | # TODO other sanity checks? | ||||||||||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||||||||||
| 593 | 3 | for my $f ( keys %$req_data ) { | |||||||||||||||||||||||||||||||||||||||||
| 594 | 3 | ▶ | if ( $object->can($f) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 595 | 3 | $object->$f( $req_data->{$f} ); | |||||||||||||||||||||||||||||||||||||||||
| 596 | } | ||||||||||||||||||||||||||||||||||||||||||
| 597 | } | ||||||||||||||||||||||||||||||||||||||||||
| 598 | 3 | return $object; | |||||||||||||||||||||||||||||||||||||||||
| 599 | } | ||||||||||||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||||||||||||
| 601 | =head2 can_read( I<ctx> ) | ||||||||||||||||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||||||||||||||||
| 603 | Returns true if the current request is authorized to read() the C<object> in | ||||||||||||||||||||||||||||||||||||||||||
| 604 | stash(). | ||||||||||||||||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||||||||||||||||
| 606 | Default is true. | ||||||||||||||||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||||||||||||||||
| 608 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||||||||||||||||
| 610 | 12 | ▶ | sub can_read {1} | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
can_read: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||||||||||||||||
| 612 | =head2 can_write( I<ctx> ) | ||||||||||||||||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||||||||||||||||
| 614 | Returns true if the current request is authorized to create() or update() | ||||||||||||||||||||||||||||||||||||||||||
| 615 | the C<object> in stash(). | ||||||||||||||||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||||||||||||||||
| 617 | Default is true. | ||||||||||||||||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||||||||||||||||
| 619 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||||||||||||||||
| 621 | 11 | ▶ | sub can_write {1} | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
can_write: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||||||||||||||||
| 623 | =head2 precommit( I<ctx>, I<object> ) | ||||||||||||||||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||||||||||||||||
| 625 | Called by save_object(). If precommit() returns a false value, save_object() is aborted. | ||||||||||||||||||||||||||||||||||||||||||
| 626 | If precommit() returns a true value, create_or_update_object() gets called. | ||||||||||||||||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||||||||||||||||
| 628 | The default return is true. | ||||||||||||||||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||||||||||||||||
| 630 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||||||||||||||||
| 632 | 5 | ▶ | sub precommit {1} | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
precommit: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||||||||||||||||
| 634 | =head2 postcommit( I<cxt>, I<obj> ) | ||||||||||||||||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||||||||||||||||
| 636 | Called internally inside save_object(). Our default just returns true. | ||||||||||||||||||||||||||||||||||||||||||
| 637 | Override this method to post-process a successful save_object() action. | ||||||||||||||||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||||||||||||||||
| 639 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||||||||||||||||
| 641 | 5 | ▶ | sub postcommit {1} | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
postcommit: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||||||||||||||||
| 643 | =head2 fetch( I<ctx>, I<pk> ) | ||||||||||||||||||||||||||||||||||||||||||
| 644 | |||||||||||||||||||||||||||||||||||||||||||
| 645 | Determines the correct value and field name for I<pk> | ||||||||||||||||||||||||||||||||||||||||||
| 646 | and calls the do_model() method for C<fetch>. | ||||||||||||||||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||||||||||||||||
| 648 | On success, the B<object> key will be set in stash(). | ||||||||||||||||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||||||||||||||||
| 650 | On failure, calls status_not_found() and sets the | ||||||||||||||||||||||||||||||||||||||||||
| 651 | B<fetch_failed> stash() key. | ||||||||||||||||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||||||||||||||||
| 653 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||||||||||||||||
| 655 | 12 | ▶ | sub fetch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fetch: called CC=14 SCAR=32.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 656 | 12 | my ( $self, $c, $id ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 657 | |||||||||||||||||||||||||||||||||||||||||||
| 658 | 12 | ▶ | unless ( $self->can_read($c) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 659 | 0 | $self->status_forbidden( $c, message => 'Permission denied' ); | |||||||||||||||||||||||||||||||||||||||||
| 660 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 661 | } | ||||||||||||||||||||||||||||||||||||||||||
| 662 | |||||||||||||||||||||||||||||||||||||||||||
| 663 | 12 | $c->stash->{object_id} = $id; | |||||||||||||||||||||||||||||||||||||||||
| 664 | 12 | my @pk = $self->get_primary_key( $c, $id ); | |||||||||||||||||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||||||||||||||||
| 666 | # make sure all elements of the @pk pairs are not-null | ||||||||||||||||||||||||||||||||||||||||||
| 667 | 12 | ▶ | if ( scalar(@pk) % 2 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 668 | 0 | $self->throw_error( | |||||||||||||||||||||||||||||||||||||||||
| 669 | "Odd number of elements returned from get_primary_key()"); | ||||||||||||||||||||||||||||||||||||||||||
| 670 | } | ||||||||||||||||||||||||||||||||||||||||||
| 671 | 12 | my %pk_pairs = @pk; | |||||||||||||||||||||||||||||||||||||||||
| 672 | 12 | my $pk_is_null; | |||||||||||||||||||||||||||||||||||||||||
| 673 | 12 | for my $key ( keys %pk_pairs ) { | |||||||||||||||||||||||||||||||||||||||||
| 674 | 12 | my $val = $pk_pairs{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 675 | 12 | ▶ | if ( !defined($val) or !length($val) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $val and length $val
MC/DC0%0/2
defined $val and length $val
defined $val
length $val
Truth table33%1/3
A and B
A= defined $valB= length $val
| |||||||||||||||||||||||||||||||||||||||||||
| 676 | 0 | $pk_is_null = $key; | |||||||||||||||||||||||||||||||||||||||||
| 677 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 678 | } | ||||||||||||||||||||||||||||||||||||||||||
| 679 | } | ||||||||||||||||||||||||||||||||||||||||||
| 680 | 12 | ▶ | if ( $c->debug and defined $pk_is_null ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$c->debug and defined $pk_is_null
MC/DC0%0/2
$c->debug and defined $pk_is_null
$c->debug
defined $pk_is_null
Truth table33%1/3
A and B
A= $c->debugB= defined $pk_is_null
| |||||||||||||||||||||||||||||||||||||||||||
| 681 | 0 | $c->log->debug("Null PK value for '$pk_is_null'"); | |||||||||||||||||||||||||||||||||||||||||
| 682 | } | ||||||||||||||||||||||||||||||||||||||||||
| 683 | 12 | ▶ | my @arg = ( defined $pk_is_null || !$id ) ? () : (@pk); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $pk_is_null or not $id
MC/DC0%0/2
defined $pk_is_null or not $id
defined $pk_is_null
not $id
Truth table33%1/3
A or not(B)
A= defined $pk_is_nullB= not $id
| |||||||||||||||||||||||||||||||||||||||||||
| 684 | 12 | ▶ | $c->log->debug( "fetch: " . dump \@arg ) if $c->debug; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 685 | 12 | $c->stash->{object} = $self->do_model( $c, 'fetch', @arg ); | |||||||||||||||||||||||||||||||||||||||||
| 686 | 12 | ▶ | if ( !$c->stash->{object} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 687 | 4 | my $err_msg | |||||||||||||||||||||||||||||||||||||||||
| 688 | = sprintf( "No such %s with id '%s'", $self->model_name, $id ); | ||||||||||||||||||||||||||||||||||||||||||
| 689 | 4 | $self->status_not_found( $c, message => $err_msg ); | |||||||||||||||||||||||||||||||||||||||||
| 690 | 4 | $c->log->error($err_msg); | |||||||||||||||||||||||||||||||||||||||||
| 691 | 4 | $c->stash( fetch_failed => 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 692 | 4 | $c->clear_errors; | |||||||||||||||||||||||||||||||||||||||||
| 693 | 4 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 694 | } | ||||||||||||||||||||||||||||||||||||||||||
| 695 | 8 | ▶ | if ( $self->has_errors($c) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 696 | 0 | ▶ | $c->log->debug("errors in fetch") if $c->debug; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 697 | } | ||||||||||||||||||||||||||||||||||||||||||
| 698 | 8 | return $c->stash->{object}; | |||||||||||||||||||||||||||||||||||||||||
| 699 | } | ||||||||||||||||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||||||||||||||||
| 701 | =head2 do_search( I<ctx>, I<arg> ) | ||||||||||||||||||||||||||||||||||||||||||
| 702 | |||||||||||||||||||||||||||||||||||||||||||
| 703 | Prepare and execute a search. Called internally by list() | ||||||||||||||||||||||||||||||||||||||||||
| 704 | and search(). | ||||||||||||||||||||||||||||||||||||||||||
| 705 | |||||||||||||||||||||||||||||||||||||||||||
| 706 | Results are saved in stash() under the C<results> key. | ||||||||||||||||||||||||||||||||||||||||||
| 707 | |||||||||||||||||||||||||||||||||||||||||||
| 708 | If B<naked_results> is true, then results are set just as they are | ||||||||||||||||||||||||||||||||||||||||||
| 709 | returned from search() or list() (directly from the Model). | ||||||||||||||||||||||||||||||||||||||||||
| 710 | |||||||||||||||||||||||||||||||||||||||||||
| 711 | If B<naked_results> is false (default), then results is a | ||||||||||||||||||||||||||||||||||||||||||
| 712 | CatalystX::CRUD::Results object. | ||||||||||||||||||||||||||||||||||||||||||
| 713 | |||||||||||||||||||||||||||||||||||||||||||
| 714 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 715 | |||||||||||||||||||||||||||||||||||||||||||
| 716 | 5 | ▶ | sub do_search { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_search: called CC=11 SCAR=26.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 717 | 5 | my ( $self, $c, @arg ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 718 | |||||||||||||||||||||||||||||||||||||||||||
| 719 | # if we have no input, just return for initial search | ||||||||||||||||||||||||||||||||||||||||||
| 720 | 5 | ▶ | if ( !@arg && !$c->req->param && $c->action->name eq 'search' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
not @arg || $c->req->param and $c->action->name eq "search"
@arg || $c->req->param
MC/DC0%0/3
not @arg || $c->req->param and $c->action->name eq "search"
@arg
$c->req->param
$c->action->name eq "search"
Truth table50%2/4
not(A || B) and C
A= @argB= $c->req->paramC= $c->action->name eq "search"
| |||||||||||||||||||||||||||||||||||||||||||
| 721 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 722 | } | ||||||||||||||||||||||||||||||||||||||||||
| 723 | |||||||||||||||||||||||||||||||||||||||||||
| 724 | 5 | my $query; | |||||||||||||||||||||||||||||||||||||||||
| 725 | 5 | ▶ | if ( $self->can('make_query') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 726 | 0 | $query = $self->make_query( $c, @arg ); | |||||||||||||||||||||||||||||||||||||||||
| 727 | } | ||||||||||||||||||||||||||||||||||||||||||
| 728 | elsif ( $self->model_can( $c, 'make_query' ) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 729 | 5 | $query = $self->do_model( $c, 'make_query', @arg ); | |||||||||||||||||||||||||||||||||||||||||
| 730 | } | ||||||||||||||||||||||||||||||||||||||||||
| 731 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 732 | 0 | $self->throw_error( | |||||||||||||||||||||||||||||||||||||||||
| 733 | "neither controller nor model implement a make_query() method"); | ||||||||||||||||||||||||||||||||||||||||||
| 734 | } | ||||||||||||||||||||||||||||||||||||||||||
| 735 | 5 | ▶ | my $count = $self->do_model( $c, 'count', $query ) || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->do_model($c, 'count', $query) || 0
MC/DC100%1/1
$self->do_model($c, 'count', $query) || 0
$self->do_model($c, 'count', $query)
Truth table100%2/2
A
A= $self->do_model($c, 'count', $query)
| |||||||||||||||||||||||||||||||||||||||||||
| 736 | 5 | my $results; | |||||||||||||||||||||||||||||||||||||||||
| 737 | 5 | ▶ | unless ( $c->stash->{fetch_no_results} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 738 | 4 | $results = $self->do_model( $c, 'search', $query ); | |||||||||||||||||||||||||||||||||||||||||
| 739 | } | ||||||||||||||||||||||||||||||||||||||||||
| 740 | |||||||||||||||||||||||||||||||||||||||||||
| 741 | 5 | my $pager; | |||||||||||||||||||||||||||||||||||||||||
| 742 | 5 | ▶ | if ( $count && $self->model_can( $c, 'make_pager' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$count and $self->model_can($c, "make_pager")
MC/DC0%0/2
$count and $self->model_can($c, "make_pager")
$count
$self->model_can($c, "make_pager")
Truth table66%2/3
A and B
A= $countB= $self->model_can($c, "make_pager")
| |||||||||||||||||||||||||||||||||||||||||||
| 743 | 0 | $pager = $self->do_model( $c, 'make_pager', $count, $results ); | |||||||||||||||||||||||||||||||||||||||||
| 744 | } | ||||||||||||||||||||||||||||||||||||||||||
| 745 | |||||||||||||||||||||||||||||||||||||||||||
| 746 | 5 | ▶ | my $r | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 747 | = $self->naked_results | ||||||||||||||||||||||||||||||||||||||||||
| 748 | ? $results | ||||||||||||||||||||||||||||||||||||||||||
| 749 | : CatalystX::CRUD::Results->new( | ||||||||||||||||||||||||||||||||||||||||||
| 750 | { count => $count, | ||||||||||||||||||||||||||||||||||||||||||
| 751 | pager => $pager, | ||||||||||||||||||||||||||||||||||||||||||
| 752 | results => $results, | ||||||||||||||||||||||||||||||||||||||||||
| 753 | query => $query, | ||||||||||||||||||||||||||||||||||||||||||
| 754 | } | ||||||||||||||||||||||||||||||||||||||||||
| 755 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 756 | 5 | $c->stash( results => $r ); | |||||||||||||||||||||||||||||||||||||||||
| 757 | |||||||||||||||||||||||||||||||||||||||||||
| 758 | } | ||||||||||||||||||||||||||||||||||||||||||
| 759 | |||||||||||||||||||||||||||||||||||||||||||
| 760 | =head2 do_model( I<ctx>, I<args> ) | ||||||||||||||||||||||||||||||||||||||||||
| 761 | |||||||||||||||||||||||||||||||||||||||||||
| 762 | Wrapper around the ControllerRole method of the same name. | ||||||||||||||||||||||||||||||||||||||||||
| 763 | The wrapper does an eval and sets the I<ctx> error param | ||||||||||||||||||||||||||||||||||||||||||
| 764 | with $@. | ||||||||||||||||||||||||||||||||||||||||||
| 765 | |||||||||||||||||||||||||||||||||||||||||||
| 766 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 767 | |||||||||||||||||||||||||||||||||||||||||||
| 768 | 37 | ▶ | around 'do_model' => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 769 | 37 | my ( $orig, $self, $c, @args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 770 | 37 | my $results; | |||||||||||||||||||||||||||||||||||||||||
| 771 | 37 | eval { $results = $self->$orig( $c, @args ); }; | |||||||||||||||||||||||||||||||||||||||||
| 772 | 37 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 773 | 1 | $c->error($@); # re-throw | |||||||||||||||||||||||||||||||||||||||||
| 774 | 1 | return $results; | |||||||||||||||||||||||||||||||||||||||||
| 775 | } | ||||||||||||||||||||||||||||||||||||||||||
| 776 | 36 | return $results; | |||||||||||||||||||||||||||||||||||||||||
| 777 | 3 | }; | |||||||||||||||||||||||||||||||||||||||||
| 778 | |||||||||||||||||||||||||||||||||||||||||||
| 779 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 780 | |||||||||||||||||||||||||||||||||||||||||||
| 781 | __END__ | ||||||||||||||||||||||||||||||||||||||||||