Commit | Line | Data |
d2739840 |
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 | |
406086f3 |
17 | Provides a REST style API interface to the functionality described in L<Catalyst::Controller::DBIC::API>. |
d2739840 |
18 | |
19 | By 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 | |
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 |
406086f3 |
35 | ( action => { setup => { PathPart => 'track', Chained => '/api/rest/rest_base' } }, |
d2739840 |
36 | ... |
37 | ); |
38 | |
b66d4310 |
39 | =method_protected update_or_create_objects |
d2739840 |
40 | |
533075c7 |
41 | Chained: L</objects_no_id> |
d2739840 |
42 | PathPart: none |
b66d4310 |
43 | Args: 0 |
44 | Method: POST/PUT |
d2739840 |
45 | |
b66d4310 |
46 | Calls L<Catalyst::Controller::DBIC::API/update_or_create>. |
d2739840 |
47 | |
48 | =cut |
49 | |
a784948c |
50 | sub update_or_create_objects : Chained('objects_no_id') PathPart('') Does('MatchRequestMethod') Method('POST') Method('PUT') Args(0) |
8cf0b66a |
51 | { |
d2739840 |
52 | my ( $self, $c ) = @_; |
3d85db11 |
53 | $self->update_or_create($c); |
d2739840 |
54 | } |
55 | |
b66d4310 |
56 | =method_protected delete_many_objects |
57 | |
58 | Chained: L</objects_no_id> |
59 | PathPart: none |
60 | Args: 0 |
61 | Method: DELETE |
62 | |
63 | Calls L<Catalyst::Controller::DBIC::API/delete>. |
64 | |
65 | =cut |
66 | |
a784948c |
67 | sub delete_many_objects : Chained('objects_no_id') PathPart('') Does('MatchRequestMethod') Method('DELETE') Args(0) |
8cf0b66a |
68 | { |
d2739840 |
69 | my ( $self, $c ) = @_; |
3d85db11 |
70 | $self->delete($c); |
d2739840 |
71 | } |
72 | |
b66d4310 |
73 | =method_protected list_objects |
74 | |
75 | Chained: L</objects_no_id> |
76 | PathPart: none |
77 | Args: 0 |
78 | Method: GET |
79 | |
80 | Calls L<Catalyst::Controller::DBIC::API/list>. |
81 | |
82 | =cut |
83 | |
a784948c |
84 | sub list_objects : Chained('objects_no_id') PathPart('') Does('MatchRequestMethod') Method('GET') Args(0) |
8cf0b66a |
85 | { |
d2739840 |
86 | my ( $self, $c ) = @_; |
3d85db11 |
87 | $self->list($c); |
d2739840 |
88 | } |
89 | |
b66d4310 |
90 | =method_protected update_or_create_one_object |
609916e5 |
91 | |
92 | Chained: L</object_with_id> |
93 | PathPart: none |
b66d4310 |
94 | Args: 0 |
95 | Method: POST/PUT |
609916e5 |
96 | |
b66d4310 |
97 | Calls L<Catalyst::Controller::DBIC::API/update_or_create>. |
609916e5 |
98 | |
99 | =cut |
100 | |
a784948c |
101 | sub update_or_create_one_object : Chained('object_with_id') PathPart('') Does('MatchRequestMethod') Method('POST') Method('PUT') Args(0) |
8cf0b66a |
102 | { |
103 | my ( $self, $c ) = @_; |
3d85db11 |
104 | $self->update_or_create($c); |
8cf0b66a |
105 | } |
106 | |
b66d4310 |
107 | =method_protected delete_one_object |
108 | |
109 | Chained: L</object_with_id> |
110 | PathPart: none |
111 | Args: 0 |
112 | Method: DELETE |
113 | |
114 | Calls L<Catalyst::Controller::DBIC::API/delete>. |
115 | |
116 | =cut |
117 | |
a784948c |
118 | sub delete_one_object : Chained('object_with_id') PathPart('') Does('MatchRequestMethod') Method('DELETE') Args(0) |
8cf0b66a |
119 | { |
120 | my ( $self, $c ) = @_; |
3d85db11 |
121 | $self->delete($c); |
8cf0b66a |
122 | } |
123 | |
b66d4310 |
124 | =method_protected list_one_object |
125 | |
126 | Chained: L</object_with_id> |
127 | PathPart: none |
128 | Args: 0 |
129 | Method: GET |
130 | |
131 | Calls L<Catalyst::Controller::DBIC::API/item>. |
132 | |
133 | =cut |
134 | |
a784948c |
135 | sub list_one_object : Chained('object_with_id') PathPart('') Does('MatchRequestMethod') Method('GET') Args(0) |
8cf0b66a |
136 | { |
137 | my ( $self, $c ) = @_; |
3d85db11 |
138 | $self->item($c); |
8cf0b66a |
139 | } |
140 | |
d2739840 |
141 | 1; |