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 | |
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 | |
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 |
35 | ( action => { setup => { PathPart => 'track', Chained => '/api/rest/rest_base' } }, |
36 | ... |
37 | ); |
38 | |
609916e5 |
39 | =method_protected no_id |
d2739840 |
40 | |
533075c7 |
41 | Chained: L</objects_no_id> |
d2739840 |
42 | PathPart: none |
43 | CaptureArgs: 0 |
44 | |
45 | Forwards to list level methods described in L<Catalyst::Controller::DBIC::API> as follows: |
46 | |
609916e5 |
47 | DELETE: L<Catalyst::Controller::DBIC::API/delete> |
48 | POST/PUT: L<Catalyst::Controller::DBIC::API/update_or_create> |
d2739840 |
49 | GET: forwards to L<Catalyst::Controller::DBIC::API/list> |
50 | |
51 | =cut |
52 | |
533075c7 |
53 | sub no_id : Chained('objects_no_id') PathPart('') ActionClass('REST') {} |
d2739840 |
54 | |
8cf0b66a |
55 | sub no_id_PUT |
56 | { |
d2739840 |
57 | my ( $self, $c ) = @_; |
d2739840 |
58 | $c->forward('update_or_create'); |
59 | } |
60 | |
8cf0b66a |
61 | sub no_id_POST |
62 | { |
d2739840 |
63 | my ( $self, $c ) = @_; |
d2739840 |
64 | $c->forward('update_or_create'); |
65 | } |
66 | |
8cf0b66a |
67 | sub no_id_DELETE |
68 | { |
d2739840 |
69 | my ( $self, $c ) = @_; |
d2739840 |
70 | $c->forward('delete'); |
71 | } |
72 | |
8cf0b66a |
73 | sub no_id_GET |
74 | { |
d2739840 |
75 | my ( $self, $c ) = @_; |
d2739840 |
76 | $c->forward('list'); |
77 | } |
78 | |
609916e5 |
79 | =method_protected with_id |
80 | |
81 | Chained: L</object_with_id> |
82 | PathPart: none |
83 | CaptureArgs: 0 |
84 | |
85 | Forwards to list level methods described in L<Catalyst::Controller::DBIC::API> as follows: |
86 | |
87 | DELETE: L<Catalyst::Controller::DBIC::API/delete> |
88 | POST/PUT: L<Catalyst::Controller::DBIC::API/update_or_create> |
89 | GET: forwards to L<Catalyst::Controller::DBIC::API/item> |
90 | |
91 | =cut |
92 | |
533075c7 |
93 | sub with_id :Chained('object_with_id') :PathPart('') :ActionClass('REST') {} |
8cf0b66a |
94 | |
95 | sub with_id_PUT |
96 | { |
97 | my ( $self, $c ) = @_; |
98 | $c->forward('update_or_create'); |
99 | } |
100 | |
101 | sub with_id_POST |
102 | { |
103 | my ( $self, $c ) = @_; |
104 | $c->forward('update_or_create'); |
105 | } |
106 | |
107 | sub with_id_DELETE |
108 | { |
109 | my ( $self, $c ) = @_; |
110 | $c->forward('delete'); |
111 | } |
112 | |
113 | sub with_id_GET |
114 | { |
115 | my ( $self, $c ) = @_; |
116 | $c->forward('item'); |
117 | } |
118 | |
d2739840 |
119 | 1; |