| 1 | package Catalyst::Controller::REST::DBIC::Item; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use base qw/Catalyst::Controller::REST/; | ||
| 7 | |||
| 8 | use Carp; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Catalyst::Controller::REST::DBIC::Item - (EXPERIMENTAL) Common base class for typical REST | ||
| 13 | actions against a DBIx::Class object | ||
| 14 | |||
| 15 | =head1 VERSION | ||
| 16 | |||
| 17 | Version 0.001000_001 - DEVELOPER RELEASE | ||
| 18 | |||
| 19 | Because this is a developer release, please send any feedback or suggestions | ||
| 20 | to me either through RT or email at C<< cpan@coldhardcode.com >> | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | our $VERSION = '0.001000_001'; | ||
| 25 | |||
| 26 | =head1 DESCRIPTION | ||
| 27 | |||
| 28 | This is an experimental controller base class that is designed to build some | ||
| 29 | conventions around integrating REST and DBIC together in a Catalyst | ||
| 30 | application. | ||
| 31 | |||
| 32 | The idea of this class is that, in the simplest terms, each item in a REST | ||
| 33 | application has one URL to interface with it. Typically, these objects have a | ||
| 34 | one to one mapping with a backend schema class. This base class helps to reduce | ||
| 35 | the repetition in your REST classes and allow you to focus on just handling | ||
| 36 | the individual verbs (GET, PUT, POST, etc) | ||
| 37 | |||
| 38 | =head1 PHILOSOPHY | ||
| 39 | |||
| 40 | The general ideas for this module are such that I don't expect it to be of the | ||
| 41 | most use for a CRUD application designed to function in a browser. Most | ||
| 42 | modern browsers support all the REST verbs via XmlHttpRequest but standard | ||
| 43 | requests are still limited to GET and POST. This package will have the most | ||
| 44 | influence in a webservice scenario, and I hope that adapter classes can be | ||
| 45 | built upon this one that will combine L<Catalyst::Request::REST::ForBrowsers> | ||
| 46 | and other utilities to create controllers that work equally well in a | ||
| 47 | browser-based setting. | ||
| 48 | |||
| 49 | |||
| 50 | =head1 SYNOPSIS | ||
| 51 | |||
| 52 | package MyApp::Controller::Person; | ||
| 53 | |||
| 54 | use base 'Catalyst::Controller::REST::DBIC::Item'; | ||
| 55 | |||
| 56 | __PACKAGE__->config( | ||
| 57 | # Add REST config | ||
| 58 | 'default' => 'text/html' | ||
| 59 | # And, our bindings to DBIC, will use | ||
| 60 | # $c->model('Schema::Person') effectively. | ||
| 61 | class => 'Schema::Person', | ||
| 62 | # To find a record, $c->model($class)->search( $item_key => $key ) | ||
| 63 | item_key => 'name', | ||
| 64 | # If you want to call a method before sending it to the serializers | ||
| 65 | serialize_method => 'serialize', | ||
| 66 | # Set to 0 if you don't want to call the above serialize method | ||
| 67 | # if the request is in a browser | ||
| 68 | browser_serialize => 0 | ||
| 69 | ); | ||
| 70 | |||
| 71 | # This operates off of chained, and requires rest_base to be defined | ||
| 72 | sub rest_base : Chained('.') PathPart('person') CaptureArgs(0) { } | ||
| 73 | |||
| 74 | =head1 Overriding Default Methods | ||
| 75 | |||
| 76 | Catalyst::Controller::REST::DBIC::Item operates by using Chained, which allows | ||
| 77 | for midpoints to be overridden easily as long as you maintain the conventions. | ||
| 78 | |||
| 79 | If you want to override one of the default chained midpoints, you only have | ||
| 80 | to be mindful of which stash key to set. By default, the stash key: | ||
| 81 | C<< $c->stash->{rest}->{item} >> is used for fetching a single item. If you | ||
| 82 | want to add additional DBIC logic, such as a prefetch, you can override | ||
| 83 | the item_base midpoint and properly set the stash. Here is an example from | ||
| 84 | the L<http://www.iwatchsoccer.com> source (available at | ||
| 85 | L<http://our.coldhardcode.com/svn/IWS>, which prefetches games to reduce the | ||
| 86 | total number of queries. | ||
| 87 | |||
| 88 | sub item_base : Chained('rest_base') PathPart('') CaptureArgs(1) { | ||
| 89 | my ( $self, $c, $identifier ) = @_; | ||
| 90 | |||
| 91 | # just populate $c->stash->{rest}->{item} and item_GET, etc | ||
| 92 | $c->stash->{rest}->{item} = $self->get_item($c, $identifier)->search( | ||
| 93 | { | ||
| 94 | 'games.end_time' => { '>=', $c->stash->{now} } | ||
| 95 | }, | ||
| 96 | { | ||
| 97 | prefetch => { | ||
| 98 | games => [ | ||
| 99 | 'home', 'visitor', | ||
| 100 | { broadcasts => 'network' } | ||
| 101 | ] | ||
| 102 | } | ||
| 103 | } | ||
| 104 | )->single; | ||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | =head1 CONFIGURATION | ||
| 109 | |||
| 110 | =over | ||
| 111 | |||
| 112 | =item class | ||
| 113 | |||
| 114 | What is your L<DBIx::Class> schema class to attach to? This is the part you | ||
| 115 | would put in C<< $c->model(...) >> | ||
| 116 | |||
| 117 | =item item_key | ||
| 118 | |||
| 119 | What goes in C<< ->search({ item_key => $identifier }) >> | ||
| 120 | |||
| 121 | =item serialize_method | ||
| 122 | |||
| 123 | Call this on result sets and items to serialize them. This is useful as | ||
| 124 | a base class in your DBIC schema (or just use get_columns) when you want to | ||
| 125 | support serializers that won't touch blessed objects, or don't want to serialize | ||
| 126 | the blessed object | ||
| 127 | |||
| 128 | =item browser_serialize | ||
| 129 | |||
| 130 | Set this value to 0 if you don't want to serialize a browser request. This | ||
| 131 | requires you use L<Catalyst::Request::REST::ForBrowsers>, and will throw an | ||
| 132 | exception if you don't use it. | ||
| 133 | |||
| 134 | =back | ||
| 135 | |||
| 136 | =head1 GENERATED REST METHODS | ||
| 137 | |||
| 138 | =head2 rest_list PathPart('') | ||
| 139 | |||
| 140 | =cut | ||
| 141 | |||
| 142 | sub rest_list : Chained('rest_base') PathPart('') Args(0) ActionClass('REST') { | ||
| 143 | |||
| 144 | } | ||
| 145 | |||
| 146 | =head2 rest_list_GET | ||
| 147 | |||
| 148 | The simple method that handles GET actions on the list action (A call to | ||
| 149 | C</foo>). | ||
| 150 | |||
| 151 | This method will call the C<serialize_method>, if configured, as well as respect | ||
| 152 | the C<browser_serialize> configuration setting if the request looks like it | ||
| 153 | is coming from a browser (which requires L<Catalyst::Request::REST::ForBrowsers> | ||
| 154 | to be loaded). | ||
| 155 | |||
| 156 | =cut | ||
| 157 | |||
| 158 | sub rest_list_GET { | ||
| 159 | my ( $self, $c ) = @_; | ||
| 160 | |||
| 161 | my $rs = $self->get_rs($c); | ||
| 162 | my $entity; | ||
| 163 | |||
| 164 | if ( defined $self->{browser_serialize} and | ||
| 165 | $self->{browser_serialize} == 0 and | ||
| 166 | $c->req->looks_like_browser | ||
| 167 | ) { | ||
| 168 | $entity = $rs; | ||
| 169 | } | ||
| 170 | elsif ( my $method = $self->{serialize_method} ) { | ||
| 171 | $entity = $rs->can($method) ? $rs->$method : $rs; | ||
| 172 | } else { | ||
| 173 | $entity = $rs; | ||
| 174 | } | ||
| 175 | return $self->status_ok( $c, entity => $entity ); | ||
| 176 | } | ||
| 177 | |||
| 178 | |||
| 179 | =head2 rest_item_base : Chained('rest_base') PathPart('') CaptureArgs(1) | ||
| 180 | |||
| 181 | This chain midpoint fetches the record by the captured argument (which should | ||
| 182 | be the record identifier). | ||
| 183 | |||
| 184 | Override this method if you want to customize how the record is fetched. It | ||
| 185 | is expected that if a record is found, C<< $c->stash->{rest}->{item} >> is set. | ||
| 186 | If not, the default GET request will return not found. | ||
| 187 | |||
| 188 | =cut | ||
| 189 | |||
| 190 | sub rest_item_base : Chained('rest_base') PathPart('') CaptureArgs(1) { | ||
| 191 | my ( $self, $c, $pk1 ) = @_; | ||
| 192 | |||
| 193 | $c->stash->{rest}->{item} = $self->get_item( $c, $pk1 )->single; | ||
| 194 | } | ||
| 195 | |||
| 196 | =head2 rest_item | ||
| 197 | |||
| 198 | This method chains to rest_item_base, has 0 arguments and uses | ||
| 199 | L<Catalyst::Action::REST>. | ||
| 200 | |||
| 201 | This method is used to handle triggering the single item methods. Override | ||
| 202 | rest_item_GET, rest_item_POST, etc to have custom serialization behavior. | ||
| 203 | |||
| 204 | If you want to override the item selection, override rest_item_base | ||
| 205 | |||
| 206 | =cut | ||
| 207 | |||
| 208 | sub rest_item : Chained('rest_item_base') PathPart('') Args(0) ActionClass('REST') { | ||
| 209 | my ( $self, $c ) = @_; | ||
| 210 | } | ||
| 211 | |||
| 212 | =head2 rest_item_GET | ||
| 213 | |||
| 214 | The default method for handling a simple GET request. This will serialize | ||
| 215 | however L<Catalyst::Controller::REST> is configured for the controller, and | ||
| 216 | will call the C<serialize_method> if configured (while respecting the | ||
| 217 | C<browser_serialize> configuration setting). | ||
| 218 | |||
| 219 | If the item is not found (which means C<< $c->stash->{rest}->{item} >> is not | ||
| 220 | defined) it will return a status_not_found result, with the message: | ||
| 221 | |||
| 222 | message => $c->localize('ITEM_NOT_FOUND') ); | ||
| 223 | |||
| 224 | =cut | ||
| 225 | |||
| 226 | sub rest_item_GET { | ||
| 227 | my ( $self, $c ) = @_; | ||
| 228 | my $item; | ||
| 229 | unless ( $item = $c->stash->{rest}->{item} ) { | ||
| 230 | return $self->status_not_found( $c, | ||
| 231 | message => $c->localize('ITEM_NOT_FOUND') ); | ||
| 232 | } | ||
| 233 | |||
| 234 | my $entity; | ||
| 235 | if ( defined $self->{browser_serialize} and | ||
| 236 | $self->{browser_serialize} == 0 and | ||
| 237 | $c->req->looks_like_browser | ||
| 238 | ) { | ||
| 239 | $entity = $item; | ||
| 240 | } | ||
| 241 | elsif ( my $method = $self->{serialize_method} ) { | ||
| 242 | $c->log->debug("Serializing with $method ($self->{browser_serialize} and " . $c->req->looks_like_browser . ")"); | ||
| 243 | $entity = $item->can($method) ? $item->$method : $item; | ||
| 244 | } else { | ||
| 245 | $entity = $item; | ||
| 246 | } | ||
| 247 | |||
| 248 | return $self->status_ok( $c, entity => $entity ); | ||
| 249 | } | ||
| 250 | |||
| 251 | =head1 HELPER METHODS | ||
| 252 | |||
| 253 | The following methods are convenience methods that can be used in your methods | ||
| 254 | to fetch items out of DBIC. | ||
| 255 | |||
| 256 | =head2 get_item($context, $identifier) | ||
| 257 | |||
| 258 | This method should return a result set that points to a single item that is | ||
| 259 | identified by $identifier. | ||
| 260 | |||
| 261 | If the configuration key C<item_key> is defined, it will do a search where | ||
| 262 | C<item_key == $identifier>, otherwise just a simple pull of the result | ||
| 263 | source primary keys, which is most likely what is expected but may not be | ||
| 264 | what you want. | ||
| 265 | |||
| 266 | =cut | ||
| 267 | |||
| 268 | sub get_item { | ||
| 269 | my ( $self, $c, $item ) = @_; | ||
| 270 | my $rs = $self->get_rs($c); | ||
| 271 | |||
| 272 | if ( my $key = $self->{item_key} ) { | ||
| 273 | return $rs->search({ "me.$key" => $item }); | ||
| 274 | } else { | ||
| 275 | my @cols = $rs->primary_columns; | ||
| 276 | if ( @cols == 1 ) { | ||
| 277 | return $self->get_rs($c)->search({ "me.$cols[0]" => $item }); | ||
| 278 | } | ||
| 279 | $c->log->error("Can't fetch item if schema has multiple primary keys and item_key is not defined."); | ||
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | =head2 get_rs($context) | ||
| 284 | |||
| 285 | This fetches the result set to be used. This is a good point to override for | ||
| 286 | adding things like pagination. A simple example for overriding would be: | ||
| 287 | |||
| 288 | sub get_rs { | ||
| 289 | my $rs = shift->next::method(@_); | ||
| 290 | return $rs->search(undef, { rows => 10, page => 1 }); | ||
| 291 | } | ||
| 292 | |||
| 293 | Just make sure this always returns a result set. | ||
| 294 | |||
| 295 | =cut | ||
| 296 | |||
| 297 | sub get_rs { | ||
| 298 | my ( $self, $c ) = @_; | ||
| 299 | my $model = $self->{class}; | ||
| 300 | my $rs = $c->model($model); | ||
| 301 | croak "Unable to find DBIC resultset $model, check config" | ||
| 302 | unless $rs; | ||
| 303 | return $rs; | ||
| 304 | } | ||
| 305 | |||
| 306 | |||
| 307 | =head1 AUTHOR | ||
| 308 | |||
| 309 | J. Shirley, C<< <jshirley at coldhardcode.com> >> | ||
| 310 | |||
| 311 | =head1 BUGS | ||
| 312 | |||
| 313 | Please report any bugs or feature requests to C<bug-catalyst-controller-rest-dbic-item at rt.cpan.org>, or through | ||
| 314 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-REST-DBIC-Item>. I will be notified, and then you'll | ||
| 315 | automatically be notified of progress on your bug as I make changes. | ||
| 316 | |||
| 317 | |||
| 318 | =head1 SUPPORT | ||
| 319 | |||
| 320 | You can find documentation for this module with the perldoc command. | ||
| 321 | |||
| 322 | perldoc Catalyst::Controller::REST::DBIC::Item | ||
| 323 | |||
| 324 | |||
| 325 | You can also look for information at: | ||
| 326 | |||
| 327 | =over 4 | ||
| 328 | |||
| 329 | =item * RT: CPAN's request tracker | ||
| 330 | |||
| 331 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-REST-DBIC-Item> | ||
| 332 | |||
| 333 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 334 | |||
| 335 | L<http://annocpan.org/dist/Catalyst-Controller-REST-DBIC-Item> | ||
| 336 | |||
| 337 | =item * CPAN Ratings | ||
| 338 | |||
| 339 | L<http://cpanratings.perl.org/d/Catalyst-Controller-REST-DBIC-Item> | ||
| 340 | |||
| 341 | =item * Search CPAN | ||
| 342 | |||
| 343 | L<http://search.cpan.org/dist/Catalyst-Controller-REST-DBIC-Item> | ||
| 344 | |||
| 345 | =back | ||
| 346 | |||
| 347 | |||
| 348 | =head1 ACKNOWLEDGEMENTS | ||
| 349 | |||
| 350 | |||
| 351 | =head1 COPYRIGHT & LICENSE | ||
| 352 | |||
| 353 | Copyright 2008 Cold Hard Code, LLC, all rights reserved. | ||
| 354 | |||
| 355 | This program is free software; you can redistribute it and/or modify it | ||
| 356 | under the same terms as Perl itself. | ||
| 357 | |||
| 358 | |||
| 359 | =cut | ||
| 360 | |||
| 361 | 1; # End of Catalyst::Controller::REST::DBIC::Item | ||
| 362 | |||
| 363 |