initial commit with working tests, docs, and conversion to dzil+podweaver
[catagits/Catalyst-Controller-DBIC-API.git] / lib / Catalyst / Controller / DBIC / API / REST.pm
CommitLineData
d2739840 1package Catalyst::Controller::DBIC::API::REST;
2
3#ABSTRACT: Provides a REST interface to DBIx::Class
4use Moose;
5BEGIN { extends 'Catalyst::Controller::DBIC::API'; }
6
7__PACKAGE__->config(
8 'default' => 'application/json',
9 'stash_key' => 'response',
10 'map' => {
11 'application/x-www-form-urlencoded' => 'JSON',
12 'application/json' => 'JSON',
13 });
14
15=head1 DESCRIPTION
16
17Provides a REST style API interface to the functionality described in L<Catalyst::Controller::DBIC::API>.
18
19By default provides the following endpoints:
20
21 $base (accepts PUT and GET)
22 $base/[identifier] (accepts POST and DELETE)
23
24Where $base is the URI described by L</setup>, the chain root of the controller, and the request type will determine the L<Catalyst::Controller::DBIC::API> method to forward.
25
26=method_protected setup
27
28Chained: override
29PathPart: override
30CaptureArgs: 0
31
32As described in L<Catalyst::Controller::DBIC::API/setup>, this action is the chain root of the controller but has no pathpart or chain parent defined by default, so these must be defined in order for the controller to function. The neatest way is normally to define these using the controller's config.
33
34 __PACKAGE__->config
35 ( action => { setup => { PathPart => 'track', Chained => '/api/rest/rest_base' } },
36 ...
37 );
38
39=method_protected base
40
41Chained: L</setup>
42PathPart: none
43CaptureArgs: 0
44
45Forwards to list level methods described in L<Catalyst::Controller::DBIC::API> as follows:
46
47DELETE: forwards to L<Catalyst::Controller::DBIC::API/object> then L<Catalyst::Controller::DBIC::API/delete>
48POST/PUT: forwards to L<Catalyst::Controller::DBIC::API/object> then L<Catalyst::Controller::DBIC::API/update_or_create>
49GET: forwards to L<Catalyst::Controller::DBIC::API/list>
50
51=cut
52
53sub base : Chained('setup') PathPart('') ActionClass('REST') Args {
54 my ( $self, $c ) = @_;
55
56}
57
58sub base_PUT {
59 my ( $self, $c ) = @_;
60
61 $c->forward('object');
62 return if $self->get_errors($c);
63 $c->forward('update_or_create');
64}
65
66sub base_POST {
67 my ( $self, $c ) = @_;
68
69 $c->forward('object');
70 return if $self->get_errors($c);
71 $c->forward('update_or_create');
72}
73
74sub base_DELETE {
75 my ( $self, $c ) = @_;
76 $DB::single =1;
77 $c->forward('object');
78 return if $self->get_errors($c);
79 $c->forward('delete');
80}
81
82sub base_GET {
83 my ( $self, $c ) = @_;
84
85 $c->forward('list');
86}
87
881;