Use ActionRole::MatchRequestMethod instead of Action::REST.
[catagits/Catalyst-Controller-DBIC-API.git] / lib / Catalyst / Controller / DBIC / API / REST.pm
1 package Catalyst::Controller::DBIC::API::REST;
2
3 #ABSTRACT: Provides a REST interface to DBIx::Class
4 use Moose;
5 BEGIN { 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
17 Provides a REST style API interface to the functionality described in L<Catalyst::Controller::DBIC::API>.
18
19 By default provides the following endpoints:
20
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)
23
24 Where $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
28 Chained: override
29 PathPart: override
30 CaptureArgs: 0
31
32 As 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 no_id
40
41 Chained: L</objects_no_id>
42 PathPart: none
43 CaptureArgs: 0
44
45 Calls list level methods described in L<Catalyst::Controller::DBIC::API> as follows:
46
47 DELETE: L<Catalyst::Controller::DBIC::API/delete>
48 POST/PUT: L<Catalyst::Controller::DBIC::API/update_or_create>
49 GET: forwards to L<Catalyst::Controller::DBIC::API/list>
50
51 =cut
52
53 sub update_or_create_objects : Chained('objects_no_id') PathPart('') Does('MatchRequestMethod') Method('POST') Method('PUT') Args(0)
54 {
55         my ( $self, $c ) = @_;
56     $self->update_or_create($c);
57 }
58
59 sub delete_many_objects : Chained('objects_no_id') PathPart('') Does('MatchRequestMethod') Method('DELETE') Args(0)
60 {
61         my ( $self, $c ) = @_;
62     $self->delete($c);
63 }
64
65 sub list_objects : Chained('objects_no_id') PathPart('') Does('MatchRequestMethod') Method('GET') Args(0)
66 {
67         my ( $self, $c ) = @_;
68     $self->list($c);
69 }
70
71 =method_protected with_id
72
73 Chained: L</object_with_id>
74 PathPart: none
75 CaptureArgs: 0
76
77 Forwards to list level methods described in L<Catalyst::Controller::DBIC::API> as follows:
78
79 DELETE: L<Catalyst::Controller::DBIC::API/delete>
80 POST/PUT: L<Catalyst::Controller::DBIC::API/update_or_create>
81 GET: forwards to L<Catalyst::Controller::DBIC::API/item>
82
83 =cut
84
85 sub update_or_create_one_object : Chained('object_with_id') PathPart('') Does('MatchRequestMethod') Method('POST') Method('PUT') Args(0)
86 {
87         my ( $self, $c ) = @_;
88     $self->update_or_create($c);
89 }
90
91 sub delete_one_object : Chained('object_with_id') PathPart('') Does('MatchRequestMethod') Method('DELETE') Args(0)
92 {
93         my ( $self, $c ) = @_;
94     $self->delete($c);
95 }
96
97 sub list_one_object : Chained('object_with_id') PathPart('') Does('MatchRequestMethod') Method('GET') Args(0)
98 {
99         my ( $self, $c ) = @_;
100     $self->item($c);
101 }
102
103 1;