Use ActionRole::MatchRequestMethod instead of Action::REST.
[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
406086f3 17Provides a REST style API interface to the functionality described in L<Catalyst::Controller::DBIC::API>.
d2739840 18
19By default provides the following endpoints:
20
609916e5 21 $base (operates on lists of objects and accepts GET, PUT, POST and DELETE)
22 $base/[identifier] (operates on a single object and accepts GET, PUT, POST and DELETE)
d2739840 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
406086f3 35 ( action => { setup => { PathPart => 'track', Chained => '/api/rest/rest_base' } },
d2739840 36 ...
37 );
38
609916e5 39=method_protected no_id
d2739840 40
533075c7 41Chained: L</objects_no_id>
d2739840 42PathPart: none
43CaptureArgs: 0
44
3d85db11 45Calls list level methods described in L<Catalyst::Controller::DBIC::API> as follows:
d2739840 46
609916e5 47DELETE: L<Catalyst::Controller::DBIC::API/delete>
48POST/PUT: L<Catalyst::Controller::DBIC::API/update_or_create>
d2739840 49GET: forwards to L<Catalyst::Controller::DBIC::API/list>
50
51=cut
52
a784948c 53sub update_or_create_objects : Chained('objects_no_id') PathPart('') Does('MatchRequestMethod') Method('POST') Method('PUT') Args(0)
8cf0b66a 54{
d2739840 55 my ( $self, $c ) = @_;
3d85db11 56 $self->update_or_create($c);
d2739840 57}
58
a784948c 59sub delete_many_objects : Chained('objects_no_id') PathPart('') Does('MatchRequestMethod') Method('DELETE') Args(0)
8cf0b66a 60{
d2739840 61 my ( $self, $c ) = @_;
3d85db11 62 $self->delete($c);
d2739840 63}
64
a784948c 65sub list_objects : Chained('objects_no_id') PathPart('') Does('MatchRequestMethod') Method('GET') Args(0)
8cf0b66a 66{
d2739840 67 my ( $self, $c ) = @_;
3d85db11 68 $self->list($c);
d2739840 69}
70
609916e5 71=method_protected with_id
72
73Chained: L</object_with_id>
74PathPart: none
75CaptureArgs: 0
76
77Forwards to list level methods described in L<Catalyst::Controller::DBIC::API> as follows:
78
79DELETE: L<Catalyst::Controller::DBIC::API/delete>
80POST/PUT: L<Catalyst::Controller::DBIC::API/update_or_create>
81GET: forwards to L<Catalyst::Controller::DBIC::API/item>
82
83=cut
84
a784948c 85sub update_or_create_one_object : Chained('object_with_id') PathPart('') Does('MatchRequestMethod') Method('POST') Method('PUT') Args(0)
8cf0b66a 86{
87 my ( $self, $c ) = @_;
3d85db11 88 $self->update_or_create($c);
8cf0b66a 89}
90
a784948c 91sub delete_one_object : Chained('object_with_id') PathPart('') Does('MatchRequestMethod') Method('DELETE') Args(0)
8cf0b66a 92{
93 my ( $self, $c ) = @_;
3d85db11 94 $self->delete($c);
8cf0b66a 95}
96
a784948c 97sub list_one_object : Chained('object_with_id') PathPart('') Does('MatchRequestMethod') Method('GET') Args(0)
8cf0b66a 98{
99 my ( $self, $c ) = @_;
3d85db11 100 $self->item($c);
8cf0b66a 101}
102
d2739840 1031;