Version 2.008001
[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
16 =head1 DESCRIPTION
17
18 Provides a REST style API interface to the functionality described in
19 L<Catalyst::Controller::DBIC::API>.
20
21 By default provides the following endpoints:
22
23   $base (operates on lists of objects and accepts GET, PUT, POST and DELETE)
24   $base/[identifier] (operates on a single object and accepts GET, PUT, POST and DELETE)
25
26 Where $base is the URI described by L</setup>, the chain root of the controller
27 and the request type will determine the L<Catalyst::Controller::DBIC::API>
28 method to forward.
29
30 =method_protected setup
31
32 Chained: override
33 PathPart: override
34 CaptureArgs: 0
35
36 As described in L<Catalyst::Controller::DBIC::API/setup>, this action is the
37 chain root of the controller but has no pathpart or chain parent defined by
38 default.
39
40 These must be defined in order for the controller to function.
41
42 The neatest way is normally to define these using the controller's config.
43
44   __PACKAGE__->config
45     ( action => { setup => { PathPart => 'track', Chained => '/api/rest/rest_base' } },
46         ...
47   );
48
49 =method_protected update_or_create_objects
50
51 Chained: L</objects_no_id>
52 PathPart: none
53 Args: 0
54 Method: POST/PUT
55
56 Calls L<Catalyst::Controller::DBIC::API/update_or_create>. 
57
58 =cut
59
60 sub update_or_create_objects : POST PUT Chained('objects_no_id') PathPart('')
61     Args(0) {
62     my ( $self, $c ) = @_;
63     $self->update_or_create($c);
64 }
65
66 =method_protected delete_many_objects
67
68 Chained: L</objects_no_id>
69 PathPart: none
70 Args: 0
71 Method: DELETE
72
73 Calls L<Catalyst::Controller::DBIC::API/delete>. 
74
75 =cut
76
77 sub delete_many_objects : DELETE Chained('objects_no_id') PathPart('')
78     Args(0) {
79     my ( $self, $c ) = @_;
80     $self->delete($c);
81 }
82
83 =method_protected list_objects
84
85 Chained: L</objects_no_id>
86 PathPart: none
87 Args: 0
88 Method: GET
89
90 Calls L<Catalyst::Controller::DBIC::API/list>. 
91
92 =cut
93
94 sub list_objects : GET Chained('objects_no_id') PathPart('') Args(0) {
95     my ( $self, $c ) = @_;
96     $self->list($c);
97 }
98
99 =method_protected update_or_create_one_object
100
101 Chained: L</object_with_id>
102 PathPart: none
103 Args: 0
104 Method: POST/PUT
105
106 Calls L<Catalyst::Controller::DBIC::API/update_or_create>.
107
108 =cut
109
110 sub update_or_create_one_object : POST PUT Chained('object_with_id')
111     PathPart('') Args(0) {
112     my ( $self, $c ) = @_;
113     $self->update_or_create($c);
114 }
115
116 =method_protected delete_one_object
117
118 Chained: L</object_with_id>
119 PathPart: none
120 Args: 0
121 Method: DELETE
122
123 Calls L<Catalyst::Controller::DBIC::API/delete>.
124
125 =cut
126
127 sub delete_one_object : DELETE Chained('object_with_id') PathPart('') Args(0)
128 {
129     my ( $self, $c ) = @_;
130     $self->delete($c);
131 }
132
133 =method_protected list_one_object
134
135 Chained: L</object_with_id>
136 PathPart: none
137 Args: 0
138 Method: GET
139
140 Calls L<Catalyst::Controller::DBIC::API/item>.
141
142 =cut
143
144 sub list_one_object : GET Chained('object_with_id') PathPart('') Args(0) {
145     my ( $self, $c ) = @_;
146     $self->item($c);
147 }
148
149 1;